...

/

Random Search Using the Random Forest Algorithm

Random Search Using the Random Forest Algorithm

Learn how to apply the random search method to find the best hyperparameters for a random forest model.

A practical example of the random search method

This is the second example of how we can use the random search method to optimize the hyperparameters of the ML model.

In this second example, we’ll use the random forest algorithm to determine which combination of hyperparameter values will produce the best results compared to the results obtained by using the default values for the hyperparameters.

Press + to interact

What will we learn?

In the Jupyter Notebook, we’ll learn to:

  • Create and train an ML model (random forest regression).

  • Measure the performance of the ML model.

  • Perform the steps required to implement the random search method.

  • Identify a combination of hyperparameters that provide the best results.

Import important packages

First, we import the important Python packages that will do the following tasks:

  • Create and train an ML model (random forest algorithm).

  • Check the ML model’s performance.

  • Implement the random search method.

  • Identify a combination of hyperparameters that provide the best results.

Press + to interact
# import important modules
import numpy as np
import pandas as pd
# sklearn modules
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import MinMaxScaler
import warnings
warnings.filterwarnings("ignore")
# seeding
np.random.seed(123)
...