数据挖掘决策树java

上传人:第*** 文档编号:32756687 上传时间:2018-02-12 格式:DOCX 页数:9 大小:16.79KB
返回 下载 相关 举报
数据挖掘决策树java_第1页
第1页 / 共9页
数据挖掘决策树java_第2页
第2页 / 共9页
数据挖掘决策树java_第3页
第3页 / 共9页
数据挖掘决策树java_第4页
第4页 / 共9页
数据挖掘决策树java_第5页
第5页 / 共9页
点击查看更多>>
资源描述

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

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

2、childs = null;public TreeNode(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 Linke

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

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

5、性上进行分类String return_atrribute = null;/计算每个未分类属性的 Gain 值int count = 0; /计算到第几个属性for(String atrribute:atrributes) /该属性有多少个值,该属性有多少个分类int values_count, class_count;/属性值对应的下标int index = attrIndexMap.get(atrribute);/存放属性的各个值和分类值LinkedHashSet values = new LinkedHashSet();LinkedHashSet classes = new Linked

6、HashSet();for(int i = 0; i max) max = Gaini;return_atrribute = atrribute;i+;return return_atrribute;/node:在当前结点构造决策树/deData:数据集/flags:指示在当前结点构造决策树时哪些数据是需要的/attributes:未分类的属性集/attrIndexMap:属性与对应数据下标public void buildDecisionTree(TreeNode node, String deData, boolean flags,LinkedHashSet attributes, Has

7、hMap attrIndexMap) /如果待分类属性已空if(attributes.isEmpty() = true) /从数据集中选择多数类,遍历符合条件的所有数据HashMap classMap = new HashMap();int classIndex = deData0.length - 1;for(int i = 0; i it = classMap.keySet().iterator();while(it.hasNext() String strClass = (String)it.next();if(classMap.get(strClass) mostCount) most

8、Class = strClass;mostCount = classMap.get(strClass);/对结点进行赋值,该结点为叶结点node.setElement(mostClass);node.setChilds(null);System.out.println(yezhi: + node.getElement() + : + node.getValue();return;/如果待分类数据全都属于一个类int class_index = deData0.length - 1;String class_name = null;HashSet classSet = new HashSet()

9、;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(tn);node.setChilds(childs);/在候选分类属性中删除当前属性attributes.remove(attribute);/在各个子结点上递归调用本函数if(childs.isEmpty() != true) for(Tr

10、eeNode child:childs) /设置子结点待分类的数据集boolean newFlags = new booleandeData.length ;for(int i = 0; i newAttributes = new LinkedHashSet();for(String attr:attributes) newAttributes.add(attr);/在子结点上递归生成决策树buildDecisionTree(child, deData, newFlags, newAttributes, attrIndexMap);/输出决策树public void printDecision

11、Tree() public class DeTree public static void main(String args) /*/输入数据集 1String deData = new String12;deData0 = new StringYes,No,No,Yes,Some,high,No,Yes,French,010,Yes;deData1 = 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;deData

12、3 = 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,middle,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

13、,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;deData10= 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;/待分类的属性集 1String att

14、r = new Stringalt, bar, fri, hun, pat, price, rain, res, type, est;*/输入数据集 2String deData = new String14;deData0 = new Stringyouth,high,no,fair,no;deData1 = new Stringyouth,high,no,excellent,no;deData2 = new Stringmiddle_aged,high,no,fair,yes;deData3 = new Stringsenior,medium,no,fair,yes;deData4 = new Stringsenior,low,yes,fair,yes;deData5 = new Stringsenior,low,yes,excellent,no;deData6 = new Stringmiddle_aged,low,yes,excellent,yes;deDat

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

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

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