from six import StringIO
from sklearn.feature_extraction import DictVectorizer
from sklearn import tree, preprocessing
import numpy as np
import pydotplus
# 特征必须是字典形式 由于indictor 的标识是y n所以在决策树决策时候,必须把两个状态都输入进,所以在33行预测时候,预测的
# 数组也必须含有indictor = y ,indictor =n 并标识预测的数的状态是什么才能准确预测不然会报输入的特征数小于原特征数的错误
l1 = [
{"类别": "公务员", "学历": "本科", "性别": "女", "颜值": "高"},
{"类别": "公司", "学历": "专科", "性别": "男", "颜值": "中"},
{"类别": "事业编", "学历": "本科", "性别": "女", "颜值": "低"},
{"类别": "公司", "学历": "专科", "性别": "男", "颜值": "高"},
{"类别": "事业编", "学历": "专科", "性别": "女", "颜值": "中"},
{"类别": "公司", "学历": "本科", "性别": "女", "颜值": "高"},
]
# 特征的预测数值
l1_1 = [
{"类别": "事业编", "学历": "本科", "性别": "女", "颜值": "低"},
]
# 工资结果列表
l2 = [
20000, 3000, 2500, 10000, 5000, 30000
]
# 特征提取器并转换成预测数组
ve = DictVectorizer()
label = preprocessing.LabelEncoder()
trainx = ve.fit_transform(l1).toarray()
# 打印标签编码
print(trainx)
trainy = np.array(l2)
# 选择决策树 选择标准类型
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf.fit(trainx, trainy)
t = clf.predict(ve.transform(l1_1))
# 输出预测值
print(t)
# 输出特征值
print(clf.feature_importances_)
"""
可视化
"""
dot_data = StringIO()
tree.export_graphviz(
clf,
feature_names=ve.get_feature_names(),
filled=True,
rounded=True,
special_characters=True,
out_file=dot_data
)
# dot_data.getvalue().replace('helvetica', '"Microsoft YaHei"') 可以显示中文。
graph = pydotplus.graph_from_dot_data(dot_data.getvalue().replace('helvetica', '"Microsoft YaHei"'))
graph.write_pdf("demo.pdf")
*******************************************************************************************************************************************
需要提前准备的工作
1 .import graphviz
2. windows中手动下载 graphviz 安装后并 指定环境变量入path. 重启pycharm即可
………………………………………………………………………………………………………………………………………………………………………………………………
entropy 是信息熵
samples 是样本数量
value 是 概率值即 有多少个结果,多少个样本对应着该结果。(结果值小->大)