예제 #1
0
        /// <summary>
        /// Generate random points clumped into individual, well-separated, Gaussian clusters with optional uniform noise added.
        ///
        /// </summary>
        /// <returns>Points that are grouped into clusters and stored in a Classification.</returns>
        public Classification <UnsignedPoint, string> MakeClusters()
        {
            var clusters = new Classification <UnsignedPoint, string>();

            r = new FastRandom();
            //var z = new ZigguratGaussianSampler();
            var farthestDistanceFromClusterCenter = 0.0;

            var minDistance     = EllipsoidalGenerator.MinimumSeparation(MaxDistanceStdDev, Dimensions);
            var centerGenerator = new DiffuseGenerator(Dimensions, minDistance)
            {
                // Keep the centers of the clusters away from the edge, so that points do not go out of bounds and have their coordinates truncated.
                Minimum = MaxDistanceStdDev,
                Maximum = MaxCoordinate - MaxDistanceStdDev
            };
            var iCluster       = 0;
            var clusterCenters = new Dictionary <string, UnsignedPoint> ();

            foreach (var clusterCenter in centerGenerator.Take(ClusterCount).Where(ctr => ctr != null))
            {
                var centerPoint = new UnsignedPoint(clusterCenter);
                // The cluster size may be random, or come from ClusterSizes.
                int clusterSize;
                if (ClusterSizes.Length > 0)
                {
                    clusterSize = ClusterSizes[iCluster % ClusterSizes.Length];
                }
                else
                {
                    clusterSize = r.Next(MinClusterSize, MaxClusterSize);
                }
                var pointGenerator = new EllipsoidalGenerator(clusterCenter, RandomDoubles(Dimensions, MinDistanceStdDev, MaxDistanceStdDev, r), Dimensions);
                var clusterId      = iCluster.ToString();
                foreach (var iPoint in Enumerable.Range(1, clusterSize))
                {
                    UnsignedPoint p;
                    clusters.Add(
                        p = new UnsignedPoint(pointGenerator.Generate(new int[Dimensions])),
                        clusterId
                        );
                    var distance = Math.Sqrt(centerPoint.Measure(p));
                    farthestDistanceFromClusterCenter = Math.Max(farthestDistanceFromClusterCenter, distance);
                }
                clusterCenters[clusterId] = centerPoint;
                iCluster++;
            }
            AddNoise((int)Math.Floor(clusters.NumPoints * NoisePercentage / 100), clusterCenters, clusters);
            Debug.WriteLine("Test data: Farthest Distance from center = {0:N2}. Minimum Distance Permitted between Clusters = {1:N2}. Max Standard Deviation = {2}",
                            farthestDistanceFromClusterCenter,
                            minDistance,
                            MaxDistanceStdDev
                            );
            return(clusters);
        }
예제 #2
0
        /// <summary>
        /// Generate random points clumped into individual, well-separated, chains of Gaussian clusters.
        /// Each chain consists of individiual Gaussian clusters that overlap.
        /// </summary>
        /// <returns>Points that are grouped into clusters and stored in a Classification.</returns>
        public Classification <UnsignedPoint, string> MakeChains(int chainLength)
        {
            var clusters = new Classification <UnsignedPoint, string>();

            r = new FastRandom();

            var minDistance     = EllipsoidalGenerator.MinimumSeparation(MaxDistanceStdDev, Dimensions);
            var centerGenerator = new ChainGenerator(Dimensions, minDistance)
            {
                // Keep the centers of the clusters away from the edge, so that points do not go out of bounds and have their coordinates truncated.
                Minimum = MaxDistanceStdDev,
                Maximum = MaxCoordinate - MaxDistanceStdDev
            };
            var segmentLength = (int)(MinDistanceStdDev * Math.Sqrt(Dimensions) / 3);
            var iCluster      = 0;

            foreach (var chain in centerGenerator.Chains(chainLength, segmentLength).Take(ClusterCount).Where(chain => chain.Any()))
            {
                var centerPoints = chain.Select(center => new UnsignedPoint(center)).ToList();
                // The cluster size may be random, or come from ClusterSizes.
                int clusterSize;
                if (ClusterSizes.Length > 0)
                {
                    clusterSize = ClusterSizes[iCluster % ClusterSizes.Length];
                }
                else
                {
                    clusterSize = r.Next(MinClusterSize, MaxClusterSize);
                }
                // Having decided on an overall cluster size, each segment gets an even number of points.
                var segmentSize = clusterSize / chainLength;
                var clusterId   = iCluster.ToString();
                // Each point generator is for a different segment of a chain.
                foreach (var pointGenerator in chain
                         .Select(segmentCenter =>
                                 new EllipsoidalGenerator(segmentCenter, RandomDoubles(Dimensions, MinDistanceStdDev, MaxDistanceStdDev, r), Dimensions))
                         )
                {
                    foreach (var iPoint in Enumerable.Range(1, segmentSize))
                    {
                        clusters.Add(
                            new UnsignedPoint(pointGenerator.Generate(new int[Dimensions])),
                            clusterId
                            );
                    }
                }
                iCluster++;
            }
            return(clusters);
        }
예제 #3
0
        /// <summary>
        /// Create two random clusters that may be separated from one another by enough distance
        /// that they do not overlap, or be partly overlapping, or fully overlapping.
        ///
        /// NOTE: This type of setup is to test divisive clustering, that divides two partly mixed gaussians.
        /// </summary>
        /// <param name="overlapPercent">A number from zero to 100.
        /// If zero, the clusters do not overlap at all.
        /// If fifty, then the clusters partly overlap.
        /// If 100, the clusters have the same center, so are indistinguishable.</param>
        /// <returns>The two clusters.</returns>
        public Classification <UnsignedPoint, string> TwoClusters(double overlapPercent)
        {
            var clusters = new Classification <UnsignedPoint, string>();

            r = new FastRandom();
            var farthestDistanceFromClusterCenter = 0.0;

            var minDistance     = EllipsoidalGenerator.MinimumSeparation(MaxDistanceStdDev, Dimensions);
            var centerGenerator = new DiffuseGenerator(Dimensions, minDistance)
            {
                // Keep the centers of the clusters away from the edge, so that points do not go out of bounds and have their coordinates truncated.
                // Keep the maximum coordinate farther away, because we will pick the second point by shifting one coordinate
                // in the higher direction.
                Minimum = MaxDistanceStdDev,
                Maximum = MaxCoordinate - MaxDistanceStdDev - (int)minDistance
            };
            var iCluster       = 0;
            var clusterCenter1 = centerGenerator.Take(1).FirstOrDefault();
            var clusterCenter2 = (int[])clusterCenter1.Clone();

            clusterCenter2[0] += (int)(minDistance * (100.0 - overlapPercent) / 100.0);
            var centers = new[] { clusterCenter1, clusterCenter2 };

            foreach (var clusterCenter in centers)
            {
                var centerPoint    = new UnsignedPoint(clusterCenter);
                var clusterSize    = r.Next(MinClusterSize, MaxClusterSize);
                var pointGenerator = new EllipsoidalGenerator(clusterCenter, RandomDoubles(Dimensions, MinDistanceStdDev, MaxDistanceStdDev, r), Dimensions);
                var clusterId      = iCluster.ToString();
                foreach (var iPoint in Enumerable.Range(1, clusterSize))
                {
                    UnsignedPoint p;
                    clusters.Add(
                        p = new UnsignedPoint(pointGenerator.Generate(new int[Dimensions])),
                        clusterId
                        );
                    var distance = Math.Sqrt(centerPoint.Measure(p));
                    farthestDistanceFromClusterCenter = Math.Max(farthestDistanceFromClusterCenter, distance);
                }
                iCluster++;
            }
            //TODO: Go back and recluster the points. Put each point into the cluster whose centroid
            //      it is nearest. Thus, if two clusters partly overlap, the points from one will be pushed into the other.
            return(clusters);
        }