決策樹的python實現(xiàn)方法
本文實例講述了決策樹的python實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
決策樹算法優(yōu)缺點:
優(yōu)點:計算復(fù)雜度不高,輸出結(jié)果易于理解,對中間值缺失不敏感,可以處理不相關(guān)的特征數(shù)據(jù)
缺點:可能會產(chǎn)生過度匹配的問題
適用數(shù)據(jù)類型:數(shù)值型和標(biāo)稱型
算法思想:
1.決策樹構(gòu)造的整體思想:
決策樹說白了就好像是if-else結(jié)構(gòu)一樣,它的結(jié)果就是你要生成這個一個可以從根開始不斷判斷選擇到葉子節(jié)點的樹,但是呢這里的if-else必然不會是讓我們認(rèn)為去設(shè)置的,我們要做的是提供一種方法,計算機可以根據(jù)這種方法得到我們所需要的決策樹。這個方法的重點就在于如何從這么多的特征中選擇出有價值的,并且按照最好的順序由根到葉選擇。完成了這個我們也就可以遞歸構(gòu)造一個決策樹了
2.信息增益
劃分?jǐn)?shù)據(jù)集的最大原則是將無序的數(shù)據(jù)變得更加有序。既然這又牽涉到信息的有序無序問題,自然要想到想弄的信息熵了。這里我們計算用的也是信息熵(另一種方法是基尼不純度)。公式如下:
數(shù)據(jù)需要滿足的要求:
① 數(shù)據(jù)必須是由列表元素組成的列表,而且所有的列白哦元素都要具有相同的數(shù)據(jù)長度
② 數(shù)據(jù)的最后一列或者每個實例的最后一個元素應(yīng)是當(dāng)前實例的類別標(biāo)簽
函數(shù):
calcShannonEnt(dataSet)
計算數(shù)據(jù)集的香農(nóng)熵,分兩步,第一步計算頻率,第二部根據(jù)公式計算香農(nóng)熵
splitDataSet(dataSet, aixs, value)
劃分?jǐn)?shù)據(jù)集,將滿足X[aixs]==value的值都劃分到一起,返回一個劃分好的集合(不包括用來劃分的aixs屬性,因為不需要)
chooseBestFeature(dataSet)
選擇最好的屬性進(jìn)行劃分,思路很簡單就是對每個屬性都劃分下,看哪個好。這里使用到了一個set來選取列表中唯一的元素,這是一中很快的方法
majorityCnt(classList)
因為我們遞歸構(gòu)建決策樹是根據(jù)屬性的消耗進(jìn)行計算的,所以可能會存在最后屬性用完了,但是分類還是沒有算完,這時候就會采用多數(shù)表決的方式計算節(jié)點分類
createTree(dataSet, labels)
基于遞歸構(gòu)建決策樹。這里的label更多是對于分類特征的名字,為了更好看和后面的理解。
#coding=utf-8
import operator
from math import log
import time
def createDataSet():
dataSet=[[1,1,'yes'],
[1,1,'yes'],
[1,0,'no'],
[0,1,'no'],
[0,1,'no']]
labels = ['no surfaceing','flippers']
return dataSet, labels
#計算香農(nóng)熵
def calcShannonEnt(dataSet):
numEntries = len(dataSet)
labelCounts = {}
for feaVec in dataSet:
currentLabel = feaVec[-1]
if currentLabel not in labelCounts:
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
shannonEnt = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
shannonEnt -= prob * log(prob, 2)
return shannonEnt
def splitDataSet(dataSet, axis, value):
retDataSet = []
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis]
reducedFeatVec.extend(featVec[axis+1:])
retDataSet.append(reducedFeatVec)
return retDataSet
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0]) - 1#因為數(shù)據(jù)集的最后一項是標(biāo)簽
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0
bestFeature = -1
for i in range(numFeatures):
featList = [example[i] for example in dataSet]
uniqueVals = set(featList)
newEntropy = 0.0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet, i, value)
prob = len(subDataSet) / float(len(dataSet))
newEntropy += prob * calcShannonEnt(subDataSet)
infoGain = baseEntropy -newEntropy
if infoGain > bestInfoGain:
bestInfoGain = infoGain
bestFeature = i
return bestFeature
#因為我們遞歸構(gòu)建決策樹是根據(jù)屬性的消耗進(jìn)行計算的,所以可能會存在最后屬性用完了,但是分類
#還是沒有算完,這時候就會采用多數(shù)表決的方式計算節(jié)點分類
def majorityCnt(classList):
classCount = {}
for vote in classList:
if vote not in classCount.keys():
classCount[vote] = 0
classCount[vote] += 1
return max(classCount)
def createTree(dataSet, labels):
classList = [example[-1] for example in dataSet]
if classList.count(classList[0]) ==len(classList):#類別相同則停止劃分
return classList[0]
if len(dataSet[0]) == 1:#所有特征已經(jīng)用完
return majorityCnt(classList)
bestFeat = chooseBestFeatureToSplit(dataSet)
bestFeatLabel = labels[bestFeat]
myTree = {bestFeatLabel:{}}
del(labels[bestFeat])
featValues = [example[bestFeat] for example in dataSet]
uniqueVals = set(featValues)
for value in uniqueVals:
subLabels = labels[:]#為了不改變原始列表的內(nèi)容復(fù)制了一下
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet,
bestFeat, value),subLabels)
return myTree
def main():
data,label = createDataSet()
t1 = time.clock()
myTree = createTree(data,label)
t2 = time.clock()
print myTree
print 'execute for ',t2-t1
if __name__=='__main__':
main()
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
python利用re,bs4,requests模塊獲取股票數(shù)據(jù)
這篇文章主要介紹了python利用re,bs4,requests模塊獲取股票數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Python實現(xiàn)基于Excel數(shù)據(jù)繪制棋盤圖
這篇文章主要為大家介紹了如何根據(jù)可視化的需要,利用Python將Excel中的數(shù)據(jù)用棋盤圖的樣式來展示,文中的示例代碼簡潔易懂,需要的可以參考一下2023-07-07python中split(),?os.path.split()和os.path.splitext()的用法
本文主要介紹了python中split(),?os.path.split()和os.path.splitext()的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02