机器学习-决策树实战

机器学习-决策树实战

代码

from sklearn import tree
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
import pandas as pd
import matplotlib.pyplot as plt
import graphviz
wine= load_wine()
print(wine.keys())
#特征矩阵和目标值
print(wine.data.shape,wine.target)
df = pd.DataFrame(wine.data,columns=wine.feature_names)
print(df)
df['target'] = wine.target
print(df)
X_train,X_test,Y_train,Y_test = train_test_split(wine.data,wine.target,test_size=0.3)
print(X_train.shape)

clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(X_train,Y_train)
score = clf.score(X_test,Y_test)
print(score)

dt_data = tree.export_graphviz(clf,
                     feature_names=wine.feature_names,
                     class_names=['琴酒','雪莉','贝尔摩德'],
                     filled=True,
                     rounded=True)
grap = graphviz.Source(dt_data)
print(grap)

plt.figure(figsize=(6,8),facecolor='white',dpi=300)
# 设置字体的属性
# plt.rcParams["font.sans-serif"] = "Arial Unicode MS"
plt.rcParams["font.sans-serif"] = "SimHei"
plt.rcParams["axes.unicode_minus"] = False

tree.plot_tree(clf,
               feature_names=wine.feature_names,
               class_names=['琴酒','雪莉','贝尔摩德'],
               filled=True,
               rounded=True)
plt.show()
zip = [*zip(wine.feature_names,clf.feature_importances_)]
print(zip)

决策树图

机器学习-决策树实战_第1张图片

你可能感兴趣的:(机器学习,matplot代码绘图,机器学习,决策树,python)