import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
irisdata=load_iris()
irisdata.data
irisdata.target
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.scatter(irisdata.data[:,2],irisdata.data[:,3],c=irisdata.target)
plt.xlabel("花瓣长度")
plt.ylabel("花瓣宽度")
x=irisdata.data[irisdata.target!=0]
y=irisdata.target[irisdata.target!=0]
x_train,x_test,y_train,y_target=train_test_split(x,y,test_size=0.3,random_state=40)
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
classifier=DecisionTreeClassifier(max_depth = 2)
clf=classifier.fit(x_train,y_train)
tree.plot_tree(clf)
y_pred = classifier.predict(x_test)
acc=accuracy_score(y_pred,y_target)
print("the accuarcy of prediction is", acc)
plt.scatter(x_test[:,2], x_test[:,3], c=y_target)
plt.show()
plt.scatter(x_test[:,2], x_test[:,3], c=y_pred)
plt.show()
import os
os.environ["PATH"]+= os.pathsep + 'E:\ProgramData\Anaconda3\bin\Graphviz\bin'
from sklearn import tree
tree.export_graphviz(classifier,out_file="tree.dot" )
import pydotplus
from IPython.display import Image
dot_data=tree.export_graphviz(classifier, out_file=None,feature_names=irisdata.feature_names,filled=True, rounded=True,special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
|