Machine Learning
Cross Validation
Estimate performance with k-fold and grid search.
By EZ4Code Team
cross-validationgrid-search
Code
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import (cross_val_score, StratifiedKFold,
GridSearchCV, RandomizedSearchCV)
from sklearn.datasets import load_iris
from scipy.stats import randint
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier(random_state=42)
# K-fold CV
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=cv, scoring="accuracy")
print(f"acc: {scores.mean():.3f} +/- {scores.std():.3f}")
# Grid search
param_grid = {
"n_estimators": [50, 100, 200],
"max_depth": [None, 5, 10],
}
grid = GridSearchCV(model, param_grid, cv=cv, scoring="accuracy", n_jobs=-1)
grid.fit(X, y)
print("best:", grid.best_params_, grid.best_score_)
# Randomized search over a wider space
rand = RandomizedSearchCV(model, {"n_estimators": randint(50, 300),
"max_depth": randint(3, 20)},
n_iter=20, cv=cv, random_state=42, n_jobs=-1)
rand.fit(X, y)Explanation
cross_val_score runs k-fold CV and returns per-fold scores whose mean and std summarize performance. StratifiedKFold preserves class balance in each fold for classification. GridSearchCV exhaustively evaluates a parameter grid while RandomizedSearchCV samples it, often reaching a good solution faster on large grids.
More Machine Learning Snippets
Data Preprocessing
Scale, encode, and impute features with sklearn.
Train Test Split
Split data into training and evaluation sets.
Classification
Train and predict with common classifiers.
Regression
Fit regressors and evaluate with RMSE and R2.
Clustering
Cluster with KMeans and DBSCAN.
Metrics
Evaluate classifiers with confusion matrix and reports.