Beispiel #1
0
 public void TestEncode()
 {
     var eq = new Equilateral(3, -1, 1);
     double[] d = eq.Encode(1);
     Assert.AreEqual(0.8660254037844386, d[0], AIFH.DefaultPrecision);
     Assert.AreEqual(-0.5, d[1], AIFH.DefaultPrecision);
 }
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
        /**
         * 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
         *
         * @param column   The column to use.
         * @param offValue The off value to use.
         * @param onValue  The on value to use.
         * @return The column to index mapping (the same result as calling enumerateClasses).
         */

        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 #4
0
 public void TestDecode()
 {
     var eq = new Equilateral(3, -1, 1);
     double[] d0 = { 0.866, 0.5 };
     double[] d1 = { -0.866, 0.5 };
     double[] d2 = { 0, -1 };
     Assert.AreEqual(2, eq.Decode(d0));
     Assert.AreEqual(2, eq.Decode(d1));
     Assert.AreEqual(0, eq.Decode(d2));
 }
Beispiel #5
0
 public void TestError()
 {
     var eq = new Equilateral(3, -1, 1);
     eq.Encode(10);
 }
Beispiel #6
0
        /**
         * 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
         *
         * @param column   The column to use.
         * @param offValue The off value to use.
         * @param onValue  The on value to use.
         * @return The column to index mapping (the same result as calling enumerateClasses).
         */
        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;
        }