๐ Building a model is not enough.
We must also make sure it performs well on unseen data.
This is done using:
โ Cross Validation
โ Hyperparameter Tuning
๐น 1. What is Cross Validation?
Cross Validation checks how well a model generalizes to new data.
๐ Instead of using only one train-test split, data is divided multiple times.
๐ฅ 2. K-Fold Cross Validation โญ
How it Works:
1๏ธโฃ Split data into K parts (folds)
2๏ธโฃ Use one fold for testing
3๏ธโฃ Use remaining folds for training
4๏ธโฃ Repeat until every fold is tested
โ Example
If K = 5:
โข 4 folds โ Training
โข 1 fold โ Testing
Repeated 5 times.
๐น 3. Why Cross Validation is Important?
โ Better model evaluation
โ Reduces overfitting risk
โ More reliable accuracy
๐น 4. Implementation (Python)
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
scores = cross_val_score(model, X, y, cv=5)
print(scores)
๐ฅ 5. What are Hyperparameters?
๐ Hyperparameters are settings controlled before training the model.
Examples:
โ Number of trees in Random Forest
โ Value of K in KNN
โ Learning rate
๐น 6. Hyperparameter Tuning
๐ Finding the best settings for the model.
๐ฅ 7. Grid Search โญ
Grid Search tries multiple parameter combinations automatically.
from sklearn.model_selection import GridSearchCV
โ Example
params = {
"n_neighbors": [3,5,7]
}
๐ Tests different K values in KNN.
๐น 8. Why Tuning is Important?
โ Improves model performance
โ Increases accuracy
โ Helps build optimized ML systems
๐ฏ Todayโs Goal
โ Understand cross validation
โ Learn K-Fold method
โ Understand hyperparameters
โ Learn Grid Search basics
๐ฌ Tap โค๏ธ for more!