資源簡介
Python實(shí)現(xiàn)對IrisData的分類,使用ID3算法
代碼片段和文件信息
#?!usr/bin/python
from?numpy?import?*
#?load?‘Iris.data‘into?our?workspace
traindata?=?loadtxt(“.\\Iris.txt“delimiter?=?‘‘usecols?=?(0123)dtype?=?float)
trainlabel?=?loadtxt(“.\\Iris.txt“delimiter?=?‘‘usecols?=?(range(45))dtype?=?str)
feaname?=?[“#0““#1““#2““#3“]??#?re-defined?feature?names
#?calculate?entropy
from?math?import?log
def?calentropy(label):
????n?=?label.size?#?the?number?of?samples
????count?=?{}?#?calculate?the?number?of?samples?belong?to?different?labels
????for?curlabel?in?label:
????????if?curlabel?not?in?count.keys():
????????????count[curlabel]?=?0
????????count[curlabel]?+=?1
????entropy?=?0
????for?key?in?count:
????????pxi?=?float(count[key])/n
????????entropy?-=?pxi*log(pxi2)
????return?entropy
#?print?(calentropy(trainlabel))
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????4127??2016-08-21?09:34??DT_ID3_IrisData\DT_ID3_IrisData.py
?????文件???????4700??2016-08-19?22:52??DT_ID3_IrisData\Iris.txt
?????目錄??????????0??2016-08-21?10:38??DT_ID3_IrisData
-----------?---------??----------?-----??----
?????????????????8827????????????????????3
評論
共有 條評論