python代碼實現(xiàn)ID3決策樹算法
更新時間:2017年12月20日 14:35:08 作者:史帥
這篇文章主要為大家詳細介紹了python代碼實現(xiàn)ID3決策樹算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)ID3決策樹算法的具體代碼,供大家參考,具體內(nèi)容如下
'''''
Created on Jan 30, 2015
@author: 史帥
'''
from math import log
import operator
import re
def fileToDataSet(fileName):
'''''
此方法功能是:從文件中讀取樣本集數(shù)據(jù),樣本數(shù)據(jù)的格式為:數(shù)據(jù)以空白字符分割,最后一列為類標(biāo)簽
參數(shù):
fileName:存放樣本集數(shù)據(jù)的文件路徑
返回值:
dataSet:樣本集數(shù)據(jù)組成的二維數(shù)組
'''
file=open(fileName, mode='r')
lines=file.readlines()
dataSet=[]
index=0
p=re.compile(r"\s+")
for line in lines:
line=p.split(line.strip())
dataSet.append(line)
index+=1
return dataSet
def calculateShannonEntropy(dataSet):
'''''
此方法功能是:計算樣本集數(shù)據(jù)類別的信息熵,樣本數(shù)據(jù)的格式為二維數(shù)組
參數(shù):
dataSet:樣本集數(shù)據(jù)組成的二維數(shù)組
返回值:
shannonEntropy:樣本集數(shù)據(jù)類別的信息熵
'''
dataCount=len(dataSet)
classCountDic={}
for data in dataSet:
label=data[-1]
if label not in classCountDic.keys():
classCountDic[label]=0
classCountDic[label]+=1
shannonEntropy=0.0
for key in classCountDic:
prob=float(classCountDic[key])/dataCount
shannonEntropy-=prob*log(prob,2)
return shannonEntropy
def splitDataSet(dataSet,axis,value):
'''''
此方法功能是:對樣本集數(shù)據(jù)按照某一特征進行分割,使得分割后的數(shù)據(jù)集中該特征的值全部等于同一個值,并且將分割后的數(shù)據(jù)中該特征列去除
參數(shù):
dataSet:待分割的樣本集數(shù)據(jù),二維數(shù)組
axis:特征所在樣本集數(shù)據(jù)列中的位置
value:樣本集數(shù)據(jù)分割后該特征的值
返回值:
splitedDataSet:按照所在位置為axis的特征進行分割,并且該特征值為value的樣本集數(shù)據(jù)的子集
'''
splitedDataSet=[]
for data in dataSet:
if data[axis]==value:
splitedData=data[:axis]
splitedData.extend(data[axis+1:])
splitedDataSet.append(splitedData)
return splitedDataSet
def chooseBestFeatureToSlipt(dataSet):
'''''
此方法功能是:分別計算整個樣本集數(shù)據(jù)的信息熵與按照各個特征分割后的數(shù)據(jù)集的信息熵之差,得到使差值最大的分割方案,得到該分割方案的特征
參數(shù):
dataSet:待分割的樣本集數(shù)據(jù),二維數(shù)組
返回值:
bestFeature:按照分割前后信息熵差值最大的分割方案得到的特征,返回此特征所在樣本集數(shù)據(jù)列中的位置
'''
bestFeature=-1
dataSetShannonEntropy=calculateShannonEntropy(dataSet)
infoGain=0
featureCount=len(dataSet[0])-1
for i in range(featureCount):
featureList=[example[i] for example in dataSet]
featureSet=set(featureList)
splitedDataSetShannonEntropy=0
for feature in featureSet:
splitedDataSet=splitDataSet(dataSet,i,feature)
splitedDataSetShannonEntropy+=float(len(splitedDataSet))/len(dataSet)*calculateShannonEntropy(splitedDataSet)
if dataSetShannonEntropy-splitedDataSetShannonEntropy>infoGain:
infoGain=dataSetShannonEntropy-splitedDataSetShannonEntropy
bestFeature=i
return bestFeature
def majorityClass(classList):
'''''
此方法功能是:從類別列表中得到個數(shù)最多的類別
參數(shù):
classList:類別列表,一維數(shù)組
返回值:
類別列表中個數(shù)最多的類別
'''
classCountDic={}
for label in classList:
if label not in classCountDic.keys():
classCountDic[label]=0
classCountDic[label]+=1
classCountDic=sorted(classCountDic.item(),key=operator.itemgetter(1),reverse=True)
return classCountDic[0][0]
def createTree(dataSet,features):
'''''
此方法功能是:根據(jù)訓(xùn)練樣本集數(shù)據(jù)創(chuàng)建對分類最有效的決策樹
參數(shù):
dataSet:訓(xùn)練樣本集數(shù)據(jù),二維數(shù)組
features:與訓(xùn)練樣本集數(shù)據(jù)中各列的特征值相對應(yīng)的特征名稱集合,一維數(shù)組
返回值:
tree:根據(jù)訓(xùn)練樣本集數(shù)據(jù)所創(chuàng)建的,對分類最有效的決策樹
'''
subFeatures=features[:]
classList=[example[-1] for example in dataSet]
if classList.count(classList[0])==len(classList):
return classList[0]
if len(dataSet[0])==1:
return majorityClass(classList)
bestFeature=chooseBestFeatureToSlipt(dataSet)
label=subFeatures[bestFeature]
tree={label:{}}
del(subFeatures[bestFeature])
featureList=[example[bestFeature] for example in dataSet]
featureSet=set(featureList)
for feature in featureSet:
splitedDataSet=splitDataSet(dataSet,bestFeature,feature)
tree[label][feature]=createTree(splitedDataSet, subFeatures)
return tree
def classify(inX,tree,features):
'''''
此方法功能是:根據(jù)創(chuàng)建好的決策樹,對特定的數(shù)據(jù)進行分類
參數(shù):
inX:待分類的數(shù)據(jù),特征值向量,一維數(shù)組
tree:根據(jù)決策樹算法創(chuàng)建好的最有效的決策樹
features:與訓(xùn)練樣本集數(shù)據(jù)中各列的特征值相對應(yīng)的特征名稱集合,一維數(shù)組
返回值:
label:待分類的數(shù)據(jù)通過決策樹分類之后的類別
'''
feature=list(tree.keys())[0]
featureIndex=features.index(feature)
secondTree=tree[feature][inX[featureIndex]]
if type(secondTree).__name__=="dict":
label=classify(inX,secondTree,features)
else:
label=secondTree
return label
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲實現(xiàn)方法
這篇文章主要介紹了以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲實現(xiàn)方法,Pyspider是一個開源項目、用Python語言編寫十分簡潔且具有爬蟲程序的代表性,需要的朋友可以參考下2015-03-03
Windows10+anacond+GPU+pytorch安裝詳細過程
這篇文章主要介紹了Windows10+anacond+GPU+pytorch安裝詳細過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Python2和Python3中print的用法示例總結(jié)
在Python 3中接觸的第一個很大的差異就是縮進是作為語法的一部分,這和C++等其他語言確實很不一樣,所以要小心,其中python3和python2中print的用法有很多不同,這篇文章主要給大家介紹了關(guān)于Python2和Python3中print用法的相關(guān)資料,需要的朋友可以參考下。2017-10-10
Python中查看變量的類型內(nèi)存地址所占字節(jié)的大小
這篇文章主要介紹了Python中查看變量的類型,內(nèi)存地址,所占字節(jié)的大小,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
Python基于釘釘監(jiān)控發(fā)送消息提醒的實現(xiàn)
本文主要介紹了Python基于釘釘監(jiān)控發(fā)送消息提醒的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python中parsel兩種獲取數(shù)據(jù)方式小結(jié)
本文主要介紹了Python中parsel兩種獲取數(shù)據(jù)方式小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

