Accord.Statistics.Kernels.Polynomial.Transform C# (CSharp) Method

Transform() public static method

Projects an input point into feature space.
public static Transform ( double input, int degree, double constant ) : double[]
input double The input point to be projected into feature space.
degree int The parameter of the kernel.
constant double The parameter of the kernel.
return double[]
        public static double[] Transform(double[] input, int degree, double constant)
        {
            if (constant != 0)
            {
                throw new NotSupportedException(
                    "The explicit feature-space projection function for degrees "
                    + " higher than two is only available for homogeneous kernels"
                    + " (i.e. kernel functions with the constant term set to zero).");
            }

            int n = input.Length;
            int m = (int)Math.Pow(n, degree);

            double[] features = new double[m];

            int index = 0;
            foreach (int[] s in Combinatorics.Sequences(input.Length, degree))
            {
                double prod = 1;
                for (int i = 0; i < s.Length; i++)
                    prod *= input[s[i]];
                features[index++] = prod;
            }

            return features;
        }
    } 

Same methods

Polynomial::Transform ( double input ) : double[]

Usage Example

        public void LearnTest()
        {

            double[][] inputs =
            {
                new double[] { -1, -1 },
                new double[] { -1,  1 },
                new double[] {  1, -1 },
                new double[] {  1,  1 }
            };

            int[] xor =
            {
                -1,
                 1,
                 1,
                -1
            };

            var kernel = new Polynomial(2, 0.0);

            double[][] augmented = new double[inputs.Length][];
            for (int i = 0; i < inputs.Length; i++)
                augmented[i] = kernel.Transform(inputs[i]);

            SupportVectorMachine machine = new SupportVectorMachine(augmented[0].Length);

            // Create the Least Squares Support Vector Machine teacher
            var learn = new LinearDualCoordinateDescent(machine, augmented, xor);

            // Run the learning algorithm
            double error = learn.Run();

            Assert.AreEqual(0, error);

            int[] output = augmented.Apply(p => Math.Sign(machine.Compute(p)));
            for (int i = 0; i < output.Length; i++)
                Assert.AreEqual(System.Math.Sign(xor[i]), System.Math.Sign(output[i]));
        }
All Usage Examples Of Accord.Statistics.Kernels.Polynomial::Transform