SwadhyAI Notes

All Python notes

Grid Search 🔍

Tuning Hyperparameters

Machine Learning models have "parameters" (learned from data) and "hyperparameters" (set by you before training). Grid Search is a method to find the best combination of hyperparameters.

✨ How it Works

It creates a "grid" of all possible combinations of the hyperparameters you want to test, trains a model for each combination, and picks the best one.

It's exhaustive, meaning it tries everything.

💻 Implementation

python
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

iris = datasets.load_iris()
X = iris['data']
y = iris['target']

# Define the grid of values to search
param_grid = {'C': [0.1, 1, 10, 100], 'solver': ['liblinear', 'lbfgs']}

logit = LogisticRegression(max_iter=1000)

# Create GridSearchCV object
grid = GridSearchCV(logit, param_grid, cv=5)

# Fit the grid search
grid.fit(X, y)

print("Best Parameters:", grid.best_params_)
print("Best Score:", grid.best_score_)

💡 Did You Know?

Since Grid Search tries every combination, it can be very slow. A faster alternative is RandomizedSearchCV, which tries a random selection of combinations.