Inheritance: Accord.Statistics.Kernels.KernelBase, IKernel, IDistance, IReverseDistance, ICloneable, ITransform
コード例 #1
0
ファイル: QuadraticTest.cs プロジェクト: CanerPatir/framework
        public void FunctionTest()
        {
            Quadratic target = new Quadratic(0);

            double[] x = new double[] { 1, 1 };
            double[] y = new double[] { 1, 1 };

            double expected = System.Math.Pow(x.InnerProduct(y), 2);
            double actual = target.Function(x, y);
            Assert.AreEqual(expected, actual);


            x = new double[] { 0.5, 2.0 };
            y = new double[] { 1.3, -0.2 };

            expected = 0.0625;
            actual = target.Function(x, y);

            Assert.AreEqual(expected, actual);


            target = new Quadratic(4.2);

            x = new double[] { 9.4, 22.1 };
            y = new double[] { -6.21, 4 };

            expected = System.Math.Pow(x.InnerProduct(y) + 4.2, 2);
            actual = target.Function(x, y);
            Assert.AreEqual(expected, actual, 0.0001);
        }
コード例 #2
0
ファイル: QuadraticTest.cs プロジェクト: CanerPatir/framework
        public void DistanceTest()
        {
            Quadratic target = new Quadratic(1);

            double[] x = new double[] { 1, 1 };
            double[] y = new double[] { 1, 1 };

            double expected = 0;
            double actual = target.Distance(x, y);
            Assert.AreEqual(expected, actual);


            x = new double[] { 0.5, 2.0 };
            y = new double[] { 1.3, -0.2 };

            expected = 31.8904;
            actual = target.Distance(x, y);

            Assert.AreEqual(expected, actual);


            target = new Quadratic(3);

            x = new double[] { 9.4, 22.1 };
            y = new double[] { -6.21, 4 };

            expected = 337265.44515681011;
            actual = target.Distance(x, y);
            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
ファイル: Polynomial.cs プロジェクト: zadiran/framework
        /// <summary>
        ///   Projects an input point into feature space.
        /// </summary>
        ///
        /// <param name="input">The input point to be projected into feature space.</param>
        ///
        /// <returns>
        ///   The feature space representation of the given <paramref name="input"/> point.
        /// </returns>
        ///
        public double[] Transform(double[] input)
        {
            switch (degree)
            {
            case 1:
                return(Linear.Transform(input, constant));

            case 2:
                return(Quadratic.Transform(input, constant));

            default:
                return(Transform(input, degree, constant));
            }
        }
コード例 #4
0
        public void TransformTest_Quadratic()
        {
            double[][] data = 
            {
                new double[] { 5.1, 3.5, 1.4, 0.2 },
                new double[] { 5.0, 3.6, 1.4, 0.2 },
                new double[] { 4.9, 3.0, 1.4, 0.2 },
                new double[] { 5.8, 4.0, 1.2, 0.2 },
                new double[] { 4.7, 3.2, 1.3, 0.2 },
            };

            var target = new Polynomial(2);
            var quadratic = new Quadratic();

            double[][] expected = data.Apply(quadratic.Transform);
            double[][] actual = data.Apply(target.Transform);

            Assert.IsTrue(expected.IsEqual(actual, 1e-10));
        }
コード例 #5
0
ファイル: QuadraticTest.cs プロジェクト: CanerPatir/framework
        public void ExpandReverseDistanceWithConstantTest()
        {
            Quadratic kernel = new Quadratic(4.2);

            var x = new double[] { 0.5, 2.0 };
            var y = new double[] { 1.3, -0.2 };

            var phi_x = kernel.Transform(x);
            var phi_y = kernel.Transform(y);

            double d = Distance.SquareEuclidean(x, y);
            double phi_d = kernel.ReverseDistance(phi_x, phi_y);

            Assert.AreEqual(5.48, phi_d, 1e-10);
            Assert.AreEqual(5.48, d);

            Assert.IsFalse(double.IsNaN(phi_d));
            Assert.IsFalse(double.IsNaN(d));
        }
コード例 #6
0
ファイル: QuadraticTest.cs プロジェクト: CanerPatir/framework
        public void ExpandDistanceWithConstantTest()
        {
            Quadratic kernel = new Quadratic(4.2);

            var x = new double[] { 0.5, 2.0 };
            var y = new double[] { 1.3, -0.2 };

            var phi_x = kernel.Transform(x);
            var phi_y = kernel.Transform(y);

            double d1 = Distance.SquareEuclidean(phi_x, phi_y);
            double d2 = kernel.Distance(x, y);
            double d3 = Accord.Statistics.Tools.Distance(kernel, x, y);

            Assert.AreEqual(66.9624, d1);
            Assert.AreEqual(66.9624, d2, 1e-10);
            Assert.AreEqual(66.9624, d3, 1e-10);

            Assert.IsFalse(double.IsNaN(d1));
            Assert.IsFalse(double.IsNaN(d2));
            Assert.IsFalse(double.IsNaN(d3));
        }
コード例 #7
0
ファイル: QuadraticTest.cs プロジェクト: CanerPatir/framework
        public void ExpandReverseDistanceTest()
        {
            Quadratic kernel = new Quadratic(0);

            var x = new double[] { 0.5, 2.0 };
            var y = new double[] { 1.3, -0.2 };

            var phi_x = kernel.Transform(x);
            var phi_y = kernel.Transform(y);

            double d = Distance.SquareEuclidean(x, y);
            double phi_d = kernel.ReverseDistance(phi_x, phi_y);

            Assert.AreEqual(phi_d, d);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: CanerPatir/framework
        private static void xor()
        {
            // Create a simple binary XOR
            // classification problem:

            double[][] problem =
            {
                //             a    b    a XOR b
                new double[] { 0,   0,      0    },
                new double[] { 0,   1,      1    },
                new double[] { 1,   0,      1    },
                new double[] { 1,   1,      0    },
            };

            // Get the two first columns as the problem
            // inputs and the last column as the output

            // input columns
            double[][] inputs = problem.GetColumns(0, 1);

            // output column
            int[] outputs = problem.GetColumn(2).ToInt32();

            // Plot the problem on screen
            ScatterplotBox.Show("XOR", inputs, outputs).Hold();


            // However, SVMs expect the output value to be
            // either -1 or +1. As such, we have to convert
            // it so the vector contains { -1, -1, -1, +1 }:
            //
            outputs = outputs.Apply(x => x == 0 ? -1 : 1);


            // Create a new linear-SVM for two inputs (a and b)
            SupportVectorMachine svm = new SupportVectorMachine(inputs: 2);

            // Create a L2-regularized L2-loss support vector classification
            var teacher = new LinearDualCoordinateDescent(svm, inputs, outputs)
            {
                Loss = Loss.L2,
                Complexity = 1000,
                Tolerance = 1e-5
            };

            // Learn the machine
            double error = teacher.Run(computeError: true);

            // Compute the machine's answers for the learned inputs
            int[] answers = inputs.Apply(x => Math.Sign(svm.Compute(x)));

            // Plot the results
            ScatterplotBox.Show("SVM's answer", inputs, answers).Hold();

            // Use an explicit kernel expansion to transform the 
            // non-linear classification problem into a linear one
            //
            // Create a quadratic kernel
            Quadratic quadratic = new Quadratic(constant: 1);
            
            // Project the inptus into a higher dimensionality space
            double[][] expansion = inputs.Apply(quadratic.Transform);


            
            // Create a new linear-SVM for the transformed input space
            svm = new SupportVectorMachine(inputs: expansion[0].Length);

            // Create the same learning algorithm in the expanded input space
            teacher = new LinearDualCoordinateDescent(svm, expansion, outputs)
            {
                Loss = Loss.L2,
                Complexity = 1000,
                Tolerance = 1e-5
            };

            // Learn the machine
            error = teacher.Run(computeError: true); 

            // Compute the machine's answers for the learned inputs
            answers = expansion.Apply(x => Math.Sign(svm.Compute(x)));

            // Plot the results
            ScatterplotBox.Show("SVM's answer", inputs, answers).Hold();
        }