コード例 #1
0
ファイル: DistanceTests.cs プロジェクト: Novacta/analytics
        public void CompleteDiameterTest()
        {
            // cluster is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = Distance.CompleteDiameter((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "cluster");
            }

            // cluster is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix()[items, ":"];

                var actual = Distance.CompleteDiameter(attributes);

                var expected = 0.6480741;

                Assert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
コード例 #2
0
ファイル: DistanceTests.cs プロジェクト: Novacta/analytics
        public void EuclideanTest()
        {
            // cluster is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = Distance.Euclidean((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "cluster");
            }

            // cluster is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix()[items, ":"];

                var actual = Distance.Euclidean(attributes);

                var expected = DoubleMatrix.Dense(
                    numberOfItems,
                    numberOfItems,
                    new double[] {
                    0.0000000, 0.5385165, 0.5099020, 0.6480741,
                    0.5385165, 0.0000000, 0.3000000, 0.3316625,
                    0.5099020, 0.3000000, 0.0000000, 0.2449490,
                    0.6480741, 0.3316625, 0.2449490, 0.0000000
                },
                    StorageOrder.RowMajor);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
コード例 #3
0
        public void IndexPartitionDaviesBouldinTest()
        {
            // data is null
            {
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.DaviesBouldinIndex(null, partition);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "data");
            }

            // partition is null
            {
                var data = IrisDataSet.GetAttributesAsDoubleMatrix();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.DaviesBouldinIndex(data, null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "partition");
            }

            // The first part contains an invalid index
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                partition[1][0] = 100000;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.DaviesBouldinIndex(data, partition);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_INP_PART_CONTAINS_INVALID_INDEX"),
                    expectedParameterName: "partition");
            }

            // The second part contains an invalid index
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                partition[2][0] = 100000;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.DaviesBouldinIndex(data, partition);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_INP_PART_CONTAINS_INVALID_INDEX"),
                    expectedParameterName: "partition");
            }

            // Valid input
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                var actual = IndexPartition.DaviesBouldinIndex(
                    data, partition);

                // The expected below was obtained in R as follows:
                //
                // library(clv)
                // data(iris)
                // iris.data < -iris[, 1:4]
                // # cluster data
                // agnes.mod < -agnes(iris.data) # create cluster tree
                // v.pred < - as.integer(cutree(agnes.mod, 5)) # "cut" the tree
                // intraclust = c("complete", "average", "centroid")
                // interclust = c("single", "complete", "average", "centroid", "aveToCent", "hausdorff")
                // cls.scatt < -cls.scatt.data(iris.data, v.pred, dist = "euclidean")
                // davies1 <- clv.Davies.Bouldin(cls.scatt, intraclust, interclust)

                // This is davies1[4,3], corresponding to
                // intra = "centroid",
                // inter = "centroid".
                double expected = 0.685838025870551;

                Assert.AreEqual(expected, actual, 1e-3);
            }
        }
コード例 #4
0
        public void IndexPartitionMinimumCentroidTest()
        {
            // data is null
            {
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.MinimumCentroidLinkage(null, partition);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "data");
            }

            // partition is null
            {
                var data = IrisDataSet.GetAttributesAsDoubleMatrix();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.MinimumCentroidLinkage(data, null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "partition");
            }

            // The first part contains an invalid index
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                partition[1][0] = 100000;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.MinimumCentroidLinkage(data, partition);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_INP_PART_CONTAINS_INVALID_INDEX"),
                    expectedParameterName: "partition");
            }

            // The second part contains an invalid index
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                partition[2][0] = 100000;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var actual = IndexPartition.MinimumCentroidLinkage(data, partition);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_INP_PART_CONTAINS_INVALID_INDEX"),
                    expectedParameterName: "partition");
            }

            // Valid input
            {
                var data      = IrisDataSet.GetAttributesAsDoubleMatrix();
                var target    = IrisDataSet.GetClassPredictions();
                var partition = IndexPartition.Create(target);

                var actual = IndexPartition.MinimumCentroidLinkage(
                    data, partition);

                List <double> linkages = new();
                foreach (var leftId in partition.Identifiers)
                {
                    var leftPart = partition[leftId];
                    var left     = data[leftPart, ":"];
                    foreach (var rightId in partition.Identifiers)
                    {
                        if (rightId != leftId)
                        {
                            var rightPart = partition[rightId];
                            var right     = data[rightPart, ":"];
                            linkages.Add(Distance.CentroidLinkage(left, right));
                        }
                    }
                }

                double expected = linkages.Min();

                Assert.AreEqual(expected, actual, 1e-4);
            }
        }
コード例 #5
0
ファイル: DistanceTests.cs プロジェクト: Novacta/analytics
        public void AverageLinkageTest()
        {
            // left is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Distance.AverageLinkage(
                        left: (DoubleMatrix)null,
                        right: DoubleMatrix.Identity(2));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "left");
            }

            // right is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Distance.AverageLinkage(
                        left: DoubleMatrix.Identity(2),
                        right: (DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "right");
            }

            // right and left have not the same number of columns
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Distance.AverageLinkage(
                        left: DoubleMatrix.Identity(2),
                        right: DoubleMatrix.Identity(3));
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                        "left"),
                    expectedParameterName: "right");
            }

            // input is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix();
                var classes       = IrisDataSet.GetClasses();
                var partition     = IndexPartition.Create(classes);
                var left          = attributes[partition["virginica"], ":"][items, ":"];
                var right         = attributes[partition["setosa"], ":"][items, ":"];

                var actual = Distance.AverageLinkage(left, right);

                var expected = 4.932325;

                Assert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }