何喆379 2022-02-03 21:18 采纳率: 100%
浏览 106
已结题

机器学习决策树鸢尾花数据集,绘制决策边界,出现相同代码相同数据多次运行,结果不一致的问题

练习机器学习中,采用决策树将鸢尾花的数据进行分类,并绘制决策边界,代码如下:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
x = iris.data[:,2:]
y = iris.target
from sklearn.tree import DecisionTreeClassifier
dt_clf = DecisionTreeClassifier(max_depth = 2,criterion = 'entropy')
dt_clf.fit(x,y)

def plot_decision_boundary(model,axis):
    x0,x1 = np.meshgrid(
        np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*200)),
        np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*200))
    )
    
    x_new = np.c_[x0.ravel(),x1.ravel()]
    y_predict = model.predict(x_new)
    zz = y_predict.reshape(x0.shape)
    
    from matplotlib.colors import ListedColormap
    custom_cmap = ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])
    plt.contourf(x0,x1,zz,cmap = custom_cmap)

plot_decision_boundary(dt_clf,axis= [0.5,7.5,0,3])
plt.scatter(x[y==0,0],x[y==0,1])
plt.scatter(x[y==1,0],x[y==1,1])
plt.scatter(x[y==2,0],x[y==2,1])
plt.show() # 这个结果有点不对 ,但我又不知道哪里搞错了

第一次运行出现了下图所示的分类结果:

img

第二次及以后运行时出现了下图的分类结果:

img

我想知道明明是相同的数据,相同的代码,只是运行先后顺序不同,为什么会出现上下两个图之间的完全不同的分类结果,并且出现哪种分类结果还有一定的随机性?我的代码里也没有随机数。虽然非参数学习对于数据依赖非常严重,但是我的数据也没有发生更改啊,很奇怪。

  • 写回答

1条回答 默认 最新

  • SmallAntJ 2022-02-04 03:53
    关注

    sklearn.tree.DecisionTreeClassifier()在进行分支的时候特征选择是随机的,即使是splitter=”best”的时候。打印dt_clf.feature_importances_的话就会看到有两种不同的结果,对应两种决策边界。sklearn.tree.DecisionTreeClassifier的函数说明中明确说:

    random_state:int, RandomState instance or None, default=None
    Controls the randomness of the estimator. The features are always randomly permuted at each split, even if splitter is set to "best". When max_features < n_features, the algorithm will select max_features at random at each split before finding the best split among them. But the best found split may vary across different runs, even if max_features=n_features. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, random_state has to be fixed to an integer. See Glossary for details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分