示例#1
0
        /// <summary>
        /// Initialize empty clusters with centroid values based on their distances from each other.
        /// </summary>
        /// <param name="clusterCount">Number of clusters to initialize.</param>
        protected void InitEmptyKmeansClusters(uint clusterCount)
        {
            _clusters = new Dictionary <uint, Cluster>();
            CentroidPoint firstcentroid = new CentroidPoint(PointDim);

            firstcentroid.FillWithRandomValues();                        //first center is random
            _clusters.Add(firstcentroid.ID, new Cluster(firstcentroid));

            for (uint i = 1; i < clusterCount; i++)
            {
                CentroidPoint centroid = new CentroidPoint(PointDim);
                Point         farthest = new Point(PointDim);
                float         maxDist  = 0.0f;
                foreach (ImagePoint point in _points)
                {
                    float dist = 0.0f;
                    foreach (var cluster in _clusters)
                    {
                        dist += Point.SquaredEuclDist(cluster.Value.Centroid, point);
                    }

                    if (dist > maxDist)
                    {
                        maxDist  = dist;
                        farthest = point;
                    }
                }
                centroid.CopyValuesFrom(farthest);
                _clusters.Add(centroid.ID, new Cluster(centroid));
            }
        }
示例#2
0
 /// <summary>
 /// Initialize empty clusters with centroid values randomly generated from their domain.
 /// </summary>
 /// <param name="clusterCount">Number of clusters to initialize.</param>
 protected void InitEmptyClusters(uint clusterCount)
 {
     _clusters = new Dictionary <uint, Cluster>();
     for (uint i = 0; i < clusterCount; i++)
     {
         CentroidPoint centroid = new CentroidPoint(PointDim);
         centroid.FillWithRandomValues();
         _clusters.Add(centroid.ID, new Cluster(centroid));
     }
 }