private void button4_Click_1(object sender, EventArgs e)
        {
            List <string> ids             = new List <string>();
            List <Case>   standrize_array = new List <Case>();

            //get ids od cases to delete
            for (int i = 0; i < dataGridView_old.SelectedRows.Count; i++)
            {
                ids.Add(dataGridView_old.SelectedRows[i].Cells["id"].Value.ToString());
            }
            // delete cases from tables
            Db.del_case_id(choice.TableName, ids);
            Db.del_case_id(choice.Tablename_Standardization, ids);
            // re_standrize all tables
            ArrayList array = Db.get_cases_as_arraylist_condition(choice.Tablename_Standardization, choice.CaseName);

            standrize_array = choice.StandardizeData(array);
            Db.del_data_table(choice.Tablename_Standardization);
            standrize_array.Add(choice.Case_Max_Mean);
            standrize_array.Add(choice.Case_Min_SD);
            int num = Convert.ToInt32(choice.Clusters_Num);

            for (int i = 0; i < num; i++) // recalc centroids
            {
                List <Case> cases_cluster = standrize_array.FindAll(
                    delegate(Case ca)
                {
                    return(ca.GetFeature("cluster").GetFeatureValue().ToString() == i.ToString());
                }
                    );
                Case centroid = Kmeans.ReCalcCentroid(cases_cluster, i);
                standrize_array.Add(centroid);
            }
            // add all after restandrize
            Db.insert_data_to_standrize_caseTable_noID(choice.Tablename_Standardization, choice.get_rows_as_string(standrize_array));
            dataGridView_old.DataSource = Db.get_table_contetnt(choice.TableName);
        }
Beispiel #2
0
        private void button4_Click_2(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                dG_data.DataSource     = Db.get_all_orderd(cmbox_case.SelectedValue.ToString(), "cluster");
                dg_clusters.DataSource = Db.get_count_cluster(cmbox_case.SelectedValue.ToString());
                return;
            }
            lstBox_Notes.Items.Add("Normalizing Data using MIn-Max Scaling");

            List <Case> standardize_current_cases = mybestchoice.StandardizeData(mybestchoice.ArrayCases);

            mykp = new KmeansPlus();
            KmeansPlus.PointClusters        myclusters;
            List <KmeansPlus.PointClusters> Allmyclusters = new List <KmeansPlus.PointClusters>();
            // myclusters = mykp.GetKMeansPP(standardize_current_cases,int.Parse( Clusters_num));
            double min_fk = Double.MaxValue;
            int    min_i  = 0;

            for (int j = 0; j < 10; j++)
            {
                myclusters = new KmeansPlus.PointClusters();
                myclusters = mykp.GetKMeansPP_K(standardize_current_cases);
                Allmyclusters.Add(myclusters);
                if (myclusters.Fk < min_fk)
                {
                    min_i = j;
                }
            }
            myclusters = Allmyclusters[min_i];


            lstBox_Notes.Items.Add("Best Clusters Number = " + myclusters.PC.Count.ToString());


            // intialize db grids
            dG_data.Rows.Clear();
            dG_data.Columns.Clear();
            for (int j = 0; j < mybestchoice.Cases[0].GetFeatures().Count; j++)
            {
                Feature feature = (Feature)mybestchoice.Cases[0].GetFeatures()[j];
                dG_data.Columns.Add(feature.GetFeatureName().ToString(), feature.GetFeatureName().ToString());
            }
            dG_data.Rows.Add(mybestchoice.Cases.Count);


            int countcluster = myclusters.PC.Count;

            dg_clusters.Rows.Clear();
            dg_clusters.Columns.Clear();
            dg_clusters.Columns.Add("cluster", "cluster");
            dg_clusters.Columns.Add("count", "count");
            dg_clusters.Rows.Add(countcluster + 1);
            dg_clusters.Rows[0].Cells[0].Value = "عدد العناقيد الكلي";
            dg_clusters.Rows[0].Cells[1].Value = countcluster;
            int t = 1;
            int i = 0;

            foreach (List <Case> cluster in myclusters.PC.Values)
            {
                dg_clusters.Rows[t].Cells[0].Value = t.ToString();
                dg_clusters.Rows[t].Cells[1].Value = cluster.Count;
                foreach (Case c in cluster)
                {
                    Case realcase = mybestchoice.Cases.Find(
                        delegate(Case ca)
                    {
                        return(ca.GetFeature("id").GetFeatureValue() == c.GetFeature("id").GetFeatureValue());
                    }
                        );
                    realcase.GetFeature("cluster").SetFeatureValue(t);
                    c.GetFeature("cluster").SetFeatureValue(t);
                    for (int j = 0; j < c.GetFeatures().Count; j++)
                    {
                        Feature f = (Feature)realcase.GetFeatures()[j];
                        dG_data.Rows[i].Cells[j].Value = f.GetFeatureValue();
                    }
                    i++;
                } // end feautres
                t++;
            }     // end clusters

            dG_data.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            mybestchoice.Clusters = myclusters;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            string Standrize_Type = "";

            if (rbtn_normalize.Checked)
            {
                Standrize_Type = "Q";
            }

            else if (rbtn_minmax.Checked)
            {
                Standrize_Type = "MN";
            }
            else
            {
                Standrize_Type = "none";
            }

            string Kmeans_Method = "";

            if (rbtn_kmeansPlus.Checked)
            {
                Kmeans_Method = "KPP";
            }

            else if (rbtn_simpleKmean.Checked)
            {
                Kmeans_Method = "K";
            }
            else
            {
                Kmeans_Method = "none";
            }

            string Clusters_num = txt_cluster.Text;


            mybestchoice = new BestChoice(ArrayCases, Standrize_Type, Kmeans_Method, Clusters_num);
            standardize_current_cases = mybestchoice.StandardizeData(ArrayCases);


            if (rbtn_simpleKmean.Checked)
            {
                // Kmeans kmeans = new Kmeans(int.Parse(txt_cluster.Text), standardize_current_cases);
                // kmeans.run();

                // Number of Attributes

                List <Cluster> AllClusters = new List <Cluster>();
                AllClusters.Add(null);
                Cluster myc    = new Cluster();
                int     Nd     = standardize_current_cases[0].GetFeatures().Count - 2;//except id , cluster
                Kmeans  kmeans = new Kmeans(1, standardize_current_cases);
                kmeans.run();
                myc.clusters = kmeans.clusters;
                foreach (double sk in kmeans.distances)
                {
                    myc.sk += sk;
                }
                myc.fk = 1;
                myc.k  = 1;
                AllClusters.Add(myc);
                for (int ik = 2; ik <= 5; ik++)//19
                {
                    myc    = new Cluster();
                    kmeans = new Kmeans(ik, standardize_current_cases);
                    kmeans.run();
                    myc.k        = ik;
                    myc.clusters = kmeans.clusters;
                    foreach (double sk in kmeans.distances)
                    {
                        myc.sk += sk;
                    }
                    if (AllClusters[ik - 1].sk == 0)
                    {
                        myc.fk = 1;
                    }
                    else
                    {
                        double ak = 1;
                        if (Nd > 1)
                        {
                            ak = Convert.ToDouble(kmeans.Ak(ik, Nd));
                        }
                        myc.fk = myc.sk / (ak * AllClusters[ik - 1].sk);
                    }
                    AllClusters.Add(myc);
                }


                double min = double.MaxValue;
                int    kk  = 0;
                for (int ik = 1; ik <= 19; ik++)
                {
                    if (AllClusters[ik].fk < min)
                    {
                        kk = AllClusters[ik].k; kmeans.clusters = AllClusters[ik].clusters; min = AllClusters[ik].fk;
                    }
                }
                watch.Stop();
                textBox1.Text    = min.ToString();
                txt_cluster.Text = kk.ToString();
                var elapsedMs = watch.ElapsedMilliseconds;
                label3.Text = "with clustring time  " + elapsedMs.ToString();
                int t = 0;
                int i = 0;
                dataGridView1.Rows.Clear();
                dataGridView1.Columns.Clear();
                for (int j = 0; j < ListCases[0].GetFeatures().Count; j++)
                {
                    Feature feature = (Feature)ListCases[0].GetFeatures()[j];
                    dataGridView1.Columns.Add(feature.GetFeatureName().ToString(), feature.GetFeatureName().ToString());
                }

                dataGridView2.Rows.Clear();
                dataGridView2.Columns.Clear();
                dataGridView1.Rows.Add(ArrayCases.Count);


                dataGridView2.Columns.Add("cluster", "cluster");
                dataGridView2.Columns.Add("count", "count");
                dataGridView2.Rows.Add(kmeans.clusters.Count);

                foreach (List <Case> list in kmeans.clusters) // for each cluster, define a new centroid
                {
                    kmeans.centroids[t].GetFeature("id").SetFeatureValue(t);
                    standardize_current_cases.Add(kmeans.centroids[t]);
                    dataGridView2.Rows[t].Cells[0].Value = t.ToString();
                    dataGridView2.Rows[t].Cells[1].Value = list.Count;
                    mybestchoice.Cost_Function          += kmeans.distances[t];
                    foreach (Case c in list) // Determine the poit on clusters
                    {
                        Case realcase = ListCases.Find(
                            delegate(Case ca)
                        {
                            return(ca.GetFeature("id").GetFeatureValue() == c.GetFeature("id").GetFeatureValue());
                        }
                            );
                        realcase.GetFeature("cluster").SetFeatureValue(t);
                        c.GetFeature("cluster").SetFeatureValue(t);
                        for (int j = 0; j < c.GetFeatures().Count; j++)
                        {
                            Feature f = (Feature)realcase.GetFeatures()[j];
                            dataGridView1.Rows[i].Cells[j].Value = f.GetFeatureValue();
                        }
                        i++;
                    }
                    t++;
                }
            }
            else if (rbtn_kmeansPlus.Checked)
            {
                KmeansPlus mykp = new KmeansPlus();
                KmeansPlus.PointClusters        myclusters;
                List <KmeansPlus.PointClusters> Allmyclusters = new List <KmeansPlus.PointClusters>();
                // myclusters = mykp.GetKMeansPP(standardize_current_cases,int.Parse( Clusters_num));
                double min_fk = Double.MaxValue;
                int    min_i  = 0;
                for (int j = 0; j < 100; j++)
                {
                    myclusters = new KmeansPlus.PointClusters();
                    myclusters = mykp.GetKMeansPP_K(standardize_current_cases);
                    Allmyclusters.Add(myclusters);
                    if (myclusters.Fk < min_fk)
                    {
                        min_i = j;
                    }
                }
                myclusters = Allmyclusters[min_i];
                // KmeansPlus mykp = new KmeansPlus();
                // KmeansPlus.PointClusters myclusters =new KmeansPlus.PointClusters();;
                //  myclusters = mykp.GetKMeansPP_K(standardize_current_cases);
                textBox1.Text    = myclusters.Fk.ToString();
                txt_cluster.Text = myclusters.PC.Count.ToString();
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                label3.Text = "with clustring time  " + elapsedMs.ToString();

                foreach (Case centroid in myclusters.PC.Keys)
                {
                    standardize_current_cases.Add(centroid);
                }


                // intialize db grids

                int t            = 0;
                int i            = 0;
                int countcluster = myclusters.PC.Count;
                dataGridView1.Rows.Clear();
                dataGridView1.Columns.Clear();
                for (int j = 0; j < ListCases[0].GetFeatures().Count; j++)
                {
                    Feature feature = (Feature)ListCases[0].GetFeatures()[j];
                    dataGridView1.Columns.Add(feature.GetFeatureName().ToString(), feature.GetFeatureName().ToString());
                }

                dataGridView2.Rows.Clear();
                dataGridView2.Columns.Clear();
                dataGridView1.Rows.Add(ArrayCases.Count);


                dataGridView2.Columns.Add("cluster", "cluster");
                dataGridView2.Columns.Add("count", "count");
                dataGridView2.Rows.Add(countcluster);

                foreach (List <Case> cluster in myclusters.PC.Values)
                {
                    dataGridView2.Rows[t].Cells[0].Value = t.ToString();
                    dataGridView2.Rows[t].Cells[1].Value = cluster.Count;
                    foreach (Case c in cluster)
                    {
                        Case realcase = ListCases.Find(
                            delegate(Case ca)
                        {
                            return(ca.GetFeature("id").GetFeatureValue() == c.GetFeature("id").GetFeatureValue());
                        }
                            );
                        realcase.GetFeature("cluster").SetFeatureValue(t);
                        c.GetFeature("cluster").SetFeatureValue(t);
                        for (int j = 0; j < c.GetFeatures().Count; j++)
                        {
                            Feature f = (Feature)realcase.GetFeatures()[j];
                            dataGridView1.Rows[i].Cells[j].Value = f.GetFeatureValue();
                        }
                        i++;
                    } // end feautres
                    t++;
                }     // end clusters
            }         // end if
            else
            if (rbtn_nonekmeans.Checked)
            {
                dataGridView1.DataSource = Db.get_table_contetnt(mybestchoice.TableName);
            }

            // for sorting
            comboBox1.Items.Clear();
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                comboBox1.Items.Add(dataGridView1.Columns[i].HeaderText);
            }
            comboBox1.Text = "id";


            dataGridView1.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells;
        }