?? form1.cs.txt
字號:
splitting_criterion.attribute = (string)attribute_list[0];
for (int i = 0; i < attribute_list.Count; i++)
{
string a = (string)attribute_list[i];
double temp = 1;
switch (a)
{
case "age":
temp = Infoage(d);
break;
case "education":
temp = Infoeducation(d);
break;
case "occupation":
temp = Infooccupation(d);
break;
case "sex":
temp = Infosex(d);
break;
case "native_country":
temp = Infonative_country(d);
break;
default:
MessageBox.Show("error");
break;
}
if (temp < infoA)
{
splitting_criterion.attribute = a;
infoA = temp;
}
}
//計算splitting_criterion
{
string[] type = Enum.GetNames(GetEnumType(splitting_criterion.attribute));
for (int i = 0; i < type.Length; i++)
{
ArrayList Dj = CopySmallCollection(d, splitting_criterion.attribute, type[i]);
splitting_criterion.descriptionList.Add(type[i]);
splitting_criterion.splitList.Add(Dj);
}
}
return splitting_criterion;
case attributeSelectionMethodType.GainRate:
//double splitInfo; //平均增益
double splitInfoA; //按A劃分的信息增益
double gainA; //信息增益
double gainRatioA; //信息增益率
//計算splitInfo
/*
splitInfo = SplitInfo(D);
*/
//計算最大的gainRatioA和選擇的屬性
{
gainRatioA = 0;
splitting_criterion.attribute = (string)attribute_list[0];
for (int i = 0; i < attribute_list.Count; i++)
{
string a = (string)attribute_list[i];
double temp = 0;
//計算gainA
gainA = GainA(a, d);
//計算splitInfoA
splitInfoA = SplitInfoA(a, d);
//計算gainRatioA
if (splitInfoA != 0)
{
temp = gainA / splitInfoA;
}
else
{
temp = 1;
}
if (temp > gainRatioA)
{
splitting_criterion.attribute = a;
gainRatioA = temp;
}
}
}
//計算splitting_criterion
{
string[] type = Enum.GetNames(GetEnumType(splitting_criterion.attribute));
for (int i = 0; i < type.Length; i++)
{
ArrayList Dj = CopySmallCollection(d, splitting_criterion.attribute, type[i]);
splitting_criterion.descriptionList.Add(type[i]);
splitting_criterion.splitList.Add(Dj);
}
}
return splitting_criterion;
case attributeSelectionMethodType.Gini:
GiniSplit giniA = new GiniSplit(); //按A劃分的最小gini
//計算最小giniA
{
giniA.gini = 1;
splitting_criterion.attribute = (string)attribute_list[0];
for (int i = 0; i < attribute_list.Count; i++)
{
string a = (string)attribute_list[i];
GiniSplit temp;
//計算gini
temp = GiniA(a, d);
if (temp.gini < giniA.gini)
{
giniA = temp;
}
}
}
//計算splitting_criterion
splitting_criterion = giniA.giniSplitCriterion;
return splitting_criterion;
default:
MessageBox.Show("error");
break;
}
return splitting_criterion;
}
//計算Info
private double Info(ArrayList d)
{
double info = 0;
double countYes = 0;
for (int i = 0; i < d.Count; i++)
{
if (((Item)d[i]).makeover50k == makeover50kType.yes)
countYes++;
}
if (countYes != 0)
info += -(countYes / d.Count) * Math.Log((countYes / d.Count), 2);
if (countYes != d.Count)
info += -((d.Count - countYes) / d.Count) * Math.Log(((d.Count - countYes) / d.Count), 2);
return info;
}
//計算"age"屬性的Info ageType { year0_20, year21_30, year31_40, year41_50, year51_60, yearover60 }
private double Infoage(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 * Info(CopySmallCollection(d, "age", ageType.year0_20))
+ year21_30 * Info(CopySmallCollection(d, "age", ageType.year21_30))
+ year31_40 * Info(CopySmallCollection(d, "age", ageType.year31_40))
+ year41_50 * Info(CopySmallCollection(d, "age", ageType.year41_50))
+ year51_60 * Info(CopySmallCollection(d, "age", ageType.year51_60))
+ yearover60 * Info(CopySmallCollection(d, "age", ageType.yearover60))) / d.Count;
}
//計算"education"屬性的Info
//原有education_numType: 1-16 ,分成6級: edu1_3,edu4_6,edu7_9,edu10_12,edu13_14,edu15_16
private double Infoeducation(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 * Info(CopySmallCollection(d, "education", educationType.edu1_3))
+ edu4_6 * Info(CopySmallCollection(d, "education", educationType.edu4_6))
+ edu7_9 * Info(CopySmallCollection(d, "education", educationType.edu7_9))
+ edu10_12 * Info(CopySmallCollection(d, "education", educationType.edu10_12))
+ edu13_14 * Info(CopySmallCollection(d, "education", educationType.edu13_14))
+ edu15_16 * Info(CopySmallCollection(d, "education", educationType.edu15_16))) / d.Count;
}
//計算"occupation"屬性的Info
//occupationType共14類: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 Infooccupation(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 * Info(CopySmallCollection(d, "occupation", occupationType.Tech_support))
+ Craft_repair * Info(CopySmallCollection(d, "occupation", occupationType.Craft_repair))
+ Other_service * Info(CopySmallCollection(d, "occupation", occupationType.Other_service))
+ Sales * Info(CopySmallCollection(d, "occupation", occupationType.Sales))
+ Exec_managerial * Info(CopySmallCollection(d, "occupation", occupationType.Exec_managerial))
+ Prof_specialty * Info(CopySmallCollection(d, "occupation", occupationType.Prof_specialty))
+ Handlers_cleaners * Info(CopySmallCollection(d, "occupation", occupationType.Handlers_cleaners))
+ Machine_op_inspct * Info(CopySmallCollection(d, "occupation", occupationType.Machine_op_inspct))
+ Adm_clerical * Info(CopySmallCollection(d, "occupation", occupationType.Adm_clerical))
+ Farming_fishing * Info(CopySmallCollection(d, "occupation", occupationType.Farming_fishing))
+ Transport_moving * Info(CopySmallCollection(d, "occupation", occupationType.Transport_moving))
+ Priv_house_serv * Info(CopySmallCollection(d, "occupation", occupationType.Priv_house_serv))
+ Protective_serv * Info(CopySmallCollection(d, "occupation", occupationType.Protective_serv))
+ Armed_Forces * Info(CopySmallCollection(d, "occupation", occupationType.Armed_Forces))) / d.Count;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -