
在機(jī)器學(xué)習(xí)中,因為決策樹的算法是十分給力,因此使用決策樹能夠幫助我們解決很多的問題。決策樹的算法分為很多種,今天小編主要跟大家介紹一下決策樹的分類算法。
一、決策樹的概念
決策樹,根據(jù)名字就能知道,是一種樹,一種依托于策略抉擇而建立起來的樹。在 機(jī)器學(xué)習(xí)中,決策樹是一個預(yù)測模型,代表的是對象屬性與對象值之間的一種映射關(guān)系。從數(shù)據(jù)產(chǎn)生決策樹的機(jī)器學(xué)習(xí)技術(shù)叫做決策樹學(xué)習(xí), 通俗點說就是:決策樹是一種依托于分類、訓(xùn)練上的預(yù)測樹,可以根據(jù)已知,對未來進(jìn)行預(yù)測、歸類。
舉一個簡單的例子來說明:
一個女孩選擇相親對象,通過年齡是否超過30、長相丑或不丑、收入是否低水平,以及否是公務(wù)員這幾項,將相親對象分為兩個類別:見和不見。假設(shè)這個女孩對相親對象的要求為:30歲以下、長相不丑,而且高收入,或者中等以上收入的公務(wù)員,那么女孩的決策邏輯可以用下圖來表示,典型的分類決策樹。
二、決策樹分類算法
1. ID3選取信息增益的屬性遞歸進(jìn)行分類
“熵”表示隨機(jī)變量不確定性的度量,并且熵只依賴于X的分布,與X具體取值無關(guān),所以可以表示為,熵越大,隨機(jī)變量的不確定性就越大:
信息熵: H(X)=-sigma(對每一個x)(plogp)
“條件熵H(Y|X)”表示在已知隨機(jī)變量X的條件下,隨機(jī)變量Y的不確定性:
H(Y|X)=sigma(對每一個x)(pH(Y|X=xi))
“信息增益”特征A對訓(xùn)練數(shù)據(jù)集D的信息增益g(D,A),具體定義為:集合D的經(jīng)驗熵H(D),和特征A給定條件下D的經(jīng)驗條件熵H(D)熵
信息增益:g(D,A)=H(D)-H(D|A) H(D)為整個數(shù)據(jù)集的熵
信息增益率:(H(D)-H(D|X))/H(X)
算法流程:(1)計算每一個屬性的信息增益,如果信息增益小于閾值,就將該支置為葉節(jié)點,選擇其中個數(shù)最多的類標(biāo)簽來作為該類的類標(biāo)簽。反之,則選擇其中最大的作為分類屬 性。
(2)若果各個分支中都只含有同一類數(shù)據(jù),那么就將這支置為葉子節(jié)點, 否則 繼續(xù)進(jìn)行(1)。
2. C4.5算法
C4.5算法是ID3的改進(jìn)算法 , 是機(jī)器學(xué)習(xí)算法中的另一個分類決策樹算法,可以說是決策樹核心算法。
C4.5算法特點:
C4.5用信息增益率來選擇屬性。
能處理非離散數(shù)據(jù)。
能夠處理不完整數(shù)據(jù)進(jìn)行
一個可以選擇的度量標(biāo)準(zhǔn)是增益比率gain ratio(Quinlan 1986)。增益比率度量是用前面的增益度量Gain(S,A)和分裂信息度量SplitInformation(S,A)來共同定義的,如下所示:
其中,分裂信息度量被定義為(分裂信息用來衡量屬性分裂數(shù)據(jù)的廣度和均勻):
C4.5算法構(gòu)造決策樹過程:
Function C4.5(R:包含連續(xù)屬性的無類別屬性集合,C:類別屬性,S:訓(xùn)練集)
/*返回一棵決策樹*/
Begin
If S為空,返回一個值為Failure的單個節(jié)點;
If S是由相同類別屬性值的記錄組成,
返回一個帶有該值的單個節(jié)點;
If R為空,則返回一個單節(jié)點,其值為在S的記錄中找出的頻率最高的類別屬性值;
[注意未出現(xiàn)錯誤則意味著是不適合分類的記錄];
For 所有的屬性R(Ri) Do
If 屬性Ri為連續(xù)屬性,則
Begin
將Ri的最小值賦給A1:
將Rm的最大值賦給Am;/*m值手工設(shè)置*/
For j From 2 To m-1 Do Aj=A1+j*(A1Am)/m;
將Ri點的基于{< =Aj,>Aj}的最大信息增益屬性(Ri,S)賦給A;
End;
將R中屬性之間具有最大信息增益的屬性(D,S)賦給D;
將屬性D的值賦給{dj/j=1.2...m};
將分別由對應(yīng)于D的值為dj的記錄組成的S的子集賦給{sj/j=1.2...m};
返回一棵樹,其根標(biāo)記為D;樹枝標(biāo)記為d1.d2...dm;
再分別構(gòu)造以下樹:
C4.5(R-{D},C,S1),C4.5(R-{D},C,S2)...C4.5(R-{D},C,Sm);
End C4.5
3.CART算法:
基尼系數(shù):Gini(p)=sigma(每一個類)p(1-p)
回歸樹:屬性值為連續(xù)實數(shù)。將整個輸入空間劃分為m塊,每一塊以其平均值作為輸出。f(x)=sigma(每一塊)Cm*I(x屬于Rm)
回歸樹生成:(1)選取切分變量和切分點,將輸入空間分為兩份。
(2)每一份分別進(jìn)行第一步,直到滿足停止條件。
切分變量和切分點選取:對于每一個變量進(jìn)行遍歷,從中選擇切分點。選擇一個切分點滿足分類均方誤差最小。然后在選出所有變量中最小分類誤差最小的變量作為切分 變量。
分類樹:屬性值為離散值。
分類樹生成:(1)根據(jù)每一個屬性的每一個取值,是否取該值將樣本分成兩類,計算基尼系數(shù)。選擇基尼系數(shù)最小的特征和屬性值,將樣本分成兩份。
(2)遞歸調(diào)用(1)直到無法分割。完成CART樹生成。
四、python實現(xiàn)
from sklearn.datasets import load_iris import numpy as np import math from collections import Counter class decisionnode: def __init__(self, d=None, thre=None, results=None, NH=None, lb=None, rb=None, max_label=None): self.d = d # d表示維度 self.thre = thre # thre表示二分時的比較值,將樣本集分為2類 self.results = results # 最后的葉節(jié)點代表的類別 self.NH = NH # 存儲各節(jié)點的樣本量與經(jīng)驗熵的乘積,便于剪枝時使用 self.lb = lb # desision node,對應(yīng)于樣本在d維的數(shù)據(jù)小于thre時,樹上相對于當(dāng)前節(jié)點的子樹上的節(jié)點 self.rb = rb # desision node,對應(yīng)于樣本在d維的數(shù)據(jù)大于thre時,樹上相對于當(dāng)前節(jié)點的子樹上的節(jié)點 self.max_label = max_label # 記錄當(dāng)前節(jié)點包含的label中同類最多的label def entropy(y): ''' 計算信息熵,y為labels ''' if y.size > 1: category = list(set(y)) else: category = [y.item()] y = [y.item()] ent = 0 for label in category: p = len([label_ for label_ in y if label_ == label]) / len(y) ent += -p * math.log(p, 2) return ent def Gini(y): ''' 計算基尼指數(shù),y為labels ''' category = list(set(y)) gini = 1 for label in category: p = len([label_ for label_ in y if label_ == label]) / len(y) gini += -p * p return gini def GainEnt_max(X, y, d): ''' 計算選擇屬性attr的最大信息增益,X為樣本集,y為label,d為一個維度,type為int ''' ent_X = entropy(y) X_attr = X[:, d] X_attr = list(set(X_attr)) X_attr = sorted(X_attr) Gain = 0 thre = 0 for i in range(len(X_attr) - 1): thre_temp = (X_attr[i] + X_attr[i + 1]) / 2 y_small_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] <= thre_temp] y_big_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] > thre_temp] y_small = y[y_small_index] y_big = y[y_big_index] Gain_temp = ent_X - (len(y_small) / len(y)) * \ entropy(y_small) - (len(y_big) / len(y)) * entropy(y_big) ''' intrinsic_value = -(len(y_small) / len(y)) * math.log(len(y_small) / len(y), 2) - (len(y_big) / len(y)) * math.log(len(y_big) / len(y), 2) Gain_temp = Gain_temp / intrinsic_value ''' # print(Gain_temp) if Gain < Gain_temp: Gain = Gain_temp thre = thre_temp return Gain, thre def Gini_index_min(X, y, d): ''' 計算選擇屬性attr的最小基尼指數(shù),X為樣本集,y為label,d為一個維度,type為int ''' X = X.reshape(-1, len(X.T)) X_attr = X[:, d] X_attr = list(set(X_attr)) X_attr = sorted(X_attr) Gini_index = 1 thre = 0 for i in range(len(X_attr) - 1): thre_temp = (X_attr[i] + X_attr[i + 1]) / 2 y_small_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] <= thre_temp] y_big_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] > thre_temp] y_small = y[y_small_index] y_big = y[y_big_index] Gini_index_temp = (len(y_small) / len(y)) * \ Gini(y_small) + (len(y_big) / len(y)) * Gini(y_big) if Gini_index > Gini_index_temp: Gini_index = Gini_index_temp thre = thre_temp return Gini_index, thre def attribute_based_on_GainEnt(X, y): ''' 基于信息增益選擇最優(yōu)屬性,X為樣本集,y為label ''' D = np.arange(len(X[0])) Gain_max = 0 thre_ = 0 d_ = 0 for d in D: Gain, thre = GainEnt_max(X, y, d) if Gain_max < Gain: Gain_max = Gain thre_ = thre d_ = d # 維度標(biāo)號 return Gain_max, thre_, d_ def attribute_based_on_Giniindex(X, y): ''' 基于信息增益選擇最優(yōu)屬性,X為樣本集,y為label ''' D = np.arange(len(X.T)) Gini_Index_Min = 1 thre_ = 0 d_ = 0 for d in D: Gini_index, thre = Gini_index_min(X, y, d) if Gini_Index_Min > Gini_index: Gini_Index_Min = Gini_index thre_ = thre d_ = d # 維度標(biāo)號 return Gini_Index_Min, thre_, d_ def devide_group(X, y, thre, d): ''' 按照維度d下閾值為thre分為兩類并返回 ''' X_in_d = X[:, d] x_small_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] <= thre] x_big_index = [i_arg for i_arg in range( len(X[:, d])) if X[i_arg, d] > thre] X_small = X[x_small_index] y_small = y[x_small_index] X_big = X[x_big_index] y_big = y[x_big_index] return X_small, y_small, X_big, y_big def NtHt(y): ''' 計算經(jīng)驗熵與樣本數(shù)的乘積,用來剪枝,y為labels ''' ent = entropy(y) print('ent={},y_len={},all={}'.format(ent, len(y), ent * len(y))) return ent * len(y) def maxlabel(y): label_ = Counter(y).most_common(1) return label_[0][0] def buildtree(X, y, method='Gini'): ''' 遞歸的方式構(gòu)建決策樹 ''' if y.size > 1: if method == 'Gini': Gain_max, thre, d = attribute_based_on_Giniindex(X, y) elif method == 'GainEnt': Gain_max, thre, d = attribute_based_on_GainEnt(X, y) if (Gain_max > 0 and method == 'GainEnt') or (Gain_max >= 0 and len(list(set(y))) > 1 and method == 'Gini'): X_small, y_small, X_big, y_big = devide_group(X, y, thre, d) left_branch = buildtree(X_small, y_small, method=method) right_branch = buildtree(X_big, y_big, method=method) nh = NtHt(y) max_label = maxlabel(y) return decisionnode(d=d, thre=thre, NH=nh, lb=left_branch, rb=right_branch, max_label=max_label) else: nh = NtHt(y) max_label = maxlabel(y) return decisionnode(results=y[0], NH=nh, max_label=max_label) else: nh = NtHt(y) max_label = maxlabel(y) return decisionnode(results=y.item(), NH=nh, max_label=max_label) def printtree(tree, indent='-', dict_tree={}, direct='L'): # 是否是葉節(jié)點 if tree.results != None: print(tree.results) dict_tree = {direct: str(tree.results)} else: # 打印判斷條件 print(str(tree.d) + ":" + str(tree.thre) + "? ") # 打印分支 print(indent + "L->",) a = printtree(tree.lb, indent=indent + "-", direct='L') aa = a.copy() print(indent + "R->",) b = printtree(tree.rb, indent=indent + "-", direct='R') bb = b.copy() aa.update(bb) stri = str(tree.d) + ":" + str(tree.thre) + "?" if indent != '-': dict_tree = {direct: {stri: aa}} else: dict_tree = {stri: aa} return dict_tree def classify(observation, tree): if tree.results != None: return tree.results else: v = observation[tree.d] branch = None if v > tree.thre: branch = tree.rb else: branch = tree.lb return classify(observation, branch) def pruning(tree, alpha=0.1): if tree.lb.results == None: pruning(tree.lb, alpha) if tree.rb.results == None: pruning(tree.rb, alpha) if tree.lb.results != None and tree.rb.results != None: before_pruning = tree.lb.NH + tree.rb.NH + 2 * alpha after_pruning = tree.NH + alpha print('before_pruning={},after_pruning={}'.format( before_pruning, after_pruning)) if after_pruning <= before_pruning: print('pruning--{}:{}?'.format(tree.d, tree.thre)) tree.lb, tree.rb = None, None tree.results = tree.max_label if __name__ == '__main__': iris = load_iris() X = iris.data y = iris.target permutation = np.random.permutation(X.shape[0]) shuffled_dataset = X[permutation, :] shuffled_labels = y[permutation] train_data = shuffled_dataset[:100, :] train_label = shuffled_labels[:100] test_data = shuffled_dataset[100:150, :] test_label = shuffled_labels[100:150] tree1 = buildtree(train_data, train_label, method='Gini') print('=============================') tree2 = buildtree(train_data, train_label, method='GainEnt') a = printtree(tree=tree1) b = printtree(tree=tree2) true_count = 0 for i in range(len(test_label)): predict = classify(test_data[i], tree1) if predict == test_label[i]: true_count += 1 print("CARTTree:{}".format(true_count)) true_count = 0 for i in range(len(test_label)): predict = classify(test_data[i], tree2) if predict == test_label[i]: true_count += 1 print("C3Tree:{}".format(true_count)) #print(attribute_based_on_Giniindex(X[49:51, :], y[49:51])) from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默認(rèn)字體 mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像時負(fù)號'-'顯示為方塊的問題 import treePlotter import matplotlib.pyplot as plt treePlotter.createPlot(a, 1) treePlotter.createPlot(b, 2) # 剪枝處理 pruning(tree=tree1, alpha=4) pruning(tree=tree2, alpha=4) a = printtree(tree=tree1) b = printtree(tree=tree2) true_count = 0 for i in range(len(test_label)): predict = classify(test_data[i], tree1) if predict == test_label[i]: true_count += 1 print("CARTTree:{}".format(true_count)) true_count = 0 for i in range(len(test_label)): predict = classify(test_data[i], tree2) if predict == test_label[i]: true_count += 1 print("C3Tree:{}".format(true_count)) treePlotter.createPlot(a, 3) treePlotter.createPlot(b, 4) plt.show()
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03