コード例 #1
0
ファイル: VectorDefine.cs プロジェクト: vutiendung/Kmean
 public void AddNewValue(VectorDefine input)
 {
     for (int i = 0; i < Item.Count; i++)
     {
         Item[i] += input.Item[i];
     }
 }
コード例 #2
0
ファイル: VectorDefine.cs プロジェクト: vutiendung/Kmean
 public double getDistance(VectorDefine input)
 {
     double tong = 0;
     int min = Item.Count > input.Item.Count ? input.Item.Count : Item.Count;
     for (int i = 0; i < min; i++)
     {
         tong += (Item[i] - input.Item[i]) * (Item[i] - input.Item[i]);
     }
     return Math.Sqrt(tong);
 }
コード例 #3
0
ファイル: VectorDefine.cs プロジェクト: vutiendung/Kmean
 public bool EqualWithOtherVector(VectorDefine input)
 {
     if (Item.Count != input.Item.Count)
     {
         return false;
     }
     else
     {
         for (int i = 0; i < input.Item.Count; i++)
         {
             if (input.Item[i] != Item[i])
                 return false;
         }
         return true;
     }
 }
コード例 #4
0
ファイル: KMeanCore.cs プロジェクト: vutiendung/Kmean
        /// <summary>
        /// ham su ly chinh cua kmean
        /// </summary>
        /// <param name="k"></param>
        public void ProcessKMnean()
        {
            List<VectorDefine> ListCluster = new List<VectorDefine>();
            MarkUp=new int[ListItem.Count];
            for (int i = 0; i < ListItem.Count; i++)
            {
                MarkUp[i] = -1;
            }

            int step = ListItem.Count / numberCluster;
            for (int i = 0; i < ListItem.Count; )
            {
                ListCluster.Add(ListItem[i]);
                i += step;
            }
            int soLanLap = 0;
            bool change = true;
            while (change)
            {
                soLanLap++;
                if (soLanLap == 100)
                {
                    break;
                }
                for (int i = 0; i < ListItem.Count; i++)
                {
                    double minDistance = ListItem[i].getDistance(ListCluster[0]);
                    int pos=0;
                    for (int j = 1; j < ListCluster.Count; j++)
                    {
                        double tg = ListItem[i].getDistance(ListCluster[j]);
                        if (tg < minDistance)
                        {
                            minDistance = tg;
                            pos = j;
                        }
                    }
                    MarkUp[i] = pos;
                }

                //tinh lai toa do tam
                change = false;
                for (int i = 0; i < ListCluster.Count; i++)
                {
                    VectorDefine OldVector = ListCluster[i];
                    int dem = 0;
                    VectorDefine NewVector = new VectorDefine();

                    for (int j = 0; j < ListItem.Count; j++)
                    {
                        if (j == i)
                        {
                            dem++;
                            NewVector.AddNewValue(ListItem[i]);
                        }
                    }

                    NewVector.Average(dem);

                    if (!NewVector.EqualWithOtherVector(OldVector))
                    {
                        change = true;
                    }
                }
            }
        }
コード例 #5
0
ファイル: KMeanCore.cs プロジェクト: vutiendung/Kmean
 public void ReadDataFromExel(RichTextBox Screen)
 {
     int total = 0;
     DataTable dt;
     dt = MoFileExcel.GetDatasetFromExcel(fileName);
     total = dt.Rows.Count;
     int step = total / 100;
     if (step == 0)
         step = 1;
     if (dt != null)
     {
         Screen.Text += "Loading data\n";
         for (int i = 0; i < dt.Rows.Count;i++ )
         {
             if((int)i*100/total%step==0)
             {
                 Screen.Text += "█";
                 SendKeys.Flush();
             }
             VectorDefine vtTG = new VectorDefine();
             for (int j = 0; j < dt.Columns.Count; j++)
             {
                 try
                 {
                     vtTG.Item.Add(int.Parse(dt.Rows[i][j].ToString()));
                 }
                 catch
                 {
                     break;
                 }
             }
             ListItem.Add(vtTG);
             vtTG = null;
         }
         Screen.Text += "\n";
     }
 }