下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、【決策樹】一C4.5算法建立決策樹JAVA練習(xí)以下程序是我練習(xí)寫的,不一定正確也沒做存儲優(yōu)化。有問題請留言交流。轉(zhuǎn)載請掛連接。當(dāng)前的屬性為:age in come stude nt credit_rat ing當(dāng)前的數(shù)據(jù)集為( 取后列疋TARGET_VALUE)youthhighnofairnoyouthhighnoexcelle ntnomiddle_agedhighnofairyesseniorlowyesfairyesseniorlowyesexcelle ntnomiddle_agedlowyesexcelle ntyesyouthmediumnofairnoyouthlowyesfa
2、iryesseniormediumyesfairyesyouthmediumyesexcelle ntyesmiddle_agedhighyesfairyesseniormediumnoexcelle ntnoC4.5建立樹類package C45Test;import java.util.ArrayList; import java.util.List;import java.util.Map;public class Decisi on Tree public TreeNode createDT(ListArrayList attributeList)System.out.println(
3、”當(dāng)前的 DATA 為);for (int i=O;idata.size();i+)ArrayList temp = data.get(i);for (int j=0;jtemp.size();j+) System.out.pri nt(temp.get(j)+);System.out.pri ntl n();data,ListSystem.out.println(”-);System.out.println(”當(dāng)前的 ATTR 為);for (int i=O;iattributeList.size();i+)System.out.pri nt(attributeList.get(i)+ );
4、System.out.pri ntl n();System.out.println(”-);TreeNode node = n ewTreeNode();Stri ng result = In foGai n.lsPure(l nfoGai n.getTarget(data);if (result != null )n ode.setNodeName(leafNode);no de.setTargetF unV alue(result);retur n no de;if (attributeList.size() = 0)no de.setTargetF unV alue(result);re
5、tur n no de; else In foGai n gai n =n ewI nfoGai n(data,attributeList);double maxGai n = 0.0;int attrIndex = -1;for (int i=0;iattributeList.size();i+) double tempGai n =gain. getGa in Ratio(i); if (maxGain tempGain)maxGa in = tempGa in;attrI ndex = i;System.out.pri ntln (”選擇出的最大增益率屬性為:attributeList.
6、get(attrI ndex);no de.setAttributeValue(attributeList.get(attrI ndex);ListArrayList resultData =null ;Map attrvalueMap =gai n.getAttributeValue(attrI ndex);for (Map.EntryvString, Long entry :attrvalue Map .e ntrySet()resultData = gain. getData4Value(e ntry.getKey(),attrI ndex);TreeNode leafNode =Sys
7、tem.out.pri ntl n(”+attributeList.get(attrIndex)+null ;當(dāng)前為的+e ntry.getKey()+分支。);if (resultData.size() = 0)leafNode =newTreeNode();leafNode.setNodeName(attributeList.get(attrl ndex); leafNode.setTargetF unValue(result); leafNode.setAttributeValue(e ntry.getKey();else for ( int j = 0; j resultAttr =n
8、ewArrayListvStri ng(attributeList);resultAttr.remove(attrI ndex);leafNode = createDT(resultData,resultAttr);n ode.getChildTreeNode().add(leafNode);no de.getPathName().add(e ntry.getKey();return no de;class TreeNodeprivate Stri ng attributeValue;private List childTreeNode;private List pathName;privat
9、e Stri ng targetF unV alue;private Stri ng no deName;public TreeNode(String nodeName)this .no deName = no deName;this .childTreeNode = newArrayList(); this .pathName= newArrayList();public TreeNode()this .childTreeNode = newArrayList(); this .pathName= newArrayList();public String getAttributeValue(
10、) return attributeValue;public void setAttributeValue(String attributevalue) this .attributeValue = attributeValue;public List getChildTreeNode() return childTreeNode;public void setChildTreeNode(List childTreeNode) this .childTreeNode = childTreeNode;public String getTargetFunValue() return targetF
11、unValue;public void setTargetFunValue(String targetFunValue) this .targetFunValue = targetFunValue;public String getNodeName() retur n no deName;public void setNodeName(Stri ng no deName) this .no deName = nodeName;public List getPathName() return pathName;public void setPathName(List pathName) this
12、 .pathName =pathName;Ql增益率計算類(取log的時候底用的是e,沒用2)package C45Test;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/C 4.5 實現(xiàn)public class InfoGain private ListArrayList data;private List
13、 attribute;public InfoGain(ListArrayList data,List attributethis .data =newArrayListArrayList();for (int i=O;idata.size();i+)List temp = data.get(i);ArrayList t =n ewArrayList();for (int j=0;jtemp.size();j+) t.add(temp.get(j);this .data.add(t);this .attribute =newArrayList();for (int k=0;kattribute.
14、size();k+) this .attribute.add(attribute.get(k);/*this.data = data;this.attribute = attribute;*/獲得熵public double getE ntropy()Map targetValueMap = getTargetValue();Set targetkey = targetValueMap.keySet();double en tropy = 0.0;for (String key : targetkey)double p = MathUtils.div( double )targetValueM
15、ap.get(key),(double )data.size();en tropy += (-1) * p * Math.log(p);return entropy;/ 獲得 InfoApublic double getl nfoAttribute( int attribute In dex)MapvStri ng,L ong attributeValueMap = getAttributeValue(attributeI ndex);double infoA = 0.0;for (Map.EntryvString, Long entry :attributeValueM ap.e ntryS
16、et()int size = data.size();double attributeP = MathUtils.div( double )entry.getValue()(double) size);Map targetValueMap = getAttributeValueTargetValue(entry.getKey(),attributeI ndex);long totalCount = 0L;for (Map.EntryvString, Longen tryValue :targetValue Map .e ntrySet()totalCo unt += en tryValue.g
17、etValue();double valueSum = 0.0;for (Map.EntryvString, Long entryTargetValue :targetValueMap.e ntrySet()double p =MathUtils.div( double )e ntryTargetValue.getValue(),(double )totalCou nt);valueSum += Math.log(p) * p;infoA += (-1) * attributeP * valueSum;return infoA;/得到屬性值在決策空間的比例public MapvString,L
18、ong getAttributeValueTargetValue(StringattributeName, int attribute In dex)MapvStri ng,L ong targetValueMap =newHashMapvString,Long();lteratorArrayList iterator = data.iterator();while (iterator.hasNext()List tempList = iterator. next();if (attributeName.equalsIgnoreCase(tempList.get(attributeIndex)
19、int size = tempList.size();Stri ng key = tempList.get(size - 1);Long value = targetValueMap.get(key);targetValueMap.put(key, value !=return targetValueMap;null ? +value :1L);/得到屬性在決策空間上的數(shù)量public Map getAttributeValue(int attributelndex)Map attributeValueMap =HashMap();for (ArrayList note : data) Str
20、ing key = no te.get(attributel ndex); Longvalue = attributeValueMap.get(key);attributeValueMap.put(key, value !=return attributeValueMap;newnull ? +value :1L);public ListArrayList getData4Value(String attrValue,intattrl ndex)ListArrayList resultData =newArrayListArrayList();IteratorArrayList iterato
21、r = data.iterator();for (;iterator.hasNext();)ArrayList templist = iterator. next();if(templist.get(attrlndex).equalslgnoreCase(attrValue) ArrayList temp = (ArrayList) templist.clo ne();resultData.add(temp);return resultData;/獲得增益率public double getGa in Ratio( int attribute In dex)return MathUtils.d
22、iv(getGain(attributeIndex), getSplitI nfo(attributelndex);/獲得增益量public double getGa in( int attribute In dex)return getEntropy() - getInfoAttribute(attributelndex);/得到懲罰因子public double getSplitI nfo( int attribute In dex)MapvStri ng,L ong attributeValueMap =getAttributeValue(attributel ndex);double
23、splitA = 0.0;for (Map.EntryvString, Long entry :attributeValueM ap.e ntrySet()int size = data.size();double attributeP = MathUtils.div( double )entry.getValue()(double) size);splitA += attributeP * Math.log(attributeP) * (-1);return splitA;/得到目標(biāo)函數(shù)在當(dāng)前集合范圍內(nèi)的離散的值public Map getTargetValue()Map targetVal
24、ueMap =n ewHashMapvStri ng,L ong();IteratorArrayList iterator = data.iterator();while (iterator.hasNext()List tempList = iterator. next();Stri ng key = tempList.get(tempList.size() - 1);Long value = targetValueMap.get(key);targetValueMap.put(key, value !=null ? +value : 1L);return targetValueMap;/獲得
25、 TARGE 值public static ListvStringgetTarget(ListArrayListListvStri ng list =n ewArrayList();for (ArrayList temp : data) int index = temp.size() -1;Stri ng value = temp.get(i ndex); list.add(value);return list;/判斷當(dāng)前純度是否 100%public static Stri ng lsPure(List list)Set set =n ewHashSet();for (String name
26、 :list)set.add( name);if (set.size() 1) return null ;IteratorvString iterator = set.iterator(); return iterator.next();測試類,數(shù)據(jù)集讀取以上的分別放到2個List中。package C45Test;import java.util.ArrayList;import java.util.List;import C45Test.DecisionTree.TreeNode;public class MainC45 private staticfinal ListArrayList
27、dataList =ArrayListArrayList();private staticfinal ListvStri ng attributeList =newArrayList();data)newpublic static void main(String args)Decisi on Tree dt =new Decisi on Tree();TreeNode node = dt.createDT(configData(),configAttribute();System.out.pri ntl n();大數(shù)運算工具類package C45Test;import java.math.
28、BigDecimal;public abstract class MathUtils 默認(rèn)余數(shù)長度private static final int DIV_SCALE = 10;/受限于 DOUBL 長度public static double add( double value1, double value2)BigDecimal big1 =n ewBigDecimal(Stri ng.valueOf(value1);BigDecimal big2 =n ewBigDecimal(Stri ng.valueOf(value2);return big1.add(big2).doubleValue();/大數(shù)加法public static double add(Stri ng value1,Stri ng value2)BigDecima
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 寵物肥胖癥的預(yù)防與健康飲食規(guī)劃
- 河北化工醫(yī)藥職業(yè)技術(shù)學(xué)院《馬克思主義與社會科學(xué)方法論》2023-2024學(xué)年第一學(xué)期期末試卷
- 小學(xué)課堂時間管理與規(guī)劃策略
- 2025年度綠化帶樹苗栽種與后續(xù)管護合同3篇
- 2025年度特許經(jīng)營合同某知名快餐品牌在中國區(qū)域的特許經(jīng)營3篇
- 2023年3月遼寧省普通高中學(xué)業(yè)水平合格性考試物理仿真模擬試卷C(解析版)
- 2024租鋪合同轉(zhuǎn)讓協(xié)議書
- 2024年電子產(chǎn)品代購合同范本-手機平板電腦代購6篇
- 藥房護理工作總結(jié)
- 2025年度股權(quán)轉(zhuǎn)讓合同:某科技公司的股權(quán)轉(zhuǎn)讓3篇
- 中學(xué)課堂教學(xué)評價量表
- 食堂食材配送以及售后服務(wù)方案
- 稱量與天平培訓(xùn)試題及答案
- 塊單項活動教學(xué)材料教案丹霞地貌
- 超全的超濾與納濾概述、基本理論和應(yīng)用
- 青年人應(yīng)該如何樹立正確的人生觀
- 開封辦公樓頂發(fā)光字制作預(yù)算單
- 安全生產(chǎn)標(biāo)準(zhǔn)化管理工作流程圖
- 德龍自卸車合格證掃描件(原圖)
- 藥店-醫(yī)療器械組織機構(gòu)和部門設(shè)置說明-醫(yī)療器械經(jīng)營組織機構(gòu)圖--醫(yī)療器械組織機構(gòu)圖
- 自薦書(彩色封面)
評論
0/150
提交評論