# pylint: disable=no-member
import torch
from sklearn import tree
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
import pandas as pd
import graphviz
import matplotlib.pyplot as plt
wine1=load_wine()
print(wine1.data.shape)
print(pd.concat([pd.DataFrame(wine1.data),pd.DataFrame(wine1.target)],axis=1))
print(wine1.feature_names)
print(wine1.target_names)
x_train,x_test,y_train,y_test =train_test_split(wine1.data,
wine1.target,test_size=0.2)
print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
# 模型主干部分
clf=tree.DecisionTreeClassifier(criterion='gini'
,splitter='random'
,max_depth=4
,min_samples_split=15)#gini entropy
# (random_state)降低随机性,最后得分不变
# (splitter) best and random
# max_depth
# min_samples_leaf =10 这个节点不能小于10
# min_samples_split 至少有25,才能分裂出子节点
clf=clf.fit(x_train,y_train)
score = clf.score(x_test,y_test)
print("*"*100)
print("score:{}".format(score))
print("*"*100)
dot_data = tree.export_graphviz(clf
,class_names=['0:琴酒','1:苦艾酒','2:雪莉']
,filled=True
,rounded=True
)
graph = graphviz.Source(dot_data)
graph.render('test-output/round-table.gv', view=True)
# plt.imshow(graph)
# print(dot_data)
print(graph)
print([*zip(wine1.feature_names,clf.feature_importances_)])
'''
# 可视化调参,遍历法调参可以试一下
test = []
for i in range(10):
clf=tree.DecisionTreeClassifier(criterion='entropy'#gini entropy
,splitter='random'
,max_depth=i+1
,min_samples_split=15)
# (random_state)降低随机性,最后得分不变
# (splitter) best and random
# max_depth
# min_samples_leaf =10 这个节点不能小于10
# min_samples_split 至少有25,才能分裂出子节点
clf=clf.fit(x_train,y_train)
score = clf.score(x_test,y_test)
test.append(score)
plt.plot(range(1,11),test,color="r",label="max_depth")
plt.legend()
plt.show() '''
# apply返回每个测试样本所在的叶子节点的索引
# preidict返回每个测试样本的分类结果
print("*"*100)
print(clf.apply(x_test))
# print(clf.preidict(x_test))
调参,应该来说可视化会舒服一些,不过也建议能画三维图,同时两个参数变量,应该会更好些。当然后续我会再写一个回归的例子,尽量用到网格搜索和交叉验证,感兴趣的话可以一起学习一下。
决策树样子:
等大佬提建议,解决中文乱码,睡觉,顶不住了。