Summary - Cross-validation
- Machine Learning course
- Intermediate
- Lesson 18/112
- 6 min
In this group you learned k-fold cross-validation and cross_val_score.
Summary: cross-validation
In this group:
- CV removes the luck of a single split; the result is mean ± standard deviation.
KFold,StratifiedKFold,GroupKFold,TimeSeriesSplit: the splitter must mimic the real use of the data.cross_val_score,cross_validate(multiple metrics, training score),cross_val_predict(out-of-fold predictions).- Repeated CV gives stability, nested CV an honest score including selection.
Even after CV, the test set is used only once, at the very end.
Practice Time series CV
Walk through the folds of TimeSeriesSplit(n_splits=4) for a 20-step X. On each line print the training set size and the first and last value of the validation indices.
Starter code
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.arange(20).reshape(-1, 1)
# buraya yazSample solution
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.arange(20).reshape(-1, 1)
for egitim, dogrulama in TimeSeriesSplit(n_splits=4).split(X):
print(len(egitim), dogrulama[0], dogrulama[-1])Expected output
4 4 7 8 8 11 12 12 15 16 16 19
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is the right splitter for data with several transactions per customer?
- KFold
- LeaveOneOut
- No split needed
- GroupKFold (by customer id) Correct answer
Explanation: If a customer's transactions land in different folds, the model memorises the customer.
2 Use the splitter that keeps the time order.
cv = ____(n_splits=5)
cv = TimeSeriesSplit(n_splits=5)
Explanation: TimeSeriesSplit trains only on the past in every fold.
3 Match the outputs.
| Term | Meaning |
|---|---|
cross_val_score |
Array of scores |
cross_validate |
Dict |
cross_val_predict |
Array of predictions |
get_n_splits |
Number of folds |