欢迎来到天天文库
浏览记录
ID:38587845
大小:59.50 KB
页数:11页
时间:2019-06-15
《决策树java代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.LinkedHashSet;classTreeNode{Stringelement;//该值为数据的属性名称Stringvalue;//上一个分裂属性在此结点的值LinkedHashSetchilds;//结点的子结点,以有顺序的链式哈希集存储publicTreeNode(){this.element=null;this.value=nu
2、ll;this.childs=null;}publicTreeNode(Stringvalue){this.element=null;this.value=value;this.childs=null;}publicStringgetElement(){returnthis.element;}publicvoidsetElement(Stringe){this.element=e;}publicStringgetValue(){returnthis.value;}publicvoidsetValue(Stringv){this.va
3、lue=v;}publicLinkedHashSetgetChilds(){returnthis.childs;}publicvoidsetChilds(LinkedHashSetchilds){this.childs=childs;}}//决策树类classDecisionTree{TreeNoderoot;//决策树的树根结点publicDecisionTree(){root=newTreeNode();}publicDecisionTree(TreeNoderoot){this.root
4、=root;}publicTreeNodegetRoot(){returnroot;}publicvoidsetRoot(TreeNoderoot){this.root=root;}publicStringselectAtrribute(TreeNodenode,String[][]deData,booleanflags[],LinkedHashSetatrributes,HashMapattrIndexMap){//Gain数组存放当前结点未分类属性的Gain值doubleGain[
5、]=newdouble[atrributes.size()];//每条数据中归类的下标,为每条数据的最后一个值intclass_index=deData[0].length-1;//属性名,该结点在该属性上进行分类Stringreturn_atrribute=null;//计算每个未分类属性的Gain值intcount=0;//计算到第几个属性for(Stringatrribute:atrributes){//该属性有多少个值,该属性有多少个分类intvalues_count,class_count;//属性值对应的下标intind
6、ex=attrIndexMap.get(atrribute);//存放属性的各个值和分类值LinkedHashSetvalues=newLinkedHashSet();LinkedHashSetclasses=newLinkedHashSet();for(inti=0;i7、]);}}values_count=values.size();class_count=classes.size();intvalues_vector[]=newint[values_count*class_count];intclass_vector[]=newint[class_count];for(inti=0;i8、{j++;}}intk=0;for(Stringc:classes){if(deData[i][class_index].equals(c)){break;}else{k++;}}values_vector[j*class_count
7、]);}}values_count=values.size();class_count=classes.size();intvalues_vector[]=newint[values_count*class_count];intclass_vector[]=newint[class_count];for(inti=0;i8、{j++;}}intk=0;for(Stringc:classes){if(deData[i][class_index].equals(c)){break;}else{k++;}}values_vector[j*class_count
8、{j++;}}intk=0;for(Stringc:classes){if(deData[i][class_index].equals(c)){break;}else{k++;}}values_vector[j*class_count
此文档下载收益归作者所有