Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
            }
        }