Skip to main content

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:
We don’t know which values will give the best performance, so we try different combinations. Two common methods:
  1. GridSearchCV
  2. RandomizedSearchCV

1. GridSearchCV

Definition

GridSearchCV tries every possible combination of the hyperparameters we provide.

Example

Suppose we want to find the best:
GridSearchCV tries:
So there are:

Simple Code Example

Output

The exact best parameters can vary with the dataset and search space.

Important Terms

param_grid Contains the hyperparameter values to test.
cv=5 Uses 5-fold cross-validation.
The model is trained and evaluated multiple times. 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:
There are:
Instead of trying all 16, we can tell RandomizedSearchCV:
It randomly tests only 5 combinations.

Code Example

Output

The exact result may differ depending on the dataset and search space.

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:
Total combinations:
GridSearchCV:
RandomizedSearchCV:

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.
Two important methods:
  1. K-Fold Cross-Validation
  2. 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:
The dataset is divided into:
Then:
Finally, we calculate the average score.

Simple Diagram

Python Example

Output

The model gets an average cross-validation accuracy of approximately:

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:
If we randomly divide the data, one fold might contain:
That fold has no examples of Class 1, which can cause problems. Stratified K-Fold tries to maintain the distribution:

Simple Diagram

Suppose:
With Stratified K-Fold:
So every fold has approximately the same class ratio.

Python Example

Output

The model’s average accuracy is approximately:

Important Points

  • Maintains class proportions.
  • Mainly used for classification.
  • Especially useful for imbalanced datasets.
  • StratifiedKFold ensures 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.