C45算法建立决策树JAVA练习DOC.docx

上传人:b****2 文档编号:7734 上传时间:2022-09-30 格式:DOCX 页数:15 大小:15.31KB
下载 相关 举报
C45算法建立决策树JAVA练习DOC.docx_第1页
第1页 / 共15页
C45算法建立决策树JAVA练习DOC.docx_第2页
第2页 / 共15页
C45算法建立决策树JAVA练习DOC.docx_第3页
第3页 / 共15页
C45算法建立决策树JAVA练习DOC.docx_第4页
第4页 / 共15页
C45算法建立决策树JAVA练习DOC.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C45算法建立决策树JAVA练习DOC.docx

《C45算法建立决策树JAVA练习DOC.docx》由会员分享,可在线阅读,更多相关《C45算法建立决策树JAVA练习DOC.docx(15页珍藏版)》请在冰豆网上搜索。

C45算法建立决策树JAVA练习DOC.docx

C45算法建立决策树JAVA练习DOC

【决策树】—C4.5算法建立决策树JAVA练习

以下程序是我练习写的,不一定正确也没做存储优化。

有问题请留言交流。

转载请挂连接。

当前的属性为:

ageincomestudentcredit_rating

当前的数据集为(最后一列是TARGET_VALUE):

---------------------------------

youth    high  no fair    no 

youth    high  no excellent no 

middle_aged  high  no fair    yes 

senior    low  yes fair    yes 

senior    low  yes excellent no 

middle_aged  low  yes excellent yes 

youth    medium no fair    no 

youth    low   yes fair    yes 

senior    medium yes   fair    yes 

youth    medium  yes   excellent yes 

middle_aged  high  yes fair       yes 

senior    medium no    excellent no 

---------------------------------

C4.5建立树类

packageC45Test;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.Map;

publicclassDecisionTree{

publicTreeNodecreateDT(List>data,ListattributeList){

System.out.println("当前的DATA为");

for(inti=0;i

ArrayListtemp=data.get(i);

for(intj=0;j

System.out.print(temp.get(j)+"");

}

System.out.println();

}

System.out.println("---------------------------------");

System.out.println("当前的ATTR为");

for(inti=0;i

System.out.print(attributeList.get(i)+"");

}

System.out.println();

System.out.println("---------------------------------");

TreeNodenode=newTreeNode();

Stringresult=InfoGain.IsPure(InfoGain.getTarget(data));

if(result!

=null){

node.setNodeName("leafNode");

node.setTargetFunValue(result);

returnnode;

}

if(attributeList.size()==0){

node.setTargetFunValue(result);

returnnode;

}else{

InfoGaingain=newInfoGain(data,attributeList);

doublemaxGain=0.0;

intattrIndex=-1;

for(inti=0;i

doubletempGain=gain.getGainRatio(i);

if(maxGain

maxGain=tempGain;

attrIndex=i;

}

}

System.out.println("选择出的最大增益率属性为:

"+attributeList.get(attrIndex));

node.setAttributeValue(attributeList.get(attrIndex));

List>resultData=null;

MapattrvalueMap=gain.getAttributeValue(attrIndex);

for(Map.Entryentry:

attrvalueMap.entrySet()){

resultData=gain.getData4Value(entry.getKey(),attrIndex);

TreeNodeleafNode=null;

System.out.println("当前为"+attributeList.get(attrIndex)+"的"+entry.getKey()+"分支。

");

if(resultData.size()==0){

leafNode=newTreeNode();

leafNode.setNodeName(attributeList.get(attrIndex));

leafNode.setTargetFunValue(result);

leafNode.setAttributeValue(entry.getKey());

}else{

for(intj=0;j

resultData.get(j).remove(attrIndex);

}

ArrayListresultAttr=newArrayList(attributeList);

resultAttr.remove(attrIndex);

leafNode=createDT(resultData,resultAttr);

}

node.getChildTreeNode().add(leafNode);

node.getPathName().add(entry.getKey());

}

}

returnnode;

}

classTreeNode{

privateStringattributeValue;

privateListchildTreeNode;

privateListpathName;

privateStringtargetFunValue;

privateStringnodeName;

publicTreeNode(StringnodeName){

this.nodeName=nodeName;

this.childTreeNode=newArrayList();

this.pathName=newArrayList();

}

publicTreeNode(){

this.childTreeNode=newArrayList();

this.pathName=newArrayList();

}

publicStringgetAttributeValue(){

returnattributeValue;

}

publicvoidsetAttributeValue(StringattributeValue){

this.attributeValue=attributeValue;

}

publicListgetChildTreeNode(){

returnchildTreeNode;

}

publicvoidsetChildTreeNode(ListchildTreeNode){

this.childTreeNode=childTreeNode;

}

publicStringgetTargetFunValue(){

returntargetFunValue;

}

publicvoidsetTargetFunValue(StringtargetFunValue){

this.targetFunValue=targetFunValue;

}

publicStringgetNodeName(){

returnnodeName;

}

publicvoidsetNodeName(StringnodeName){

this.nodeName=nodeName;

}

publicListgetPathName(){

returnpathName;

}

publicvoidsetPathName(ListpathName){

this.pathName=pathName;

}

}

}

 

 

增益率计算类(取log的时候底用的是e,没用2)

packageC45Test;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.HashSet;

importjava.util.Iterator;

importjava.util.List;

importjava.util.Map;

importjava.util.Set;

//C4.5实现

publicclassInfoGain{

privateList>data;

privateListattribute;

publicInfoGain(List>data,Listattribute){

this.data=newArrayList>();

for(inti=0;i

Listtemp=data.get(i);

ArrayListt=newArrayList();

for(intj=0;j

t.add(temp.get(j));

}

this.data.add(t);

}

this.attribute=newArrayList

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1