標(biāo)題:
決策樹實(shí)現(xiàn)python程序
[打印本頁]
作者:
suncaixin
時間:
2018-2-1 15:54
標(biāo)題:
決策樹實(shí)現(xiàn)python程序
利用python實(shí)現(xiàn)決策時程序,最后輸出整個決策過程的pdf文件
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 22 13:13:07 2017
@author: suncaixin
"""
#collect number
from sklearn.feature_extraction import DictVectorizer
import numpy as np
import pandas as pd
import csv
from sklearn import tree
from sklearn import preprocessing
allElectrionicsData=open(r'數(shù)據(jù)地址','rt')
reader=csv.reader(allElectrionicsData)
headers=next(reader)
print(headers)
feature_list=[]
label_list=[]
for row in reader:
label_list.append(row[len(row)-1])
rowDict={}
for i in range(1,len(row)-1):
print(row[i])
rowDict[headers[i]]=row[i]
print('rowDict:',rowDict)
feature_list.append(rowDict)
print(feature_list)
#tranform feature
vec=DictVectorizer()
dunmyX=vec.fit_transform(feature_list).toarray()
print('dunmyX:',str(dunmyX))
print(vec.get_feature_names())
#class label transform
lb=preprocessing.LabelBinarizer()
dunmyY=lb.fit_transform(label_list)
print('dunmyY:',str(dunmyY))
#decision tree
clf=tree.DecisionTreeClassifier(criterion='entropy')
clf=clf.fit(dunmyX,dunmyY)
print('clf',str(clf))
#visulize model
with open('allElectronicInformationGain.dot','w') as f:
f=tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file=f)
#output pdf:dot -Tpdf C:\Users\suncaixin\allElectronicInformationGain.dot -o outpu.pdf
#predict
oneRowx=dunmyX[0,:]
newRowx=oneRowx
newRowx[0]=1
newRowx[2]=0
predictedY=clf.predict([newRowx])
print('predicted:',str(predictedY))
復(fù)制代碼
decision_tree.zip
2018-2-1 15:54 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
884 Bytes, 下載次數(shù): 7, 下載積分: 黑幣 -5
決策樹python程序
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1