Distance() public method

Computes the squared distance in feature space between two points given in input space.
public Distance ( double x, double y ) : double
x double Vector x in input space.
y double Vector y in input space.
return double
示例#1
0
        public void DistanceTest()
        {
            Polynomial target = new Polynomial(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 = 5.48;
            actual = target.Distance(x, y);

            Assert.AreEqual(expected, actual);


            target = new Polynomial(3);

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

            expected = 192981940.60611719;
            actual = target.Distance(x, y);
            Assert.AreEqual(expected, actual);
        }
        public void DistanceTest()
        {
            Polynomial dense = new Polynomial(3);
            SparsePolynomial target = new SparsePolynomial(3);

            double[] sx = { 1, -0.555556, 2, +0.250000, 3, -0.864407, 4, -0.916667 };
            double[] sy = { 1, -0.666667, 2, -0.166667, 3, -0.864407, 4, -0.916667 };
            double[] sz = { 1, -0.944444, 3, -0.898305, 4, -0.916667 };

            double[] dx = { -0.555556, +0.250000, -0.864407, -0.916667 };
            double[] dy = { -0.666667, -0.166667, -0.864407, -0.916667 };
            double[] dz = { -0.944444, +0.000000, -0.898305, -0.916667 };

            double expected, actual;

            expected = dense.Distance(dx, dy);
            actual = target.Distance(sx, sy);
            Assert.AreEqual(expected, actual, 1e-10);

            expected = dense.Distance(dx, dz);
            actual = target.Distance(sx, sz);
            Assert.AreEqual(expected, actual, 1e-10);

            expected = dense.Distance(dy, dz);
            actual = target.Distance(sy, sz);
            Assert.AreEqual(expected, actual, 1e-10);
        }
示例#3
0
        public void ExpandDistanceTest()
        {
            for (int i = 1; i <= 10; i++)
            {
                Polynomial kernel = new Polynomial(i, 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 d1 = Distance.SquareEuclidean(phi_x, phi_y);
                double d2 = kernel.Distance(x, y);
                double d3 = Accord.Statistics.Tools.Distance(kernel, x, y);

                Assert.AreEqual(d1, d2, 1e-4);
                Assert.AreEqual(d1, d3, 1e-4);
                Assert.IsFalse(double.IsNaN(d1));
                Assert.IsFalse(double.IsNaN(d2));
                Assert.IsFalse(double.IsNaN(d3));
            }
        }