当前位置:   article > 正文

决策树后剪枝——CCP剪枝实现代码 python_python decisiontreeclassifier后剪枝

python decisiontreeclassifier后剪枝

(7条消息) decisiontreeclassifier 剪枝操作_决策树剪枝问题&python代码_weixin_39857876的博客-CSDN博客

  1. path = clf.cost_complexity_pruning_path(Xtrain, Ytrain)
  2. #cost_complexity_pruning_path:返回两个参数,
  3. #第一个是CCP剪枝后决策树序列T0,T1,...,Tt对应的误差率增益率α;第二个是剪枝后决策树所有叶子节点的不纯度
  4. ccp_alphas, impurities = path.ccp_alphas, path.impurities
  5. print(ccp_alphas)
  1. clfs = []
  2. for ccp_alpha in ccp_alphas:
  3. clf = tree.DecisionTreeClassifier(random_state=0, ccp_alpha=ccp_alpha)
  4. clf.fit(Xtrain, Ytrain)
  5. clfs.append(clf)
  6. #输出最后一个数的节点个数和ccp_alphas
  7. print("Number of nodes in the last tree is: {} with ccp_alpha: {}".format(
  8. clfs[-1].tree_.node_count, ccp_alphas[-1]))
  1. #绘制不同ccp_alpha取值下,clf在训练样本和测试样本上的精确度
  2. train_scores = [clf.score(Xtrain, Ytrain) for clf in clfs]
  3. test_scores = [clf.score(Xtest, Ytest) for clf in clfs]
  4. from matplotlib import pyplot
  5. plt.rcParams['savefig.dpi'] = 80 #图片像素
  6. plt.rcParams['figure.dpi'] = 200 #分辨率
  7. # 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600*400
  8. fig, ax = pyplot.subplots()
  9. ax.set_xlabel("alpha")
  10. ax.set_ylabel("accuracy")
  11. ax.set_title("Accuracy vs alpha for training and testing sets")
  12. ax.plot(ccp_alphas, train_scores, marker='', label="train",
  13. drawstyle="steps-post")
  14. ax.plot(ccp_alphas, test_scores, marker='', label="test",
  15. drawstyle="steps-post")
  16. ax.legend()
  17. pyplot.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/331043
推荐阅读
相关标签
  

闽ICP备14008679号