# 机器学习（一）：决策树算法及使用python构造一个决策树

1585人阅读 评论(0)

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

from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import preprocessing
from sklearn import tree
from sklearn.externals.six import StringIO

allData = open("E:\eclipse_file\Deeplearning\data\decisionTree.csv", "rU")

featureList = []
lableList = []
lableList.append(row[len(row)-1])
rowDic = {}
for i in range(1,len(row)-1):
featureList.append(rowDic)
print("featureList:"+str(featureList))
print("lablelist:"+str(lableList))
vec = DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()

print("dummyX:"+str(dummyX))
print("get_feature_names():"+str(vec.get_feature_names()))

lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(lableList)
print("dummyY:"+str(dummyY))

clf = tree.DecisionTreeClassifier(criterion="entropy")
clf = clf.fit(dummyX, dummyY)
print("clf:"+str(clf))

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

oneRow = dummyX[0]
print("oneRow:"+str(oneRow))
oneRow[0] = 1
oneRow[2] = 0
predictY = clf.predict(oneRow)
print("predictY:"+str(predictY))

0
0

个人资料
• 访问：184013次
• 积分：2530
• 等级：
• 排名：第17073名
• 原创：110篇
• 转载：6篇
• 译文：0篇
• 评论：32条
最新评论