I have created a decision tree and wanted to reduce the number of nodes.
My coding is as follows:
# Define hyperparameter grid
param_grid = {
'criterion': ['gini', 'entropy'],
'min_samples_leaf': [100, 200, 300, 400, 500, 600, 700, 800]
}
# Perform grid search with 10-fold cross-validation
model = DecisionTreeClassifier()
cv = KFold(n_splits=10, shuffle=True, random_state=42)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=cv, scoring='accuracy')
grid_search.fit(X_train, y_train);
#Fit optimal model using best params found above
optimal_model = grid_search.best_estimator_
# Visualize the optimal decision tree
plt.figure(figsize=(30, 10))
plot_tree(optimal_model, filled=True, feature_names=feature_names)
plt.show()
I have tried to use the following code to reduce the number of nodes but my juypter notebook is unable to process it.
param_grid = {
'criterion': ['gini', 'entropy'],
'min_samples_leaf': [1, 5, 10, 20, 50],
'max_depth': [5, 10, 20, 30],
'min_samples_split': [2, 5, 10, 20]
}
Can someone advise me on how to reduce the nodes?