EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsgridsearchcvcross-validation

Hyperparameter Tuning with Grid Search, and Cross-Validation

Every classifier so far was trained with hyperparameters chosen by hand. GridSearchCV tries every combination and reports which one actually scored best, cross-validated.

Ashish Revar25 October 20267 min read

Every classifier in this unit so far was trained with hyperparameters chosen by hand, not discovered from the data. GridSearchCV automates that search: it trains and cross-validates a model once for every combination of hyperparameters you specify, and reports the combination that scored best.

svm_grid_search.py

from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV

hyperparam_grid = {
    "C": [0.1, 1, 3, 9, 27],
    "gamma": [0.001, 0.01, 0.1, 1, 10],
}
search = GridSearchCV(SVC(kernel="rbf"), hyperparam_grid, cv=4)
search.fit(X_train, y_train)

print(f"Best C: {search.best_params_['C']}")
print(f"Best gamma: {search.best_params_['gamma']}")
print(f"Best cross-validated accuracy: {search.best_score_:.3f}")

Sample output

Best C: 1
Best gamma: 0.1
Best cross-validated accuracy: 0.880
Cgamma0.1139270.0010.010.11100.630.640.630.630.630.650.860.880.630.630.760.870.860.630.630.820.870.820.630.630.830.850.820.630.63
The real cross-validated accuracy of every (C, gamma) combination tested above. The best cell is outlined in red. Notice the plateau: several neighbouring combinations score almost as well, which is the normal shape of a well-tuned hyperparameter surface, not a sign the search failed

Cross-Validation: Why One Split Is Not Enough

A single train/test split gives one accuracy number, which depends partly on which rows happened to land in the test set. $k$-fold cross-validation trains and evaluates the model $k$ times, each time holding out a different fold as the test set, and reports the mean and standard deviation across all $k$ runs, a far more honest picture of how the model actually performs.

cross_validation.py

from sklearn.model_selection import cross_val_score

scores = cross_val_score(forest, X_train, y_train, cv=5)
print(f"CV accuracy: {scores.mean():.3f} (+/- {scores.std():.3f})")

Caution: A Single Split Can Flatter or Punish a Model by Luck

A model evaluated on only one train/test split can look stronger or weaker than it really is, purely because of which examples landed in the test fold. A standard deviation above roughly 0.03 to 0.05 across folds is a sign the dataset is small relative to its difficulty, or that the model is unstable, worth investigating before trusting the mean score at all.

Related to this topic

Continue learning

Reference material

eBook

ML for Cyber Security eBook

v1.0

Open resource

Cheatsheet

ML for Cyber Security Cheatsheet

v1.0

Open resource

MCQ Bank

ML for Cyber Security MCQ Bank

v1.0

Open resource

Question Bank

ML for Cyber Security Question Bank

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

Hyperparameter Tuning with Grid Search, and Cross-Validation

Hi! I'm EZ Mentor

Your AI tutor for
Hyperparameter Tuning with Grid Search, and Cross-Validation

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ LearnEZ Learn

Structured learning across Reverse Engineering, Cloud Security, and Research Methodology, plus games and practice tools in EZ Arena. Free, and open to anyone.

EZ Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Talks
  • EZ Bytes
  • Cyber Hygiene

EZ Library

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • MCQ Banks
  • Cheatsheets

EZ Exams

  • All Exams
  • REMA
  • Cloud Security
  • Research Methodology
  • Python for Cyber Security
  • Cyber Law & IPR
  • Community
  • EZ Board

EZ Arena

  • EZ Games
  • Live Quiz
  • ML Challenges
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice

Platform

  • About
  • Contact
  • Terms of Use
  • Disclaimer
  • Verify Certificate
  • Events

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar