机器学习实验报告(DOC)Word文件下载.doc
《机器学习实验报告(DOC)Word文件下载.doc》由会员分享,可在线阅读,更多相关《机器学习实验报告(DOC)Word文件下载.doc(19页珍藏版)》请在冰豆网上搜索。
输出:
决策树。
实验结果如下:
SunnyHotHighWeakNo
SunnyHotHighStrongNo
OvercastHotHighWeakYes
RainMildHighWeakYes
RainCoolNormalWeakYes
RainCoolNormalStrongNo
OvercastCoolNormalStrongYes
SunnyMildHighWeakNo
SunnyCoolNormalWeakYes
RainMildNormalWeakYes
SunnyMildNormalStrongYes
OvercastMildHighStrongYes
OvercastHotNormalWeakYes
RainMildHighStrongNo
Outlook
RainWind
StrongNo
WeakYes
OvercastYes
SunnyHumidity
NormalYes
HighNo
三、具体实现:
实现算法如下:
#include<
iostream>
fstream>
math.h>
string>
usingnamespacestd;
#defineROW14
#defineCOL5
#definelog20.69314718055
typedefstructTNode
{
chardata[15];
charweight[15];
TNode*firstchild,*nextsibling;
}*tree;
typedefstructLNode
charOutLook[15];
charTemperature[15];
charHumidity[15];
charWind[15];
charPlayTennis[5];
LNode*next;
}*link;
typedefstructAttrNode
charattributes[15];
//属性
int attr_Num;
//属性的个数
AttrNode*next;
}*Attributes;
char*Examples[ROW][COL]={//"
OverCast"
"
Cool"
High"
Strong"
No"
// "
Rain"
Hot"
Normal"
Yes"
"
Sunny"
Weak"
"
Mild"
};
char*Attributes_kind[4]={"
OutLook"
Temperature"
Humidity"
Wind"
};
int Attr_kind[4]={3,3,2,2};
char*OutLook_kind[3]={"
char*Temperature_kind[3]={"
char*Humidity_kind[2]={"
char*Wind_kind[2]={"
/*inti_Exampple[14][5]={0,0,0,0,1,
0,0,0,1,1,
1,0,0,1,0,
2,1,0,0,0,
2,2,1,0,0,
2,2,1,1,1,
1,2,1,1,0,
0,1,0,0,1,
0,2,1,0,0,
2,1,1,0,0,
0,1,1,1,0,
1,1,1,1,0,
1,1,1,0,0,
2,1,0,0,1
*/
voidtreelists(treeT);
voidInitAttr(Attributes&
attr_link,char*Attributes_kind[],intAttr_kind[]);
voidInitLink(link&
L,char*Examples[][COL]);
voidID3(tree&
T,linkL,linkTarget_Attr,Attributesattr);
voidPN_Num(linkL,int&
positve,int&
negative);
doubleGain(intpositive,intnegative,char*atrribute,linkL,Attributesattr_L);
voidmain()
linkLL,p;
Attributesattr_L,q;
treeT;
T=newTNode;
T->
firstchild=T->
nextsibling=NULL;
strcpy(T->
weight,"
"
);
data,"
attr_L=newAttrNode;
attr_L->
next=NULL;
LL=newLNode;
LL->
//成功建立两个链表
InitLink(LL,Examples);
InitAttr(attr_L,Attributes_kind,Attr_kind);
ID3(T,LL,NULL,attr_L);
cout<
<
决策树以广义表形式输出如下:
endl;
treelists(T);
//以广义表的形式输出树
// cout<
Gain(9,5,"
LL,attr_L)<
}
voidtreelists(treeT)
treep;
if(!
T)
return;
{"
T->
weight<
}"
;
data;
p=T->
firstchild;
if(p)
{
cout<
("
while(p)
{
treelists(p);
p=p->
nextsibling;
if(p)cout<
'
'
}
)"
}
attr_link,char*Attributes_kind[],intAttr_kind[])
Attributesp;
for(inti=0;
i<
4;
i++)
p=newAttrNode;
p->
strcpy(p->
attributes,Attributes_kind[i]);
attr_Num=Attr_kind[i];
p-