資源簡介
ID3算法Python代碼和數(shù)據(jù)集
代碼片段和文件信息
#-*-?coding:?utf-8?-*-
#使用ID3決策樹算法預(yù)測銷量高低
import?pandas?as?pd
#參數(shù)初始化
inputfile?=?‘G:/code/data/sales_data.xls‘
data?=?pd.read_excel(inputfile?index_col?=?u‘序號‘)?#導(dǎo)入數(shù)據(jù)
#類別標(biāo)簽‘好’,‘是’,‘高’?賦值為1,其他為-1
data[data?==?u‘好‘]?=?1
data[data?==?u‘是‘]?=?1
data[data?==?u‘高‘]?=?1
data[data?!=?1]?=?-1
x?=?data.iloc[::3].as_matrix().astype(int)#把數(shù)據(jù)中的前三列數(shù)據(jù)賦給x
y?=?data.iloc[:3].as_matrix().astype(int)#目標(biāo)數(shù)據(jù)賦給y
print(y)
print(x)
from?sklearn.tree?import?DecisionTreeClassifier?as?DTC
dtc?=?DTC(criterion=‘entropy‘)?#建立基于信息熵的決策樹模型
dtc.fit(x?y)?#訓(xùn)練模型
#導(dǎo)入相關(guān)函數(shù),可視化決策樹
#導(dǎo)出dot文件,利用Graphviz轉(zhuǎn)換為pdf或者png
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?featur
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????27136??2015-12-05?21:05??sales_data.xls
?????文件???????1057??2017-06-26?11:09??decision_tree.py
-----------?---------??----------?-----??----
????????????????28193????????????????????2
評論
共有 條評論