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 LearningClassificationParameter cv in GridSearchCV
Classification

Parameter cv in GridSearchCV

March 10, 2024March 10, 2024CEO 205 views

In scikit-learn’s GridSearchCV (Grid Search Cross Validation), the parameter cv stands for “cross-validation.” It determines the cross-validation splitting strategy to be used when evaluating the performance of a machine learning model.

When cv is set to an integer (e.g., cv=5), it represents the number of folds in a (Stratified) K-Fold cross-validation. For example, cv=5 means that the dataset will be divided into 5 equal-sized folds, and the model training and evaluation will be performed 5 times. Each time, one of the folds will be used as the test set, and the remaining folds will be used as the training set.

Here’s an example of using GridSearchCV with a decision tree classifier and 5-fold cross-validation:

from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris

# Load the iris dataset
iris = load_iris()

# Create a decision tree classifier
clf = DecisionTreeClassifier()

# Define the parameter grid to search
param_grid = {'max_depth': [2, 3, 4, 5]}

# Create a GridSearchCV object with 5-fold cross-validation
grid_search = GridSearchCV(clf, param_grid, cv=5)

# Fit the model with the cross-validated grid search
grid_search.fit(iris.data, iris.target)

# Print the best parameters found during the grid search
print("Best Parameters:", grid_search.best_params_)

In this example, the model will be trained and evaluated 5 times (5-fold cross-validation) for each combination of hyperparameters specified in the param_grid. The GridSearchCV will then identify the best combination of hyperparameters based on the average performance across all folds.

Adjusting the value of cv can impact the robustness of the model evaluation. A higher number of folds can provide a more stable estimate of the model’s performance but may also require more computational resources. Conversely, a lower number of folds may be faster but could be more sensitive to the specific split of the data. It’s a trade-off between computational cost and the reliability of the evaluation.

cross validation, cv, gridsearch

Post navigation

Previous Post
Previous post: NumPy function argsort
Next Post
Next post: NumPy function argmax

You Might Also Like

No image
RandomizedSearchCV vs GridSearchCV
April 24, 2024 Comments Off on RandomizedSearchCV vs GridSearchCV
No image
Pre-pruning Decision Tree – GridSearch for Hyperparameter…
March 8, 2024 Comments Off on Pre-pruning Decision Tree – GridSearch for Hyperparameter tuning
  • Recent
  • Popular
  • Random
  • No image
    7 months ago Low-Rank Factorization
  • No image
    7 months ago Perturbation Test for a Regression Model
  • No image
    7 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
    February 22, 2024What is Uniform Distribution?
  • No image
    February 29, 2024One-Hot Encoding
  • 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)
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS