ID3(决策树算法)

分类

属于监督学习、分类算法。

代码实现

python代码的实现如下:

from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import preprocessing
from sklearn import tree
from sklearn.externals.six import StringIO

allElectronicsData = open("D:\csv_file\data.csv", "rt")
reader = csv.reader(allElectronicsData)
headers = next(reader)
print(headers)
featureList=[]
labelList=[]
for row in reader:
    labelList.append(row[len(row)-1])
    rowDict={}
    for i in range(1,len(row)-1):
        rowDict[headers[i]] = row[i]
    featureList.append(rowDict)
print(featureList)
vec=DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()
print("dummyX:"+str(dummyX))
print(vec.get_feature_names())
print("labelList:"+str(labelList))
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)
print("dummyX:"+str(dummyY))
print("type(dummyX)"+str(type(dummyX)))
#特征选择的标准,可选择基尼系数 "gini" 或者 信息熵 "entropy"
clf = tree.DecisionTreeClassifier(criterion="entropy")
#开始训练模型
clf = clf.fit(dummyX, dummyY)
print("clf:"+str(clf))
with open("D:\csv_file\output.dot","w") as f:
	#将生成的决策树打印到本地dot文件中
    f = tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file=f)
#制造数据,然后预测
oneRowX = dummyX[0,:]
print("oneRowX:"+str(oneRowX))
newRowX = oneRowX
newRowX[0]=0
newRowX[2]=1
print("newRowX:"+str(newRowX))
predictedY = clf.predict(newRowX.reshape(1,-1))
print("newRowX.reshape(1,-1):"+str(newRowX.reshape(1,-1)))
print("predictedY:"+str(predictedY))

csv中的数据:
ID3(决策树算法)_第1张图片
安装graphviz后,可以使用dot -Tpdf iris.dot -o outpu.pdf命令转化dot文件至pdf可视化决策树
生成的决策树如下:
ID3(决策树算法)_第2张图片

你可能感兴趣的:(machine,learning)