机器学习:sklearn&pydotplus实现Decision Tree

本文通过使用Python的sklearn库,详细介绍了如何处理CSV数据,应用DictVectorizer进行特征转换,利用LabelBinarizer进行标签二值化,最终训练并可视化决策树分类器的过程。文章还涉及了pydotplus和GraphViz的安装与使用,以及如何解决常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import csv
from sklearn.feature_extraction import DictVectorizer
from sklearn import preprocessing
from sklearn import tree
import pydotplus

'''
数据集 play.csv
RID	age	income	student	credit_rating	Class_buys_computer
1	youth	high	no	fair	no
2	youth	high	no	excellent	no
3	middle_aged	high	no	fair	yes
4	senior	medium	no	fair	yes
5	senior	low	yes	fair	yes
6	senior	low	yes	excellent	yes
7	middle_aged	low	yes	excellent	no
8	youth	medium	no	fair	yes
9	youth	low	yes	fair	no
10	senior	medium	yes	fair	yes
11	youth	medium	yes	excellent	yes
12	middle_aged	medium	no	excellent	yes
13	middle_aged	high	yes	fair	yes
14	senior	medium	no	excellent	no
'''

file = open("E:\\play.csv", 'rt', encoding='utf-8')
reader = csv.reader(file)

'''
headers = reader.next() 报错
python csv2libsvm.py: AttributeError: '_csv.reader' object has no attribute 'next'
This is because of the differences between python 2 and python 3.
Use the built-in function next in python 3.
That is, write next(reader) instead of reader.next()
'''

headers = next(reader)
print("表头信息\n" + str(headers))

feature_list,result_list = [],[]
for row in reader:
    result_list.append(row[-1])
    feature_list.append(dict(zip(headers[1:-1],row[1:-1])))

print("结果\n"+str(result_list),"\n特征值\n"+str(feature_list))

vec = DictVectorizer() # 将dict类型的list数据,转换成numpy array
DummyX = vec.fit_transform(feature_list).toarray()
DummyY = preprocessing.LabelBinarizer().fit_transform(result_list)
#注意,dummyX是按首字母排序的
print("DummyX\n"+str(DummyX),"\nDummyY\n"+str(DummyY))

clf = tree.DecisionTreeClassifier(criterion="entropy",random_state=0)
# clf = tree.DecisionTreeClassifier()
clf = clf.fit(DummyX,DummyY)

print("clf\n"+str(clf))

#输出dot文件
with open("E:\\play.dot","w") as f:
    f = tree.export_graphviz(clf,out_file=f)

print( '特征向量\n',vec.get_feature_names() )

# help(tree.export_graphviz)
dot_data = tree.export_graphviz(clf,
                                feature_names=vec.get_feature_names(),
                                special_characters=True,
                                filled=True, rounded=True,
                                out_file=None,)
print("dot_data\n"+str(dot_data))

'''
pydotplus 画句子的依存结构树
pip install pydotplus 安装不上
pip install --upgrade --ignore-installed pydotplus 可以安装上
pydotplus.graphviz.InvocationException: GraphViz's executables not found
这是《机器学习升级版III》中“决策树随机森林实践”章节的问题。
解决方法:conda install graphviz ,安装完成,重启IDE集成开发工具
先安装GraphViz软件,将GraphViz解压后的目录添加到环境变量path里,然后pip 安装pydotplus,按照这个顺序
安装,如果还不行,重启一下ide或者电脑就行了
'''

graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("E:\\play.pdf")

#根据特征向量可知:0.0.1.|0.1.|1.0.0.|1.0.表示youth,fair,high,no
oneRowX=dummyX[0]
twoRowX=dummyX[1]
print("oneRowX:\n",str(oneRowX),"\ntwoRowX\n",str(twoRowX))

#进行预测
A = ([[0,0,1,0,1,1,0,0,1,0]])
B = ([[1,0,0,0,1,1,0,0,1,0]])

predict_A = clf.predict(A)
predict_B = clf.predict(B)
print("predict_A",str(predict_A),"predict_B",str(predict_B))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值