Cross-validation — more honest than a single split
Single train/test split = lucky or unlucky. 5-fold CV runs 5 different splits and averages — more honest.
script.py
v3.1
Python output
click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
The output will appear here.
🧠 How it works
Why CV? A single train/test split is one sample from many possible splits. You might get lucky (easy test set) or unlucky (hard test set). 5-fold CV runs 5 different splits and averages — more honest estimate. KFold(n_splits=5, shuffle=True) — split into 5 pieces, shuffle first. cross_validate(model, X, y, cv=cv, scoring=...) — runs the CV, returns a dict of arrays. Each metric is an array of 5 values (one per fold). Report MEAN ± STD. Std tells you how stable the model is — high std = sensitive to the data split. neg_ prefix: sklearn maximizes scores, so errors are negated (maximizing -MAE = minimizing MAE).
✏️ Try these changes
-
Change n_splits from 5 to 10→ 10-fold CV — more folds, slower, slightly more accurate -
Change shuffle from True to False→ no shuffling — if data is sorted, CV is biased -
Change n_estimators from 50 to 200→ RF R² might improve -
Add return_train_score=True and compare train vs test R²→ if train >> test, model is overfitting