Find Best Hyperparameters
Advanced · Write the code · from Evaluating ML Models: Cross-Validation, Metrics, and Overfitting
From the lesson: Evaluating ML Models: Cross-Validation, Metrics, and Overfitting
You've trained a model and it gets 95% accuracy. Should you celebrate? Not yet. That number might be lying to you. Maybe your test set was too easy. Maybe the model memorized the training data. Maybe accuracy is the wrong metric entirely.
Read the lessonUse GridSearchCV to find the best max_depth for a DecisionTreeClassifier from the values [2, 3, 4, 5, 6, 7]. Use 5-fold CV and scoring='accuracy'. Print the best max_depth value, then the best CV score rounded to 4 decimal places on a new line.
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=200, n_features=6,
random_state=42)
# Use GridSearchCV to find the best max_depth
More Model Evaluation exercises
- Run Cross-ValidationAdvanced
- Detect OverfittingAdvanced
- Compute Precision, Recall, and F1Advanced
- Complete Evaluation PipelineAdvanced
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz