Today you learn:
How to improve ML model performance systematically 🚀
This is VERY important for:
- Interviews
- Real projects
- Kaggle competitions
- Production ML
🎯 Goal of Day-13
You will:
✅ Understand hyperparameters
✅ Tune ML models
✅ Use GridSearchCV
✅ Find best model settings
🧠 What is Hyperparameter?
Simple meaning:
Settings you choose BEFORE training model
Example:
RandomForestClassifier(n_estimators=100)
👉 n_estimators is hyperparameter.
Model learns:
weights, patterns
But YOU choose:
number of trees
depth
learning rate
🧠 Why Hyperparameter Tuning Matters
Same model:
| Setting | Accuracy |
|---|---|
| Default | 75% |
| Tuned | 90% |
👉 Better settings = better model.
🚀 Part 1 – Import Libraries
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
🚀 Part 2 – Create Dataset
data = {
"Hours": [1,2,3,4,5,6,7,8],
"Marks": [40,45,50,55,70,80,90,95],
"Pass": [0,0,0,0,1,1,1,1]
}
df = pd.DataFrame(data)
X = df[["Hours", "Marks"]]
y = df["Pass"]
🚀 Part 3 – Train/Test Split
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
random_state=42
)
🚀 Part 4 – Create Model
model = RandomForestClassifier()
🚀 Part 5 – Define Hyperparameters
params = {
"n_estimators": [5, 10, 20],
"max_depth": [2, 4, 6]
}
🧠 Meaning
| Parameter | Meaning |
|---|---|
| n_estimators | Number of trees |
| max_depth | Tree depth |
🚀 Part 6 – GridSearchCV
grid = GridSearchCV(
estimator=model,
param_grid=params,
cv=3
)
🧠 What Happens?
GridSearch tries ALL combinations:
5 trees + depth 2
5 trees + depth 4
10 trees + depth 2
...
Then selects BEST model.
🚀 Part 7 – Train Grid Search
grid.fit(X_train, y_train)
🚀 Part 8 – Best Parameters
print("Best Params:", grid.best_params_)
🚀 Part 9 – Best Model Prediction
best_model = grid.best_estimator_
y_pred = best_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
🧠 Real AI Insight
Without tuning:
average model
With tuning:
optimized model
🧠 Important Concept – Cross Validation
In GridSearch:
cv=3
Means:
- Train/test multiple times
- More reliable evaluation
⚠ Important Interview Question
Q:
What is GridSearchCV?
Answer:
A technique to automatically test multiple hyperparameter combinations and select the best model using cross-validation.
🧠 Real AI Insight
This is used heavily in:
- Kaggle competitions
- Production ML
- Enterprise AI optimization
🎯 End of Day-13 Goals
You now:
✅ Understand hyperparameters
✅ Tune ML models
✅ Use GridSearchCV
✅ Improve performance systematically
0 Comments
If you have any queries, please let me know. Thanks.