Beispiel #1
0
        public int GetClosestCentroid(Point point)
        {
            int    minIndex    = 0;
            double minDistance = PointMeasures.Distance(point, centroids[minIndex]);

            for (int i = 0; i < centroids.Length; i++)
            {
                double distance = PointMeasures.Distance(point, centroids[i]);
                if (minDistance > distance)
                {
                    minIndex    = i;
                    minDistance = distance;
                }
            }
            return(minIndex);
        }
Beispiel #2
0
        /// <summary>
        ///     Updates the centroids by computing mean of each cluster
        /// </summary>
        private void updateCentroids()
        {
            // 3)
            for (int j = 0; j < k; j++)
            {
                List <int>   cluster        = cluster_indices.ElementAt(j);
                List <Point> cluster_points = new List <Point>();

                for (int i = 0; i < cluster.Count; i++)
                {
                    cluster_points.Add(data[cluster[i]]);
                }
                if (cluster_points.Count > 0)
                {
                    centroids[j] = PointMeasures.Mean(cluster_points); // TODO cluster had no points
                }
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Returns true if all centroids have been converged
        /// </summary>
        /// <param name="currentCentroids"></param>
        /// <param name="previousCentroids"></param>
        /// <returns></returns>
        private bool isConverged(Point[] currentCentroids, Point[] previousCentroids)
        {
            // Array of flags determining convergances
            // flag[i] = 1, means that that centroid has been converged
            int size = currentCentroids.Length;

            int[] flag = new int[size];
            for (int i = 0; i < size; i++)
            {
                flag[i] = 0;
            }

            // compute the distances and check if something was changed
            // according to the tolerance.
            for (int i = 0; i < size; i++)
            {
                double distance = PointMeasures.Distance(currentCentroids[i], previousCentroids[i]);
                if (distance <= distance_tolerance)
                {
                    flag[i] = 1;
                }
            }

            // Check if all centroids are converged
            int flagSum = 0;

            for (int i = 0; i < size; i++)
            {
                flagSum += flag[i];
            }
            if (flagSum == size && current_iter > 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Group points to their closest centroids
        /// </summary>
        private void updateClusters()
        {
            cluster_indices = refreshIndicesList();
            for (int i = 0; i < data.Count; i++)
            {
                Point  point     = data[i];
                int    min_index = 0;
                double min       = PointMeasures.Distance(point, centroids[min_index]);

                // for every centroid
                for (int j = 0; j < k; j++)
                {
                    double distance = PointMeasures.Distance(point, centroids[j]);
                    if (min >= distance)
                    {
                        min       = distance;
                        min_index = j;
                    }
                }
                cluster_indices.ElementAt(min_index).Add(i);
            }
        }