This lesson on Cross-Validation — When Data is Scarce is hands-on and example-driven. You will be able to explain why cross-validation is essential for reliably comparing machine learning models when data is limited. You will know the difference between training and testing data and how k-fold cross-validation ensures every data point contributes to testing.
What You'll Be Able To Do
- Define the purpose of training and testing phases in algorithm development.
- Compare and contrast different machine learning methods using test data performance.
- Explain the mechanism of k-fold cross-validation.
- Calculate the appropriate number of training and testing blocks for 4-fold CV.
- Identify scenarios where a tuning parameter requires cross-validation for optimization.
Topics Covered in Cross-Validation — When Data is Scarce
- Introduction & Goal (0:00 - 0:45) — The lesson introduces the goal of using patient variables to predict heart disease using machine learning.
- Comparing Methods (0:45 - 1:20) — Cross-validation is introduced as the tool necessary to compare different machine learning methods like Logistic Regression or SVM.
- Training vs. Testing (1:20 - 2:15) — Data must be split to both train (estimate parameters) and test (evaluate performance) the algorithm.
- Bad Approaches (2:15 - 3:05) — Reusing all data or using a fixed, arbitrary split (e.g., 75/25) are shown to be flawed methods for evaluation.
- K-Fold Mechanism (3:05 - 4:10) — Cross-validation solves the splitting problem by systematically rotating which block of data is used for testing.
- Summarizing Results (4:10 - 4:45) — After all blocks have been tested, the overall performance is summarized to select the best performing method.
- Types of CV (4:45 - 5:20) — The number of blocks (K) is arbitrary, leading to K-fold CV (e.g., 10-fold) and Leave-One-Out CV.
- Tuning Parameters (5:20 - 5:50) — Cross-validation can also be used to optimize the value of a tuning parameter that is not estimated by the data.
SQL Cheat Sheet
-
Training— Estimate parameters to define the algorithm's shape or curveSELECT EstimateParameters(chest_pain, blood_circulation) FROM PatientData WHERE is_train = TRUE; -
Testing— Evaluate method performance on data it has not seenSELECT Predict(heart_disease) FROM PatientData WHERE is_test = TRUE; -
Cross-Validation— Compares methods by systematically rotating training and testing blocksSELECT AVG(accuracy) FROM CV_Results WHERE method = 'Logistic Regression'; -
K-Fold Cross-Validation— Divides data into K blocks, rotating one block for testingSELECT * FROM PatientData WHERE fold_id = 1; -- Test set for Fold 1 -
Leave-One-Out CV— Extreme case where each individual sample is tested aloneSELECT * FROM PatientData WHERE patient_id = 1; -- Test set of size 1 -
Tuning Parameter— A parameter value that is guessed rather than estimated by the dataSELECT BestValue(tuning_param) FROM CV_Optimization;
Comparison Table
| Data Split Approach | Training Data Used | Testing Data Used |
|---|---|---|
| Terrible Approach | 100% | 0% (Reused) |
| Simple Split (75/25) | First 75% | Last 25% |
| K-Fold Cross-Validation | K-1 blocks (e.g., 90%) | 1 block (e.g., 10%) |
Common Pitfalls
- Mistake: Reusing the same data for both training and testing the algorithm. Avoid: Always test the algorithm on data it was not trained on.
- Mistake: Assuming a single 75/25 split is the optimal way to divide the dataset. Avoid: Use cross-validation to ensure all data points contribute to testing.
- Mistake: Confusing parameter estimation with tuning parameter selection. Avoid: Use CV to find the best value for guessed tuning parameters (e.g., Ridge Regression).
- Mistake: Using a small number of folds (e.g., 2-fold) for cross-validation. Avoid: Use 10-fold CV for robust results, or LOOCV if the dataset is tiny.
FAQs
- Why is reusing training data for testing a bad idea? The algorithm will appear overly accurate because it has memorized the training data. This method fails to generalize to new, unseen data.
- What is the difference between 4-fold and 10-fold cross-validation? 4-fold divides the data into four blocks, testing on 25% at a time. 10-fold divides it into ten blocks, testing on 10% at a time.
- How do I decide which machine learning method to use? Use cross-validation to compare the performance (e.g., accuracy) of different methods on the test blocks. Select the method with the best summarized performance.
- Is Leave-One-Out CV always better since it tests every sample individually? LOOCV is computationally expensive and often yields results with high variance. 10-fold CV is generally preferred in practice.
🧪 Stratified, Group-Aware, and Time-Based Cross-Validation
Plain k-fold is only the starting point — match the splitter to the data:
- StratifiedKFold (classification): preserves the class ratio in every fold, so rare classes appear in each validation slice. Default for churn, fraud, and diagnosis tasks.
- GroupKFold / StratifiedGroupKFold: keeps all rows of one entity (user, patient, device) in a single fold. Without it, the model "recognizes" entities instead of generalizing.
- TimeSeriesSplit: rolling-origin splits for temporal data — always train on the past, validate on the future. Never shuffle timestamps.
- Leakage rule: any preprocessing (impute/scale/encode/select) goes
inside the CV loop via
Pipeline, or validation scores are optimistic.