目录
一、导入相应的库
from sklearn.datasets import load_wine
import pandas as pd
from sklearn import tree
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import pandas as pd
二、查看数据
wine=load_wine()
三、数据处理和训练集划分
x=wine['data'].tolist();y=wine['target'].tolist()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.1,random_state=3)
四、模型可视化
from sklearn import tree
clf=tree.DecisionTreeClassifier(criterion='entropy',max_depth=3,random_state=3)
clf=clf.fit(x_train,y_train)
y_pred=clf.predict(x_test)
confusion_matrix(y_test,y_pred)
feature_names=[i for i in range(len(x))]
class_names=['0','1','2']
plt.figure(dpi=100,figsize=(16,9))
tree.plot_tree(clf,feature_names=feature_names,class_names=class_names,impurity=False,fontsize=10)
五、利用测试集对模型进行评估
clf.score(x_test,y_test)
结果是1.0
六、利用模型对测试集的葡萄酒就行种类鉴定
clf.predict(x_test)