示例#1
0
        private void voBtnKMeansPP_Click(object sender, EventArgs e)
        {
            int kiNumClusters;
            List <ClusterCenterPoint> koCList = null;

            try
            {
                if (this.voPList.Count == 0)
                {
                    throw new Exception("No points data exists..");
                }
                kiNumClusters = int.Parse(this.voTbCount.Text);
                KMeans.MDoKMeans(kiNumClusters, ref this.voPList, ref koCList, 0.1, 1000, true);
                MyImageProc.MDrawClusters(this.voPB, this.voPList, 1.0, kiNumClusters);
                this.voTbResult.Text = mComputeAndShowVarianceResults(kiNumClusters);
            }
            catch (Exception koEx)
            {
                MessageBox.Show(koEx.Message);
            }
        }
示例#2
0
        public static int MDoKMeansWithMinVariance(int aiNumClusters,
                                                   ref List <MyPoint> aoPList,
                                                   ref List <ClusterCenterPoint> aoCL,
                                                   double adMaxError, int aiMaxIterations, bool abMinVariance)
        {
            string                    koOut;
            double                    kdStdDev     = 0;
            double                    kdStdDevCopy = 0;
            double                    kdVariance   = 0;
            int                       kiIter       = 0;
            List <MyPoint>            koPListBest  = new List <MyPoint>( );
            List <MyPoint>            koPListCopy;
            List <ClusterCenterPoint> koCListBest = new List <ClusterCenterPoint>( );
            List <ClusterCenterPoint> koCListCopy;

            int[] kiCCount = new int[aiNumClusters];

            if (abMinVariance == true)
            {
                kdStdDev = double.MaxValue;
            }
            else
            {
                kdStdDev = double.MinValue;
            }

            for (int m = 0; m < 20; m++) // pick best i.e., most balanced clustering
            {                            // of 20 attempts at custering
                koPListCopy = new List <MyPoint>( );
                foreach (MyPoint koMP in aoPList)
                {
                    koPListCopy.Add(( MyPoint )koMP.Clone( ));
                }

                koCListCopy = null;
                kiIter     += KMeans.MDoKMeans(aiNumClusters, ref koPListCopy, ref koCListCopy, 0.01, 100, true); // true means do kmeansplusplus
                // ----compute variance of cluster memberships
                for (int i = 0; i < aiNumClusters; i++)
                {
                    kiCCount[i] = 0;
                }

                foreach (MyPoint koMP in koPListCopy)
                {
                    kiCCount[koMP.ViClusterId] += 1;
                }

                kdVariance = 0;
                for (int i = 0; i < aiNumClusters; i++)
                {
                    kdVariance += (kiCCount[i] - (koPListCopy.Count / ( double )aiNumClusters)) *
                                  (kiCCount[i] - (koPListCopy.Count / ( double )aiNumClusters));
                }
                kdStdDevCopy = Math.Sqrt(kdVariance);

                koOut = "";
                for (int n = 0; n < kiCCount.Length; n++)
                {
                    koOut += "Cluster " + n.ToString( ) + " count = " + kiCCount[n].ToString() + "\n";
                }
                //MessageBox.Show( "StdDev = " + koStdDevCopy.ToString( ) + " " + koOut );
                if (abMinVariance == true)
                {
                    if (kdStdDevCopy < kdStdDev) // if it improves, copy data into best
                    {
                        kdStdDev = kdStdDevCopy;
                        koPListBest.Clear( );
                        foreach (MyPoint koMP in koPListCopy)
                        {
                            koPListBest.Add(( MyPoint )koMP.Clone( ));
                        }
                        koCListBest.Clear( );
                        foreach (ClusterCenterPoint koCP in koCListCopy)
                        {
                            koCListBest.Add(( ClusterCenterPoint )koCP.Clone( ));
                        }
                    }
                }
                else
                {
                    if (kdStdDevCopy > kdStdDev) // if it improves, copy data into best
                    {
                        kdStdDev = kdStdDevCopy;
                        koPListBest.Clear( );
                        foreach (MyPoint koMP in koPListCopy)
                        {
                            koPListBest.Add(( MyPoint )koMP.Clone( ));
                        }
                        koCListBest.Clear( );
                        foreach (ClusterCenterPoint koCP in koCListCopy)
                        {
                            koCListBest.Add(( ClusterCenterPoint )koCP.Clone( ));
                        }
                    }
                }
            }

            aoCL    = koCListBest;
            aoPList = koPListBest;

            return(kiIter);
        }