C45算法建立决策树JAVA练习DOCWord文档下载推荐.docx
《C45算法建立决策树JAVA练习DOCWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C45算法建立决策树JAVA练习DOCWord文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
youth medium
no fair no
youth low
yes fair yes
senior medium
yes
fair yes
yes
excellent yes
high yes fair
yes
no
excellent no
C4.5建立树类
packageC45Test;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Map;
publicclassDecisionTree{
publicTreeNodecreateDT(List<
ArrayList<
String>
>
data,List<
attributeList){
System.out.println("
当前的DATA为"
);
for(inti=0;
i<
data.size();
i++){
ArrayList<
temp=data.get(i);
for(intj=0;
j<
temp.size();
j++){
System.out.print(temp.get(j)+"
"
}
System.out.println();
---------------------------------"
当前的ATTR为"
attributeList.size();
System.out.print(attributeList.get(i)+"
TreeNodenode=newTreeNode();
Stringresult=InfoGain.IsPure(InfoGain.getTarget(data));
if(result!
=null){
node.setNodeName("
leafNode"
node.setTargetFunValue(result);
returnnode;
if(attributeList.size()==0){
}else{
InfoGaingain=newInfoGain(data,attributeList);
doublemaxGain=0.0;
intattrIndex=-1;
doubletempGain=gain.getGainRatio(i);
if(maxGain<
tempGain){
maxGain=tempGain;
attrIndex=i;
选择出的最大增益率属性为:
+attributeList.get(attrIndex));
node.setAttributeValue(attributeList.get(attrIndex));
List<
resultData=null;
Map<
String,Long>
attrvalueMap=gain.getAttributeValue(attrIndex);
for(Map.Entry<
String,Long>
entry:
attrvalueMap.entrySet()){
resultData=gain.getData4Value(entry.getKey(),attrIndex);
TreeNodeleafNode=null;
当前为"
+attributeList.get(attrIndex)+"
的"
+entry.getKey()+"
分支。
"
if(resultData.size()==0){
leafNode=newTreeNode();
leafNode.setNodeName(attributeList.get(attrIndex));
leafNode.setTargetFunValue(result);
leafNode.setAttributeValue(entry.getKey());
for(intj=0;
j<
resultData.size();
j++){
resultData.get(j).remove(attrIndex);
resultAttr=newArrayList<
(attributeList);
resultAttr.remove(attrIndex);
leafNode=createDT(resultData,resultAttr);
node.getChildTreeNode().add(leafNode);
node.getPathName().add(entry.getKey());
classTreeNode{
privateStringattributeValue;
privateList<
TreeNode>
childTreeNode;
pathName;
privateStringtargetFunValue;
privateStringnodeName;
publicTreeNode(StringnodeName){
this.nodeName=nodeName;
this.childTreeNode=newArrayList<
();
this.pathName=newArrayList<
publicTreeNode(){
publicStringgetAttributeValue(){
returnattributeValue;
publicvoidsetAttributeValue(StringattributeValue){
this.attributeValue=attributeValue;
publicList<
getChildTreeNode(){
returnchildTreeNode;
publicvoidsetChildTreeNode(List<
childTreeNode){
this.childTreeNode=childTreeNode;
publicStringgetTargetFunValue(){
returntargetFunValue;
publicvoidsetTargetFunValue(StringtargetFunValue){
this.targetFunValue=targetFunValue;
publicStringgetNodeName(){
returnnodeName;
publicvoidsetNodeName(StringnodeName){
getPathName(){
returnpathName;
publicvoidsetPathName(List<
pathName){
this.pathName=pathName;
}
增益率计算类(取log的时候底用的是e,没用2)
importjava.util.HashMap;
importjava.util.HashSet;
importjava.util.Iterator;
importjava.util.Set;
//C4.5实现
publicclassInfoGain{
data;
attribute;
publicInfoGain(List<
attribute){
this.data=newArrayList<
t=newArrayList<
t.add(temp.get(j));
this.data.add(t);
this.attribute=newArrayList<
St