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转化成决策树

你可能感兴趣的:(python)