学习中心
登录
已解决
决策树生成不一致

决策树跟老师的结果不一样 马士兵教育官网 - IT职业领路人 (mashibing.com)

在这里老师让我下载iris数据集Iris - UCI 机器学习存储库根据老师给的网址,

905d6ef9abfaee48e2103c3e3c3c676f.png

我再次进行拟合

from ucimlrepo import fetch_ucirepo
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import tree
import matplotlib.pyplot as plt
import pandas as pd
iris = fetch_ucirepo(id=53)
# data (as pandas dataframes)
x = iris.data.features  # <class 'pandas.core.frame.DataFrame'>
y = iris.data.targets  # <class 'pandas.core.frame.DataFrame'>
print(x)
print(y)
y = y.replace({
    'Iris-setosa': 0,
    'Iris-versicolor': 1,
    'Iris-virginica': 2
})

# 打印转换后的结果
print(y)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=256)
model = DecisionTreeClassifier(criterion='gini')
# 用训练模型的数据进行拟合模型
model.fit(x_train, y_train)
# 用测试集数据检查模型拟合的得分与结果再与真实的测试集结果对比
print('测试数据得分:', model.score(x_test, y_test))
print('算法预测的结果:', model.predict(x_test))
print('真实结果是:   ', y_test)
# 对决策树进行可视化
plt.rcParams['font.family'] = 'STKaiti'  # 设置字体
plt.figure(figsize=(12, 16))  # 设置图片尺寸
tree.plot_tree(model, filled=True, feature_names=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], rounded=True, class_names=['0', '1', '2'])
plt.show()

得到的结果仍是

f8deea1b3c09503e6704bed13ec06175.png

与老师在视频中的还是不一致,怎么回事?

41 1
    1个回答
    写回答