您的位置:首页 > 编程语言

深度基础学习:Decision Tree 代码实现

2016-05-11 19:31 555 查看
python+anaconda

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

from sklearn.feature_extraction import DictVectorizer

import xlrd

from sklearn import preprocessing

from sklearn import tree

from sklearn.externals.six import StringIO

data = xlrd.open_workbook('/home/sanmao/workspace/test2.xls')

table = data.sheet_by_index(0)

print table.ncols

print table.nrows

print table.cell(0,1).value

print table.row_values(0)

featureList = []

labelList = []

for row in range(1,table.nrows):

labelList.append(table.cell(row,table.ncols-1).value)

rowDict = {}

for i in range(1,table.ncols-1):

rowDict[table.cell(0,i).value] = table.cell(row,i).value

featureList.append(rowDict)

print labelList

print featureList

#Vectorizer featureList

vec = DictVectorizer()

dummyX = vec.fit_transform(featureList).toarray()

print dummyX

#Vectorizer labelList

lb = preprocessing.LabelBinarizer()

dummyY = lb.fit_transform(labelList)

print dummyY

#entropy xinxisahng

clf = tree.DecisionTreeClassifier(criterion='entropy')

clf = clf.fit(dummyX,dummyY)

print clf

with open("/home/sanmao/workspace/test2.dot","w") as f:

f = tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file = f)

oneRowX = dummyX[0,:]

#print oneRowX

#newRowX = oneRowX

#print newRowX

#oneRowX[0] = 1

#oneRowX[2] = 0

#print newRowX

#print oneRowX

print oneRowX

newRowX = oneRowX

newRowX[0] = 1

newRowX[1] = 0

print newRowX

predicredY = clf.predict(newRowX)

print predictedY
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: