Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeImplementationSupervised LearningClassificationPre-pruning Decision Tree – GridSearch for Hyperparameter tuning
Classification Knowledge Base

Pre-pruning Decision Tree – GridSearch for Hyperparameter tuning

March 8, 2024March 8, 2024CEO 181 views

Grid search is a tuning technique that attempts to compute the optimum values of hyperparameters. It is an exhaustive search that is performed on the specific parameter values of a model. The parameters of the estimator/model used to apply these methods are optimized by cross-validated grid-search over a parameter grid.

from google.colab import drive
drive.mount('/content/drive')
 
functions_path = '/content/drive/MyDrive/Lib/'
%run {functions_path}myfunctions.ipynb

from sklearn.model_selection import GridSearchCV

# Choose the type of classifier.
estimator = DecisionTreeClassifier(random_state=1)

# Grid of parameters to choose from
parameters = {'max_depth': np.arange(1,10),
              'min_samples_leaf': [1, 2, 5, 7, 10,15,20],
              'max_leaf_nodes' : [2, 3, 5, 10],
              'min_impurity_decrease': [0.001,0.01,0.1]
             }

# Type of scoring used to compare parameter combinations
acc_scorer = metrics.make_scorer(metrics.recall_score)

# Run the grid search
grid_obj = GridSearchCV(estimator, parameters, scoring=acc_scorer, cv=5)
grid_obj = grid_obj.fit(X_train, y_train)

# Set the clf to the best combination of parameters
clf= grid_obj.best_estimator_

# Fit the best algorithm to the data.
clf.fit(X_train, y_train)
make_confusion_matrix(clf, X_test, y_test)
get_accuracy_and_recall_score(clf, X_train, X_test, y_train, y_test)
get_feature_importances_and_visualize(clf, X_test)
confusion matrix, decision tree, gridsearch, pre-pruning

Post navigation

Previous Post
Previous post: Pre-pruning Decision Tree – depth restricted
Next Post
Next post: Post-pruning Decision Tree with Cost Complexity Parameter ccp_alpha

You Might Also Like

No image
RandomizedSearchCV vs GridSearchCV
April 24, 2024 Comments Off on RandomizedSearchCV vs GridSearchCV
No image
BaggingClassifier from Scikit-Learn
April 7, 2024 Comments Off on BaggingClassifier from Scikit-Learn
No image
Parameter cv in GridSearchCV
March 10, 2024 Comments Off on Parameter cv in GridSearchCV
No image
Post-pruning Decision Tree with Cost Complexity Parameter…
March 8, 2024 Comments Off on Post-pruning Decision Tree with Cost Complexity Parameter ccp_alpha
No image
Pre-pruning Decision Tree – depth restricted
March 8, 2024 Comments Off on Pre-pruning Decision Tree – depth restricted
  • Recent
  • Popular
  • Random
  • No image
    8 months ago Low-Rank Factorization
  • No image
    8 months ago Perturbation Test for a Regression Model
  • No image
    8 months ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    March 10, 2024Choosing the right estimator
  • No image
    April 7, 2024BaggingClassifier from Scikit-Learn
  • No image
    January 30, 2024Univariate Analysis in EDA
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS