导入模块
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
加载数据
iris = load_iris()
X = iris.data
y = iris.target
标准的2/3数据集训练,1/3数据集训练
from sklearn.model_selection import train_test_split
train_X,test_X,train_y,test_y = train_test_split(X,y,test_size=0.33,random_state=123)
clf = DecisionTreeClassifier()
clf.fit(train_X,train_y)
计算正确率
from sklearn.metrics import accuracy_score,confusion_matrix
predicted = clf.predict(test_X)
accuracy = accuracy_score(test_y, predicted)
accuracy
计算混淆矩阵
m = confusion_matrix(test_y, predicted)
m
输出预测信息报表
from sklearn.metrics import classification_report
pri