What is Hyperparameter Tuning?
Hyperparameter Tuning is the process of finding the best settings (hyperparameters) for a machine learning model. Hyperparameters are values that we set before training. Example for a Random Forest:- GridSearchCV
- RandomizedSearchCV
1. GridSearchCV
Definition
GridSearchCV tries every possible combination of the hyperparameters we provide.Example
Suppose we want to find the best:Simple Code Example
Output
Important Terms
param_grid
Contains the hyperparameter values to test.
cv=5
Uses 5-fold cross-validation.
best_params_
Returns the best hyperparameter combination.
best_score_
Returns the best cross-validation score.
Advantages
- Tests every combination.
- Can find the best combination within the given grid.
- Easy to understand.
Disadvantages
- Can become very slow when many parameters and values are provided.
- Computationally expensive.
Remember
GridSearchCV = Try everything in the grid.
2. RandomizedSearchCV
Definition
RandomizedSearchCV randomly selects combinations from the given hyperparameter distributions. Unlike GridSearchCV, it does not test every combination.Example
Suppose we have:Code Example
Output
Important Terms
n_iter=5
Tests only 5 randomly selected combinations.
random_state=42
Makes the random selection reproducible.
best_params_
Returns the best combination found.
best_score_
Returns the best cross-validation score.
Advantages
- Faster than GridSearchCV for large search spaces.
- Can explore many possible values.
- Useful when there are many hyperparameters.
Disadvantages
- May miss the actual best combination.
- Results depend on the randomly selected combinations.
Remember
RandomizedSearchCV = Try some random combinations.
GridSearchCV vs RandomizedSearchCV
Example
Suppose:Complete Tuning Process
Easy way to remember
One-line summary
GridSearchCV exhaustively searches a specified grid, while RandomizedSearchCV randomly samples a fixed number of hyperparameter combinations.
Cross-Validation
What is Cross-Validation?
Cross-Validation is a technique used to evaluate how well a machine learning model performs on unseen data. Instead of splitting the dataset only once into training and testing data, we split the data multiple times and evaluate the model multiple times.- K-Fold Cross-Validation
- Stratified K-Fold Cross-Validation
1. K-Fold Cross-Validation
Definition
In K-Fold Cross-Validation, the dataset is divided into K equal or nearly equal parts, called folds. Each fold is used as the validation set once, while the remaining folds are used for training.Example
Suppose:Simple Diagram
Python Example
Output
Important Points
n_splits=5→ creates 5 folds.shuffle=True→ shuffles the dataset before splitting.- Every sample gets a chance to be in the validation set.
- The final score is usually the average of all fold scores.
Remember
K-Fold → Divide data into K folds and use each fold for validation once.
2. Stratified K-Fold
Definition
Stratified K-Fold is similar to K-Fold, but it maintains approximately the same class distribution in every fold. This is especially useful for classification problems.Why is it needed?
Suppose we have:Simple Diagram
Suppose:Python Example
Output
Important Points
- Maintains class proportions.
- Mainly used for classification.
- Especially useful for imbalanced datasets.
StratifiedKFoldensures each fold represents the classes properly.
Remember
Stratified K-Fold → K-Fold + preserve class distribution.
K-Fold vs Stratified K-Fold
Easy Example
Complete Cross-Validation Flow
One-line summary
K-Fold divides data into K folds, while Stratified K-Fold divides it into K folds while preserving the class distribution in each fold.