决策树在线文档:https://scikit-learn.org/stable/modules/tree.html
安装Graphviz : http://www.graphviz.org/
1.下载
2.安装:双击
3.创建桌面快捷方式
安装目录\bin文件夹\:找到gvedit.exe文件右键 发送到桌面快捷方式,如下图:
4.配置环境变量
将graphviz安装目录下的bin文件夹添加到Path环境变量中:
5.验证是否安装并配置成功
进入windows命令行界面,输入dot -version
,然后按回车,如果显示graphviz的相关版本信息,则安装配置成功。如图:
6.python环境中安装:(pycharm中)
File->Settings->Project:Python
然后输入graphivz安装
安装需要等待一会。。。。
决策树实战代码
# -*- 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
#read the csv file
allElectronicsDate = open(r'D:\Python\date\AllElectronics.csv','rt')
reader = csv.reader(allElectronicsDate)
headers = next(reader)
# headers = reader.next()
print(headers)#打印输出第一行标题
#['RID', 'age', 'income', 'student', 'credit_rating', 'Class_buys_computer']
featureList = [] #用来存储特征值
labelList = [] #用来存储类标签
#获取特征值并打印输出
for row in reader:
labelList.append(row[len(row) - 1])#每一行最后的值,类标签
rowDict = {}
for i in range(1,len(row) - 1):#每一行 遍历除第一列和最后一列的值
rowDict[headers[i]] = row[i]
featureList.append(rowDict)
print(featureList)
#vectorize feature 使用sklearn自带的方法将特征值离散化为数字标记
vec = DictVectorizer()
dummyX = vec.fit_transform(featureList).toarray()
print("dummyY:" + str(dummyX))
print(vec.get_feature_names())
# print("feature_name" + str(vec.get_feature_names()))
print("labelList:" + str(labelList))
#vectorize class labels #数字化类标签
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(labelList)
print("dummyY:" + str(dummyY))
#use the decision tree for classification
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummyX,dummyY) #构建决策树
#打印构建决策树采用的参数
print("clf:" + str(clf))
#visilize the model
with open("allElectronicInformationGainOri.dot",'w') as f:
f=tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file=f)
#这时就生成了allElectronicInformationGainOri.dot文件
# dot -Tpdf in.dot -o out.pdf dot文件输出为pdf文件
#验证数据,取出一行数据,修改几个属性预测结果
oneRowX = dummyX[0,:]
print("oneRowX:" + str(oneRowX))
newRowX = oneRowX
newRowX[0] = 1
newRowX[2] = 0
print("newRowX:" + str(newRowX))
predictedY = clf.predict(newRowX)
print("predictedY:"+str(predictedY))
结果:
['RID', 'age', 'income', 'student', 'credit_rating', 'class_buys_computer']
[{'income': 'high', 'age': 'youth', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'high', 'age': 'youth', 'student': 'no', 'credit_rating': 'excellent'}, {'income': 'high', 'age': 'middle_aged', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'senior', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'senior', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'low', 'age': 'middle_aged', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'medium', 'age': 'youth', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'youth', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'youth', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'medium', 'age': 'middle_aged', 'student': 'no', 'credit_rating': 'excellent'}, {'income': 'high', 'age': 'middle_aged', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'no', 'credit_rating': 'excellent'}]
dummyY:[[ 0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]
[ 0. 0. 1. 1. 0. 1. 0. 0. 1. 0.]
[ 1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]
[ 0. 1. 0. 0. 1. 0. 0. 1. 1. 0.]
[ 0. 1. 0. 0. 1. 0. 1. 0. 0. 1.]
[ 0. 1. 0. 1. 0. 0. 1. 0. 0. 1.]
[ 1. 0. 0. 1. 0. 0. 1. 0. 0. 1.]
[ 0. 0. 1. 0. 1. 0. 0. 1. 1. 0.]
[ 0. 0. 1. 0. 1. 0. 1. 0. 0. 1.]
[ 0. 1. 0. 0. 1. 0. 0. 1. 0. 1.]
[ 0. 0. 1. 1. 0. 0. 0. 1. 0. 1.]
[ 1. 0. 0. 1. 0. 0. 0. 1. 1. 0.]
[ 1. 0. 0. 0. 1. 1. 0. 0. 0. 1.]
[ 0. 1. 0. 1. 0. 0. 0. 1. 1. 0.]]
['age=middle_aged', 'age=senior', 'age=youth', 'credit_rating=excellent', 'credit_rating=fair', 'income=high', 'income=low', 'income=medium', 'student=no', 'student=yes']
labelList:['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
dummyY:[[0]
[0]
[1]
[1]
[1]
[0]
[1]
[0]
[1]
[1]
[1]
[1]
[1]
[0]]
clf:DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=None,
max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
random_state=None, splitter='best')
oneRowX:[ 0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]
newRowX:[ 1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]
predictedY:[1]
在项目路径里面打开dot文件
将dot文件转化为直观的PDF文件(dos 里面输入dot -Tpdf D:\Python\机器学习\allElectronicInformationGainOri.dot -o D:\Python\机器学习\out.pdf 然后回车)
dot -Tpdf D:\Python\机器学习\allElectronicInformationGainOri.dot -o D:\Python\机器学习\out.pdf