1. 程式人生 > >python決策樹模型預測銷售量

python決策樹模型預測銷售量

import pandas as pd

inputfile = 'C:/Users/Administrator/Desktop/demo/data/sales_data.xls'
data = pd.read_excel(inputfile, index_col = u'序號') 
data[data == u'好'] = 1
data[data == u'是'] = 1
data[data == u'高'] = 1
data[data != 1] = -1
x = data.iloc[:,:3].as_matrix().astype(int)
y = data.iloc[:,3].as_matrix().astype(int)

from sklearn.tree import DecisionTreeClassifier as DTC
dtc = DTC(criterion='entropy') #建立決策樹模型,基於資訊熵
dtc.fit(x, y) #訓練模型

#匯入相關函式,視覺化決策樹
#匯出結果為.dot檔案,需要安裝Graphviz才能轉化為pdf
from sklearn.tree import export_graphviz
x = pd.DataFrame(x)
from sklearn.externals.six import StringIO
x = pd.DataFrame(x)
with open("tree.dot", 'w') as f:
  f = export_graphviz(dtc, feature_names = x.columns, out_file = f)
#在匯出的檔案中新增兩行程式碼,用於識別中文字型  edge[fontname=”SimHei”]  node[fontname=”SimHei”]
#需要安裝Graphviz轉化成決策樹