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

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

path = clf.cost_complexity_pruning_path(Xtrain, Ytrain)
#cost_complexity_pruning_path:返回两个参数,
#第一个是CCP剪枝后决策树序列T0,T1,...,Tt对应的误差率增益率α;第二个是剪枝后决策树所有叶子节点的不纯度
ccp_alphas, impurities = path.ccp_alphas, path.impurities
print(ccp_alphas)

clfs = []
for ccp_alpha in ccp_alphas:
    clf = tree.DecisionTreeClassifier(random_state=0, ccp_alpha=ccp_alpha)
    clf.fit(Xtrain, Ytrain)
    clfs.append(clf)
#输出最后一个数的节点个数和ccp_alphas
print("Number of nodes in the last tree is: {} with ccp_alpha: {}".format(
      clfs[-1].tree_.node_count, ccp_alphas[-1]))
#绘制不同ccp_alpha取值下,clf在训练样本和测试样本上的精确度
train_scores = [clf.score(Xtrain, Ytrain) for clf in clfs]
test_scores = [clf.score(Xtest, Ytest) for clf in clfs]
from matplotlib import pyplot
plt.rcParams['savefig.dpi'] = 80 #图片像素
plt.rcParams['figure.dpi'] = 200 #分辨率
# 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600*400
fig, ax = pyplot.subplots()
ax.set_xlabel("alpha")
ax.set_ylabel("accuracy")
ax.set_title("Accuracy vs alpha for training and testing sets")
ax.plot(ccp_alphas, train_scores, marker='', label="train",
        drawstyle="steps-post")
ax.plot(ccp_alphas, test_scores, marker='', label="test",
        drawstyle="steps-post")
ax.legend()
pyplot.show()

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值