Accord.Tests.MachineLearning.KDTreeTest.FromDataTest C# (CSharp) Method

FromDataTest() private method

private FromDataTest ( ) : void
return void
        public void FromDataTest()
        {
            // This is the same example found in Wikipedia page on
            // k-d trees: http://en.wikipedia.org/wiki/K-d_tree

            // Suppose we have the following set of points:

            double[][] points =
            {
                new double[] { 2, 3 },
                new double[] { 5, 4 },
                new double[] { 9, 6 },
                new double[] { 4, 7 },
                new double[] { 8, 1 },
                new double[] { 7, 2 },
            };


            // To create a tree from a set of points, we use
            KDTree<int> tree = KDTree.FromData<int>(points);

            // Now we can manually navigate the tree
            KDTreeNode<int> node = tree.Root.Left.Right;

            // Or traverse it automatically
            foreach (KDTreeNode<int> n in tree)
            {
                double[] location = n.Position;
                Assert.AreEqual(2, location.Length);
            }

            // Given a query point, we can also query for other
            // points which are near this point within a radius

            double[] query = new double[] { 5, 3 };

            // Locate all nearby points within an Euclidean distance of 1.5
            // (answer should be a single point located at position (5,4))
            var result = tree.Nearest(query, radius: 1.5);

            // We can also use alternate distance functions
            tree.Distance = new Manhattan();

            // And also query for a fixed number of neighbor points
            // (answer should be the points at (5,4), (7,2), (2,3))
            var neighbors = tree.Nearest(query, neighbors: 3);


            Assert.IsTrue(node.IsLeaf);
            Assert.IsFalse(tree.Root.IsLeaf);

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("(5,4)", result[0].Node.ToString());

            Assert.AreEqual(3, neighbors.Count);
            Assert.AreEqual("(5,4)", neighbors[0].Node.ToString());
            Assert.AreEqual("(7,2)", neighbors[1].Node.ToString());
            Assert.AreEqual("(2,3)", neighbors[2].Node.ToString());

            Assert.AreEqual(7, tree.Root.Position[0]);
            Assert.AreEqual(2, tree.Root.Position[1]);
            Assert.AreEqual(0, tree.Root.Axis);

            Assert.AreEqual(5, tree.Root.Left.Position[0]);
            Assert.AreEqual(4, tree.Root.Left.Position[1]);
            Assert.AreEqual(1, tree.Root.Left.Axis);

            Assert.AreEqual(9, tree.Root.Right.Position[0]);
            Assert.AreEqual(6, tree.Root.Right.Position[1]);
            Assert.AreEqual(1, tree.Root.Right.Axis);

            Assert.AreEqual(2, tree.Root.Left.Left.Position[0]);
            Assert.AreEqual(3, tree.Root.Left.Left.Position[1]);
            Assert.AreEqual(0, tree.Root.Left.Left.Axis);

            Assert.AreEqual(4, tree.Root.Left.Right.Position[0]);
            Assert.AreEqual(7, tree.Root.Left.Right.Position[1]);
            Assert.AreEqual(0, tree.Root.Left.Right.Axis);

            Assert.AreEqual(8, tree.Root.Right.Left.Position[0]);
            Assert.AreEqual(1, tree.Root.Right.Left.Position[1]);
            Assert.AreEqual(0, tree.Root.Right.Left.Axis);

            Assert.IsNull(tree.Root.Right.Right);

        }