决策树java代码

上传人:第*** 文档编号:32746012 上传时间:2018-02-12 格式:DOC 页数:11 大小:59.50KB
返回 下载 相关 举报
决策树java代码_第1页
第1页 / 共11页
决策树java代码_第2页
第2页 / 共11页
决策树java代码_第3页
第3页 / 共11页
决策树java代码_第4页
第4页 / 共11页
决策树java代码_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《决策树java代码》由会员分享,可在线阅读,更多相关《决策树java代码(11页珍藏版)》请在金锄头文库上搜索。

1、import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashSet;class TreeNodeString element; / 该值为数据的属性名称String value; / 上一个分裂属性在此结点的值LinkedHashSet childs; / 结点的子结点,以有顺序的链式哈希集存储public TreeNode()this.element=null;this.value=null;this.childs=null;public Tree

2、Node(String value)this.element=null;this.value=value;this.childs=null;public String getElement()return this.element;public void setElement(String e)this.element=e;public String getValue()return this.value;public void setValue(String v)this.value=v; public LinkedHashSet getChilds()return this.childs;

3、public void setChilds(LinkedHashSet childs)this.childs=childs;/ 决策树类class DecisionTreeTreeNode root; / 决策树的树根结点public DecisionTree()root=new TreeNode();public DecisionTree(TreeNode root)this.root=root;public TreeNode getRoot()return root;public void setRoot(TreeNode root)this.root=root;public String

4、 selectAtrribute(TreeNode node, String deData, boolean flags,LinkedHashSet atrributes, HashMap attrIndexMap)/ Gain 数组存放当前结点未分类属性的 Gain 值 double Gain=new doubleatrributes.size(); / 每条数据中归类的下标,为每条数据的最后一个值int class_index=deData0.length - 1; / 属性名,该结点在该属性上进行分类String return_atrribute=null; / 计算每个未分类属性的 G

5、ain 值int count=0; / 计算到第几个属性for(String atrribute : atrributes) / 该属性有多少个值,该属性有多少个分类int values_count, class_count; / 属性值对应的下标int index=attrIndexMap.get(atrribute);/ 存放属性的各个值和分类值LinkedHashSet values=new LinkedHashSet();LinkedHashSet classes=new LinkedHashSet();for(int i=0; i max)max=Gaini;return_atrri

6、bute=atrribute; i+;return return_atrribute;public void buildDecisionTree(TreeNode node, String deData, boolean flags,LinkedHashSet attributes, HashMap attrIndexMap) / 如果待分类属性已空if(attributes.isEmpty() = true)/ 从数据集中选择多数类,遍历符合条件的所有数据HashMap classMap=new HashMap();int classIndex=deData0.length - 1;for(

7、int i=0; i it=classMap.keySet().iterator();while(it.hasNext()String strClass=(String) it.next();if(classMap.get(strClass) mostCount)mostClass=strClass;mostCount=classMap.get(strClass); / 对结点进行赋值,该结点为叶结点node.setElement(mostClass);node.setChilds(null);System.out.println(yezhi: + node.getElement() + :

8、+ node.getValue();return;/ 如果待分类数据全都属于一个类int class_index=deData0.length - 1;String class_name=null;HashSet classSet=new HashSet();for(int i=0; i attrValues=new LinkedHashSet();for(int i=0; i childs=new LinkedHashSet();for(String attrValue : attrValues)TreeNode tn=new TreeNode(attrValue);childs.add(t

9、n);node.setChilds(childs);/ 在候选分类属性中删除当前属性attributes.remove(attribute);/ 在各个子结点上递归调用本函数if(childs.isEmpty() != true)for(TreeNode child : childs) / 设置子结点待分类的数据集boolean newFlags=new booleandeData.length;for(int i=0; i newAttributes=new LinkedHashSet();for(String attr : attributes)newAttributes.add(attr

10、);/ 在子结点上递归生成决策树buildDecisionTree(child, deData, newFlags, newAttributes, attrIndexMap); / 输出决策树public void printDecisionTree()public class Data2public static void main(String args) /* /输入数据集 1 String deData = new String12; deData0 = new* StringYes,No,No,Yes,Some,high,No,Yes,French,010,Yes; deData1

11、=* new StringYes,No,No,Yes,Full,low,No,No,Thai,3060,No; deData2 =* new StringNo,Yes,No,No,Some,low,No,No,Burger,010,Yes; deData3* = new StringYes,No,Yes,Yes,Full,low,Yes,No,Thai,1030,Yes;* deData4 = new* StringYes,No,Yes,No,Full,high,No,Yes,French,60,No; deData5 =* new StringNo,Yes,No,Yes,Some,middl

12、e,Yes,Yes,Italian,010,Yes;* deData6 = new* StringNo,Yes,No,No,None,low,Yes,No,Burger,010,No; deData7 =* new StringNo,No,No,Yes,Some,middle,Yes,Yes,Thai,010,Yes;* deData8 = new* StringNo,Yes,Yes,No,Full,low,Yes,No,Burger,60,No; deData9 =* new StringYes,Yes,Yes,Yes,Full,high,No,Yes,Italian,1030,No;* d

13、eData10= new StringNo,No,No,No,None,low,No,No,Thai,010,No;* deData11= new* StringYes,Yes,Yes,Yes,Full,low,No,No,Burger,3060,Yes; /待分类的属性集 1* String attr = new Stringalt, bar, fri, hun, pat, price, rain, res,* type, est;*/ 输入数据集 2String deData=new String14; deData0=new String youth, high, no, fair, n

14、o ;deData1=new String youth, high, no, excellent, no ;deData2=new String middle_aged, high, no, fair, yes ;deData3=new String senior, medium, no, fair, yes ;deData4=new String senior, low, yes, fair, yes ;deData5=new String senior, low, yes, excellent, no ;deData6=new String middle_aged, low, yes, excellent, yes ;deData7=new String youth, medium, no, fair, no ;deData8=new St

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号