🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #207

Model Selection

Model selection is the broader decision of which algorithm — and which hyperparameter settings for it — to actually use, combining algorithm choice and hyperparameter tuning into one coherent process rather than treating them separately.

The Full Model Selection Process

StepWhat Happens
1Shortlist candidate algorithms based on the problem type and data characteristics
2For each candidate, tune its hyperparameters via cross-validation
3Compare each algorithm's best-tuned cross-validated performance
4Select the overall best-performing algorithm + hyperparameter combination
5Evaluate that final choice exactly once, on the untouched test set

Comparing Multiple Algorithms Fairly

from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

candidates = {
    "logreg": (LogisticRegression(max_iter=1000), {"C": [0.1, 1, 10]}),
    "svm": (SVC(), {"C": [0.1, 1, 10], "gamma": [0.01, 0.1]}),
    "random_forest": (RandomForestClassifier(random_state=42), {"n_estimators": [100, 200], "max_depth": [None, 10]}),
}

best_overall = None
for name, (model, param_grid) in candidates.items():
    search = GridSearchCV(model, param_grid, cv=5, scoring="f1")
    search.fit(X_train, y_train)
    print(f"{name}: best CV F1 = {search.best_score_:.3f}, params = {search.best_params_}")
    if best_overall is None or search.best_score_ > best_overall[1]:
        best_overall = (name, search.best_score_, search.best_estimator_)

print("Selected model:", best_overall[0])

Every candidate is compared using the same cross-validation scheme and metric — a fair, apples-to-apples comparison, avoiding the mistake of tuning one algorithm carefully while leaving another at its defaults.

Why Algorithm Choice and Hyperparameter Tuning Can't Be Fully Separated

An untuned SVM might genuinely underperform a well-tuned logistic regression, even if SVM would have won with proper tuning — comparing algorithms fairly requires tuning each one reasonably well first, not comparing default settings against each other.

Practical Use Cases

  • Choosing between fundamentally different algorithm families for a new problem, not just tuning one already-chosen algorithm
  • Documenting and justifying a model choice with a clear, reproducible comparison process

Common Mistakes

  • Comparing algorithms at their default hyperparameters, unfairly favoring whichever one happens to have better defaults for this specific problem.
  • Selecting the final model based on test-set performance across multiple candidates — this reuses the test set for selection, contaminating its role as an honest, final estimate.

Interview Relevance

Q: "How would you fairly compare Random Forest and SVM for a new classification problem?" Tune each algorithm's hyperparameters via the same cross-validation scheme and scoring metric, compare their best cross-validated scores, and only then evaluate the single winning combination once on the held-out test set — comparing untuned defaults would unfairly bias the comparison toward whichever algorithm happens to have better default settings.

Practice Question

You tune Random Forest carefully via grid search but only try SVM at its default settings, then conclude Random Forest is the better algorithm for your problem. What's wrong with this comparison?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Model Selection – FAQs

Quick answers about learning Model Selection in Machine Learning.

This free note from CodingNow 2.0 explains Model Selection in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Model Selection, is 100% free with no signup required.
With focused practice, most students grasp Model Selection in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now