One of the most common things to do when working with the machine learning model is performing Hyperparameter Tuning.
But, what is Hyperparatameter Tuning?🤔
Defining it in one line will be a search for a model's best settings.🔎
However, let’s take a step back to understand what hyperparameter is.
A hyperparameter is a configuration setting set before the learning process begins. It controls how a machine learning model is trained.
Unlike model parameters (which are learned from the data, like weights in a neural network), hyperparameters are defined manually and guide the training process itself.
💡Example: The
RandomForestClassifier
in Scikit-learn have has few hyperparameters such asn_estimators
,max_depth
, andmax_features
.
Hyperparameter tuning involves exploring all possible parameters input into the model's hyperparameters to find the “best.” By best, we mean the model performance metrics we previously set, such as accuracy, precision, recall, etc.
Let’s take a look at the Python code implementation for Hyperparameter Tuning.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier(random_state=42)
param_grid = {
'n_estimators': [10, 50, 100],
'max_depth': [2, 4, 6],
'max_features': ['sqrt', 'log2']
}
grid_search = GridSearchCV(estimator=model,
param_grid=param_grid,
cv=5,
scoring='accuracy',
n_jobs=-1)
grid_search.fit(X_train, y_train)
print("Best Hyperparameters:", grid_search.best_params_)
print("Best CV Score:", grid_search.best_score_)
In the code above, we explore the RandomForestClassifier
model Hyperparameter using the GridSearchCV
from Scikit-Learn.
Sounds simple, right? We need to include all the possible parameters, and we will obtain the best hyperparameters for the model.
Well…..It’s more than that. 👀
In fact, much of the standard Hyperparameter Tuning is only wasting time and computational power.
Applying the right strategy for Hyperparameter Tuning then becomes important so we don’t end up with a subpar model that takes too many resources.
So, why is Your Hyperparameter Tuning wasting Time, and how can you improve it? Let’s explore them together!
Don’t forget to subscribe to get access to these amazing pro tips! 👇👇👇
Why do you do Hyperparameter Tuning wrong?
Most people do hyperparameter tuning inefficiently — using too much compute, time, and money — for very little gain.
There are four main reasons why hyperparameter tuning becomes time-consuming and computationally exhaustive. Let’s see all the traps that many practitioners fall into.
Here is a short summary infographic for you to learn from👇
Keep reading with a 7-day free trial
Subscribe to Non-Brand Data to keep reading this post and get 7 days of free access to the full post archives.