Beispiel #1
0
        /// <summary>
        /// Use equilateral encoding to encode a column, use zero for the off value and one for the on value.
        /// <p/>
        /// http://www.heatonresearch.com/wiki/Equilateral
        /// </summary>
        /// <param name="column">The column to use.</param>
        /// <param name="offValue">The off value to use.</param>
        /// <param name="onValue">The on value to use.</param>
        /// <returns>The column to index mapping (the same result as calling enumerateClasses).</returns>
        public IDictionary <String, int> EncodeEquilateral(int column, double offValue, double onValue)
        {
            // remember the column name
            String name = _headers[column];

            // make space for it
            IDictionary <String, int> classes = EnumerateClasses(column);
            int classCount = classes.Count;

            InsertColumns(column + 1, classCount - 1);

            // perform the equilateral
            var eq = new Equilateral(classCount, offValue, onValue);

            foreach (var obj in _data)
            {
                int index = classes[obj[column].ToString()];

                double[] encoded = eq.Encode(index);

                for (int i = 0; i < classCount - 1; i++)
                {
                    obj[column + i] = encoded[i];
                }
            }

            // name the new columns
            for (int i = 0; i < classes.Count; i++)
            {
                _headers[column + i] = name + "-" + i;
            }

            return(classes);
        }
Beispiel #2
0
        /// <summary>
        /// Query a regression algorithm using equilateral encoding.
        /// </summary>
        /// <param name="alg">The algorithm being used.</param>
        /// <param name="theTrainingData">The training data.</param>
        /// <param name="items">The category items classified.</param>
        /// <param name="high">The high value.</param>
        /// <param name="low">The low value.</param>
        public static void QueryEquilateral(
                IRegressionAlgorithm alg,
                IList<BasicData> theTrainingData,
                IDictionary<String, int> items,
                double high, double low)
        {
            // first, we need to invert the items.  Right now it maps from category to index.  We need index to category.
            IDictionary<int, String> invMap = new Dictionary<int, string>();
            foreach (string key in items.Keys)
            {
                int value = items[key];
                invMap[value] = key;
            }

            // now we can query
            Equilateral eq = new Equilateral(items.Count, high, low);
            foreach (BasicData data in theTrainingData)
            {
                double[] output = alg.ComputeRegression(data.Input);
                int idealIndex = eq.Decode(data.Ideal);
                int actualIndex = eq.Decode(output);
                Console.WriteLine(VectorUtil.DoubleArrayToString(data.Input) + " -> " + invMap[actualIndex]
                        + ", Ideal: " + invMap[idealIndex]);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Use equilateral encoding to encode a column, use zero for the off value and one for the on value.
        /// <p/>
        /// http://www.heatonresearch.com/wiki/Equilateral
        /// </summary>
        /// <param name="column">The column to use.</param>
        /// <param name="offValue">The off value to use.</param>
        /// <param name="onValue">The on value to use.</param>
        /// <returns>The column to index mapping (the same result as calling enumerateClasses).</returns>
        public IDictionary<String, int> EncodeEquilateral(int column, double offValue, double onValue)
        {
            // remember the column name
            String name = _headers[column];

            // make space for it
            IDictionary<String, int> classes = EnumerateClasses(column);
            int classCount = classes.Count;
            InsertColumns(column + 1, classCount - 1);

            // perform the equilateral
            var eq = new Equilateral(classCount, offValue, onValue);

            foreach (var obj in _data)
            {
                int index = classes[obj[column].ToString()];

                double[] encoded = eq.Encode(index);

                for (int i = 0; i < classCount - 1; i++)
                {
                    obj[column + i] = encoded[i];
                }
            }

            // name the new columns
            for (int i = 0; i < classes.Count; i++)
            {
                _headers[column + i] = name + "-" + i;
            }

            return classes;
        }