python基于ID3思想的決策樹
這是一個判斷海洋生物數(shù)據(jù)是否是魚類而構(gòu)建的基于ID3思想的決策樹,供大家參考,具體內(nèi)容如下
# 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'], [0,0,'maybe']] 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ù)集的最后一項是標簽 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ù)屬性的消耗進行計算的,所以可能會存在最后屬性用完了,但是分類 # 還是沒有算完,這時候就會采用多數(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)容復制了一下 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()
最后我們測試一下這個腳本即可,如果想把這個生成的決策樹用圖像畫出來,也只是在需要在腳本里面定義一個plottree的函數(shù)即可。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vscode autopep8無法格式化python代碼問題解決
這篇文章主要為大家介紹了vscode autopep8無法格式化python代碼問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09python神經(jīng)網(wǎng)絡(luò)Densenet模型復現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Densenet模型復現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05使用paramiko遠程執(zhí)行命令、下發(fā)文件的實例
下面小編就為大家?guī)硪黄褂胮aramiko遠程執(zhí)行命令、下發(fā)文件的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Python光學仿真wxpython透鏡演示系統(tǒng)初始化與參數(shù)調(diào)節(jié)
這篇文章主要為大家介紹了Python光學仿真wxpython透鏡演示系統(tǒng)的初始化與參數(shù)調(diào)節(jié),同樣在學習wxpython透鏡演示系統(tǒng)的入門同學可以借鑒參考下,希望能夠有所幫助2021-10-10Python re.findall中正則表達式(.*?)和參數(shù)re.S使用
本文主要介紹了Python re.findall中正則表達式(.*?)和參數(shù)re.S使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08