コード例 #1
0
        /// <inheritdoc />
        public double Evaluate(ClusterSet <TInstance> clusterSet)
        {
            // undefined if only one cluster
            if (clusterSet.Count < 2)
            {
                return(double.NaN);
            }

            // gets clusters' centroids
            var centroids = clusterSet.Select(t => this._centroidFunc(t)).ToList();

            var n           = 0;
            var sum         = 0d;
            var minCentDist = double.MaxValue;

            for (var i = 0; i < clusterSet.Count; i++)
            {
                n += clusterSet[i].Count;
                for (var j = i + 1; j < clusterSet.Count; j++)
                {
                    // updates weighted pairwise distances
                    var betweenClusterDist = this.DissimilarityMetric.Calculate(centroids[i], centroids[j]);
                    minCentDist = Math.Min(minCentDist, betweenClusterDist);
                    sum        += clusterSet[i].Sum(
                        inst1 => clusterSet[j]
                        .Sum(inst2 => this.DissimilarityMetric.Calculate(inst1, inst2) * betweenClusterDist));
                }
            }

            return(2 * sum * minCentDist / (n * (n - 1)));
        }
コード例 #2
0
        /// <inheritdoc />
        public double Evaluate(ClusterSet <TInstance> clusterSet)
        {
            // undefined if only one cluster
            if (clusterSet.Count < 2)
            {
                return(double.NaN);
            }

            // gets clusters' centroids
            var centroids = clusterSet.Select(t => this._centroidFunc(t)).ToList();

            var n   = 0;
            var sum = 0d;

            for (var i = 0; i < clusterSet.Count; i++)
            {
                n += clusterSet[i].Count;

                // updates sum of squared distances to centroids
                foreach (var instance in clusterSet[i])
                {
                    var dist = this.DissimilarityMetric.Calculate(instance, centroids[i]);
                    sum += dist * dist;
                }
            }

            return(Math.Sqrt(sum / n));
        }
コード例 #3
0
        /// <inheritdoc />
        public double Evaluate(ClusterSet <TInstance> clusterSet)
        {
            // undefined if only one cluster
            if (clusterSet.Count < 2)
            {
                return(double.NaN);
            }

            // gets clusters' centroids
            var centroids = clusterSet.Select(t => this._centroidFunc(t)).ToList();

            var n           = 0;
            var sum         = 0d;
            var minCentDist = double.MaxValue;

            for (var i = 0; i < clusterSet.Count; i++)
            {
                n += clusterSet[i].Count;

                // updates sum of distances to centroids
                foreach (var instance in clusterSet[i])
                {
                    var dist = this.DissimilarityMetric.Calculate(instance, centroids[i]);
                    sum += dist * dist;
                }

                // updates min between-cluster distance
                for (var j = i + 1; j < clusterSet.Count; j++)
                {
                    var betweenClusterDist = this.DissimilarityMetric.Calculate(centroids[i], centroids[j]);
                    minCentDist = Math.Min(minCentDist, betweenClusterDist * betweenClusterDist);
                }
            }

            return(-sum / (n * minCentDist));
        }