Is there an efficient way to handle pruning in Decision Tree with Python ?
Currently I'm doing that:
def do_best_tree(Xtrain, ytrain, Xtest, ytest):
clf = DecisionTreeClassifier()
clf.fit(Xtrain, ytrain)
path = clf.cost_complexity_pruning_path(Xtrain, ytrain)
ccp_alphas = path.ccp_alphas
clfs = []
for ccp_alpha in tqdm(ccp_alphas):
clf = DecisionTreeClassifier(ccp_alpha=ccp_alpha)
clf.fit(Xtrain, ytrain)
clfs.append(clf)
return max(clfs, key=lambda x:x.score(Xtest, ytest))
But it's super slow (as I create and fit a lot of trees).
Is there a more efficient way to do this with scikit-learn, or another library that handle this ?