决策树的后剪枝python
时间: 2023-10-15 22:02:52
浏览: 88
决策树的后剪枝是一种用于减小决策树过拟合的技术。在Python中,可以使用scikit-learn库来实现决策树的后剪枝。
下面是一个使用后剪枝的决策树示例:
```python
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
# 假设你已经有了训练数据和标签
X_train, X_test, y_train, y_test = train_test_split(train_data, train_labels, test_size=0.2)
# 创建决策树分类器
dt = DecisionTreeClassifier()
# 使用训练数据拟合决策树模型
dt.fit(X_train, y_train)
# 进行后剪枝
dt.cost_complexity_pruning_path(X_train, y_train)
# 获取不同 alpha 值对应的树模型
ccp_alphas, impurities = dt.cost_complexity_pruning_path(X_train, y_train)
dts = []
for ccp_alpha in ccp_alphas:
dt = DecisionTreeClassifier(ccp_alpha=ccp_alpha)
dt.fit(X_train, y_train)
dts.append(dt)
# 选择最优模型
train_s
```