决策树后剪枝——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()

你可能感兴趣的:(决策树后剪枝——CCP剪枝实现代码 python)