1. 程式人生 > >機器學習:sklearn&pydotplus實現Decision Tree

機器學習:sklearn&pydotplus實現Decision Tree

import csv
from sklearn.feature_extraction import DictVectorizer
from sklearn import preprocessing
from sklearn import tree
import pydotplus

'''
資料集 play.csv
RID	age	income	student	credit_rating	Class_buys_computer
1	youth	high	no	fair	no
2	youth	high	no	excellent	no
3	middle_aged	high	no	fair	yes
4	senior	medium	no	fair	yes
5	senior	low	yes	fair	yes
6	senior	low	yes	excellent	yes
7	middle_aged	low	yes	excellent	no
8	youth	medium	no	fair	yes
9	youth	low	yes	fair	no
10	senior	medium	yes	fair	yes
11	youth	medium	yes	excellent	yes
12	middle_aged	medium	no	excellent	yes
13	middle_aged	high	yes	fair	yes
14	senior	medium	no	excellent	no
'''

file = open("E:\\play.csv", 'rt', encoding='utf-8')
reader = csv.reader(file)

'''
headers = reader.next() 報錯
python csv2libsvm.py: AttributeError: '_csv.reader' object has no attribute 'next'
This is because of the differences between python 2 and python 3.
Use the built-in function next in python 3.
That is, write next(reader) instead of reader.next()
'''

headers = next(reader)
print("表頭資訊\n" + str(headers))

feature_list,result_list = [],[]
for row in reader:
    result_list.append(row[-1])
    feature_list.append(dict(zip(headers[1:-1],row[1:-1])))

print("結果\n"+str(result_list),"\n特徵值\n"+str(feature_list))

vec = DictVectorizer() # 將dict型別的list資料,轉換成numpy array
DummyX = vec.fit_transform(feature_list).toarray()
DummyY = preprocessing.LabelBinarizer().fit_transform(result_list)
#注意,dummyX是按首字母排序的
print("DummyX\n"+str(DummyX),"\nDummyY\n"+str(DummyY))

clf = tree.DecisionTreeClassifier(criterion="entropy",random_state=0)
# clf = tree.DecisionTreeClassifier()
clf = clf.fit(DummyX,DummyY)

print("clf\n"+str(clf))

#輸出dot檔案
with open("E:\\play.dot","w") as f:
    f = tree.export_graphviz(clf,out_file=f)

print( '特徵向量\n',vec.get_feature_names() )

# help(tree.export_graphviz)
dot_data = tree.export_graphviz(clf,
                                feature_names=vec.get_feature_names(),
                                special_characters=True,
                                filled=True, rounded=True,
                                out_file=None,)
print("dot_data\n"+str(dot_data))

'''
pydotplus 畫句子的依存結構樹
pip install pydotplus 安裝不上
pip install --upgrade --ignore-installed pydotplus 可以安裝上
pydotplus.graphviz.InvocationException: GraphViz's executables not found
這是《機器學習升級版III》中“決策樹隨機森林實踐”章節的問題。
解決方法:conda install graphviz ,安裝完成,重啟IDE整合開發工具
先安裝GraphViz軟體,將GraphViz解壓後的目錄新增到環境變數path裡,然後pip 安裝pydotplus,按照這個順序
安裝,如果還不行,重啟一下ide或者電腦就行了
'''

graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("E:\\play.pdf")

#根據特徵向量可知:0.0.1.|0.1.|1.0.0.|1.0.表示youth,fair,high,no
oneRowX=dummyX[0]
twoRowX=dummyX[1]
print("oneRowX:\n",str(oneRowX),"\ntwoRowX\n",str(twoRowX))

#進行預測
A = ([[0,0,1,0,1,1,0,0,1,0]])
B = ([[1,0,0,0,1,1,0,0,1,0]])

predict_A = clf.predict(A)
predict_B = clf.predict(B)
print("predict_A",str(predict_A),"predict_B",str(predict_B))