from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
def decision_tree_isir():
#1,导入数据
iris = load_iris()
#2.数据分类
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
#3.决策树分类器
estimator = DecisionTreeClassifier(criterion=“entropy”)
estimator.fit(x_train,y_train)
#4.模型评估
# 方法1:直接比对真实值和预估值
y_predict = estimator.predict(x_test)
print(“y_predict:\n”, y_predict)
print(“直接对比真实值和预测值:\n”, y_test == y_predict)
#方法2:计算准确率
score = estimator.score(x_test,y_test)
print(“准确率:\n”,score)
None
if name ==“main”:
decision_tree_isir()