|
利用python實現決策時程序,最后輸出整個決策過程的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'數據地址','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))
復制代碼
|
-
-
decision_tree.zip
2018-2-1 15:54 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
884 Bytes, 下載次數: 7, 下載積分: 黑幣 -5
決策樹python程序
|