OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to reduce the node in a decision tree model

  • Thread starter Thread starter unleasehed
  • Start date Start date
U

unleasehed

Guest
I have created a decision tree and wanted to reduce the number of nodes.

My coding is as follows:

Code:
# 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()

enter image description here

I have tried to use the following code to reduce the number of nodes but my juypter notebook is unable to process it.

Code:
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?
<p>I have created a decision tree and wanted to reduce the number of nodes.</p>
<p>My coding is as follows:</p>
<pre><code># 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()
</code></pre>
<p><a href="https://i.sstatic.net/kEiBlBtb.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kEiBlBtb.png" alt="enter image description here" /></a></p>
<p>I have tried to use the following code to reduce the number of nodes but my juypter notebook is unable to process it.</p>
<pre><code>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]
}
</code></pre>
<p>Can someone advise me on how to reduce the nodes?</p>
 

Latest posts

G
Replies
0
Views
1
Gamal Othman
G
Top