⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 基于决策树和贝叶斯的预测分析器
💻 CS
📖 第 1 页 / 共 5 页
字号:


        //计算"sex"属性的Info
        
        private double Infosex(ArrayList d)
        {
            double Male = 0, Female = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).sex)
                {
                    case sexType.Male:
                        Male++;
                        break;
                    case sexType.Female:
                        Female++;
                        break;
                    default:
                        MessageBox.Show("无此性别");
                        break;
                }
            }
            return ( Male  * Info(CopySmallCollection(d, "sex", sexType.Male)) 
                   + Female  * Info(CopySmallCollection(d, "sex", sexType.Female)) ) / d.Count;
        }

        //计算"native_country"属性的Info
        //native_country分成5类developNO1,developNO2,developNO3,developNO4,developNO5
        private double Infonative_country(ArrayList d)
        {
            double developNO1 = 0, developNO2 = 0, developNO3 = 0, developNO4 = 0, developNO5 = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).native_country)
                {
                    case native_countryType.developNO1:
                        developNO1++;
                        break;
                    case native_countryType.developNO2:
                        developNO2++;
                        break;
                    case native_countryType.developNO3:
                        developNO3++;
                        break;
                    case native_countryType.developNO4:
                        developNO4++;
                        break;
                    case native_countryType.developNO5:
                        developNO5++;
                        break;
                    default:
                        MessageBox.Show("无此国家类别");
                        break;
                }
            }
            return ( developNO1  * Info(CopySmallCollection(d, "native_country", native_countryType.developNO1)) 
                   + developNO2  * Info(CopySmallCollection(d, "native_country", native_countryType.developNO2)) 
                   + developNO3  * Info(CopySmallCollection(d, "native_country", native_countryType.developNO3))
                   + developNO4  * Info(CopySmallCollection(d, "native_country", native_countryType.developNO4)) 
                   + developNO5  * Info(CopySmallCollection(d, "native_country", native_countryType.developNO5)) ) / d.Count;
        }

        //按属性A计算Gain
        private double GainA(string a, ArrayList d)
        {
            double info;           //按类划分的期望信息
            double infoA;          //按A划分的期望信息
            double gainA;           //信息增益


            //计算Info
            info = Info(d);

            //计算最小的InfoA和选择的属性
            {
                infoA = 1;
                switch (a)
                {
                    case "age":
                        infoA = Infoage(d);
                        break;
                    case "education":
                        infoA = Infoeducation(d);
                        break;
                    case "occupation":
                        infoA = Infooccupation(d);
                        break;
                    case "sex":
                        infoA = Infosex(d);
                        break;
                    case "native_country":
                        infoA = Infonative_country(d);
                        break;
                    default:
                        MessageBox.Show("无此属性");
                        break;
                }
            }

            //计算GainA
            gainA = info - infoA;

            return gainA;
        }

        //计算"age"属性的SplitInfo    
        //ageType { year0_20, year21_30, year31_40, year41_50, year51_60, yearover60 }
        private double SplitInfoage(ArrayList d)
        {
            double year0_20 = 0, year21_30 = 0, year31_40 = 0, year41_50 = 0, year51_60 = 0, yearover60 = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).age)
                {
                    case ageType.year0_20:
                        year0_20++;
                        break;
                    case ageType.year21_30:
                        year21_30++;
                        break;
                    case ageType.year31_40:
                        year31_40++;
                        break;
                    case ageType.year41_50:
                        year41_50++;
                        break;
                    case ageType.year51_60:
                        year51_60++;
                        break;
                    case ageType.yearover60:
                        yearover60++;
                        break;

                    default:
                        MessageBox.Show("无此年龄类别");
                        break;
                }
            }
            return ( - year0_20 * Math.Log(year0_20 / d.Count, 2) 
                     - year21_30 * Math.Log(year21_30 / d.Count, 2)
                     - year31_40 * Math.Log(year31_40 / d.Count, 2)
                     - year41_50 * Math.Log(year41_50 / d.Count, 2) 
                     - year51_60 * Math.Log(year51_60 / d.Count, 2)
                     - yearover60 * Math.Log(yearover60 / d.Count, 2) ) / d.Count ;
        }



        //计算"education"属性的SplitInfo
        //enum educationType { edu1_3, edu4_6, edu7_9, edu10_12, edu13_14, edu15_16 };
        private double SplitInfoeducation(ArrayList d)
        {
            double edu1_3 = 0, edu4_6 = 0, edu7_9 = 0, edu10_12 = 0, edu13_14 = 0, edu15_16 = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).education)
                {
                    case educationType.edu1_3:
                        edu1_3++;
                        break;
                    case educationType.edu4_6:
                        edu4_6++;
                        break;
                    case educationType.edu7_9:
                        edu7_9++;
                        break;
                    case educationType.edu10_12:
                        edu10_12++;
                        break;
                    case educationType.edu13_14:
                        edu13_14++;
                        break;
                    case educationType.edu15_16:
                        edu15_16++;
                        break;
                    default:
                        MessageBox.Show("无此教育类别");
                        break;
                }
            }
            
            return ( - edu1_3 * Math.Log(edu1_3 / d.Count, 2) - edu4_6 * Math.Log(edu4_6 / d.Count, 2)
                     - edu7_9 * Math.Log(edu7_9 / d.Count, 2) - edu10_12 * Math.Log(edu10_12 / d.Count, 2)
                     - edu13_14 * Math.Log(edu13_14 / d.Count, 2) - edu15_16 * Math.Log(edu15_16 / d.Count, 2) ) / d.Count;
        }

        //计算"occupation"属性的SplitInfo
        /* enum occupationType
         {
             Tech_support, Craft_repair, Other_service, Sales, Exec_managerial, Prof_specialty,
             Handlers_cleaners, Machine_op_inspct, Adm_clerical, Farming_fishing, Transport_moving,
             Priv_house_serv, Protective_serv, Armed_Forces
         };*/
        private double SplitInfooccupation(ArrayList d)
        {
            double Tech_support = 0, Craft_repair = 0, Other_service = 0, Sales = 0, Exec_managerial = 0;
            double Prof_specialty = 0, Handlers_cleaners = 0, Machine_op_inspct = 0, Adm_clerical = 0, Farming_fishing = 0;
            double Transport_moving = 0, Priv_house_serv = 0, Protective_serv = 0, Armed_Forces = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).occupation)
                {
                    case occupationType.Tech_support:
                        Tech_support++;
                        break;
                    case occupationType.Craft_repair:
                        Craft_repair++;
                        break;
                    case occupationType.Other_service:
                        Other_service++;
                        break;
                    case occupationType.Sales:
                        Sales++;
                        break;
                    case occupationType.Exec_managerial:
                        Exec_managerial++;
                        break;
                    case occupationType.Prof_specialty:
                        Prof_specialty++;
                        break;
                    case occupationType.Handlers_cleaners:
                        Handlers_cleaners++;
                        break;
                    case occupationType.Machine_op_inspct:
                        Machine_op_inspct++;
                        break;
                    case occupationType.Adm_clerical:
                        Adm_clerical++;
                        break;
                    case occupationType.Farming_fishing:
                        Farming_fishing++;
                        break;
                    case occupationType.Transport_moving:
                        Transport_moving++;
                        break;
                    case occupationType.Priv_house_serv:
                        Priv_house_serv++;
                        break;
                    case occupationType.Protective_serv:
                        Protective_serv++;
                        break;
                    case occupationType.Armed_Forces:
                        Armed_Forces++;
                        break;
                    default:
                        MessageBox.Show("无此职业类别");
                        break;
                }
            }
            return (  - Tech_support * Math.Log(Tech_support / d.Count, 2)
                      - Craft_repair * Math.Log(Craft_repair / d.Count, 2)
                      - Other_service * Math.Log(Other_service / d.Count, 2)
                      - Sales * Math.Log(Sales / d.Count, 2)
                      - Exec_managerial * Math.Log(Exec_managerial / d.Count, 2)
                      - Prof_specialty * Math.Log(Prof_specialty / d.Count, 2)
                      - Handlers_cleaners * Math.Log(Handlers_cleaners / d.Count, 2)
                      - Machine_op_inspct * Math.Log(Machine_op_inspct / d.Count, 2)
                      - Adm_clerical * Math.Log(Adm_clerical / d.Count, 2)
                      - Farming_fishing * Math.Log(Farming_fishing / d.Count, 2)
                      - Transport_moving * Math.Log(Transport_moving / d.Count, 2)
                      - Priv_house_serv * Math.Log(Priv_house_serv / d.Count, 2)
                      - Protective_serv * Math.Log(Protective_serv / d.Count, 2)
                      - Armed_Forces * Math.Log(Armed_Forces / d.Count, 2)  ) / d.Count;
        }

        //计算"sex"属性的SplitInfo        sexType { Male, Female }
        private double SplitInfosex(ArrayList d)
        {
            double Male = 0, Female = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).sex)
                {
                    case sexType.Male:
                        Male++;
                        break;
                    case sexType.Female:
                        Female++;
                        break;
                    default:
                        MessageBox.Show("error");
                        break;
                }
            }
            return ( - Male * Math.Log(Male / d.Count, 2) - Female * Math.Log(Female / d.Count, 2) ) / d.Count;
        }

        //计算"native_country"属性的SplitInfo
        private double SplitInfonative_country(ArrayList d)
        {
            double developNO1 = 0, developNO2 = 0, developNO3 = 0, developNO4 = 0, developNO5 = 0;
            for (int i = 0; i < d.Count; i++)
            {
                switch (((Item)d[i]).native_country)
                {
                    case native_countryType.developNO1:
                        developNO1++;
                        break;
                    case native_countryType.developNO2:
                        developNO2++;
                        break;
                    case native_countryType.developNO3:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -