Cross-validation — k-fold
5.6.5· Coding › Machine Learning (Aerospace Applications)
Core Problem: Overfitting vs Generalization
Jab hum aerospace applications ke liye ML models train karte hain (engine failures predict karna, flight paths optimize karna, sensor data mein anomalies detect karna), hume ek fundamental tension face karna padta hai:
WHY hume validation ki zaroorat hai? Kyunki training accuracy ek jhoothi hoti hai. Ek model training data memorize kar sakta hai (100% training accuracy achieve karke) jabki naye data pe bahut bura perform karta hai. Aerospace mein yeh sirf bura nahi — yeh dangerous hai.
WHAT hai naive approach? Ek single test set hold out karo. Lekin iske teen fatal flaws hain:
- Sample bias: Tumhara test set real-world conditions represent nahi kar sakta
- Variance: Ek train-test split ek performance number deta hai — kya yeh reliable hai?
- Data waste: Aerospace mein labeled data expensive hota hai (sensor failures, flight logs). Sirf testing ke liye 20% use karna precious information waste karta hai.
HOW k-fold CV yeh fix karta hai? Test set ko systematically rotate karke.

Cross-Validation Estimate Derive Karna
Chalte hain math scratch se build karte hain.
Given: Dataset with samples:
Goal: Apne learning algorithm ka true generalization error estimate karna.
Step 1: Data partition karo
ko disjoint subsets (folds) mein approximately equal size ke saath divide karo:
jahan aur for .
Yeh step kyun? Hume independent test sets chahiye. Disjoint folds ensure karte hain ki koi data leakage na ho.
Step 2: k models train karo
Har fold ke liye:
- Training set: (fold ko chhodkar sabhi data)
- Test set:
Model train karo:
Yeh step kyun? Har model training ke liye data ka hissa dekhta hai, jo full dataset pe training ko approximate karta hai (ek single 80-20 split ke unlike).
Step 3: Har model evaluate karo
Held-out fold pe performance metric (accuracy, RMSE, F1, etc.) compute karo:
Yeh step kyun? Har fold ek alag "future data" scenario represent karta hai. Model ne training ke dauran fold nahi dekha, toh yeh ek fair test hai.
Step 4: Scores aggregate karo
k-fold CV estimate test scores ka mean hai:
k mein Bias-Variance Tradeoff
Chhota (e.g., ):
- Bias: Zyada. Har training set mein sirf data ka hissa hota hai, full dataset pe true performance underestimate karta hai.
- Variance: Kam. Average karne ke liye kam models, lekin har test set bada hai (zyada stable estimates).
- Computation: Faster (sirf 3 models train karne hain).
Bada (e.g., , LOOCV):
- Bias: Kam. Training set mein samples hain, full dataset ke almost identical.
- Variance: Zyada. Test sets mein sirf 1 sample each hota hai; individual scores noisy hote hain. Saath hi, training sets heavily overlap karte hain (independent nahi), toh scores correlated hote hain.
- Computation: Sabse slow ( models train karne hain).
ya kyun? Empirical studies (Kohavi 1995) dikhate hain ki yeh values zyaadatar datasets ke liye bias aur variance achhi tarah balance karti hain. Limited data wale aerospace applications mein, common hai.
Worked Example: Aircraft Engine Failure Predict Karna
Scenario: Tumhare paas 1000 labeled flight records hain. Har record mein 50 sensor features hain (temperature, vibration, pressure) aur ek binary label: engine 100 flight hours mein fail hua (1) ya nahi (0). Tum ek Random Forest classifier train kar rahe ho.
Task: Model performance estimate karne ke liye 5-fold CV use karo.
Implementation Steps
Step 1: Shuffle aur split karo
from sklearn.model_selection import KFold
import numpy as np
X = sensor_data # Shape: (1000, 50)
y = failure_labels # Shape: (1000,)
kf = KFold(n_splits=5, shuffle=True, random_state=42)
# shuffle=True ensure karta hai ki folds sirf sequential blocks na hon
# (e.g., 2019 ka sabhi data fold 1 mein, 2020 mein fold 2...)Shuffle kyun? Agar data time ya flight route ke by sorted hai, toh sequential folds biased honge. Shuffling ensure karta hai ki har fold ek random sample ho.
Step 2: Train aur evaluate karo
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score
scores = []
for train_idx, test_idx in kf.split(X):
X_train, X_test = X[train_idx], X[test_idx]
y_train, y_test = y[train_idx], y[test_idx]
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
score = f1_score(y_test, y_pred) # F1 for imbalanced classes
scores.append(score)
print(f"Scores: {scores}")
# Example output: [0.82, 0.79, 0.85, 0.81, 0.83]F1 score kyun? Engine failures rare hoti hain (class imbalance). Accuracy misleading hogi (sabke liye "no failure" predict karna 95% accuracy deta hai lekin sabhi failures miss kar deta hai). F1 precision aur recall ko balance karta hai.
Step 3: Mean aur std report karo
cv_mean = np.mean(scores) # 0.82
cv_std = np.std(scores, ddof=1) # 0.021
print(f"5-fold CV F1: {cv_mean:.3f} ± {cv_std:.3f}")
# Output: "5-fold CV F1: 0.820 ± 0.021"Dono report kyun karein? Mean typical performance estimate karta hai. Std humein consistency batata hai. 0.82 ± 0.02 wala model 0.82 ± 0.15 se zyada reliable hai (high variance suggest karta hai ki model sensitive hai ki woh kaunsa data dekhta hai).
Results Interpret Karna
- Mean F1 = 0.82: Average pe, model unseen data pe 82% F1 achieve karta hai. Yeh real-world performance ka humara best estimate hai.
- Std = 0.021: Low variance matlab model alag-alag data subsets mein consistently generalize karta hai. Agar std 0.15 hoti, hum investigate karte: Kya kuch folds zyada mushkil hain? Kya data heterogeneous hai?
Imbalanced Classes ke liye Stratified k-fold
Aerospace mein, kai tasks mein rare events hote hain (failures, anomalies). Agar engine failures sirf 5% flights mein hoti hain, toh ek random fold mein 10% failures ho sakti hain jabki doosre mein 2%. Yeh variance badha deta hai.
Implementation:
from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for train_idx, test_idx in skf.split(X, y):
# Har fold ka y_test mein ~5% failures hongi, y se match karte hue
...Aerospace mein yeh kyun matter karta hai: Rare events safety drive karte hain. Agar ek fold mein accidentally 0 failures hain, toh us fold ka score meaningless hai. Stratification yeh prevent karta hai.
Common Mistakes aur Misconceptions
Computational Cost
models train karna cost factor se badhata hai:
1000 samples wale Random Forest ke liye trees ke saath:
- Single train: ~5 seconds
- 5-fold CV: ~25 seconds
- 10-fold CV: ~50 seconds
Aerospace context: Large flight logs (millions of records) pe deep learning ke saath training ek model ke liye ghante le sakti hai. matlab 5× zyada time. Isliye GPU acceleration aur parallel fold execution matter karta hai.
Mitigation: Folds parallel mein train karne ke liye scikit-learn mein n_jobs=-1 use karo:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5, scoring='f1', n_jobs=-1)
# 5 folds simultaneously multiple CPU cores pe train karta haiRecall Ek 12-saal ke bachche ko explain karo
Socho tum ek spelling bee ki practice kar rahe ho. Tumhare paas 100 words ki list hai. Yeh hai jo zyaadatar log galat karte hain: Galat tarika: Sabhi 100 words padho. Phir unhi 100 words pe khud ko quiz karo. Tum 95% sahi paate ho aur sochte ho tum ready ho. Lekin competition day pe, tum 20 NEW words dekhte ho aur sirf 60% sahi hote ho. Oops!
Better tarika: 100 words ko 20-20 ke 5 groups mein banto.
- Round 1: Groups 1-4 padho (80 words). Group 5 pe khud ko quiz karo (20 words). Score likhlo.
- Round 2: Groups 1-3 aur 5 padho (80 words). Group 4 pe quiz karo. Score likhlo.
- Round 3: Groups 1-2 aur 4-5 padho. Group 3 pe quiz karo.
- Aur aise hi...
Ant mein, tumhare paas 5 quiz scores hain. Unhe average karo. Yeh tumhe batata hai tum UNSEEN words pe kitna achha karoge, kyunki har quiz mein woh words use hue jinhein tumne us round mein nahi padha tha.
Yahi hai k-fold cross-validation! Usi material pe khud ko test karne ki jagah jise tumne padha (jo cheating hai), tum rotate karte ho kya padhte ho aur kya test karte ho. Yeh tumhe ek honest picture deta hai ki tum sachchi mein kitne achhe ho.
Connections
- 5.6.01-Train-test-split-validation-set — k-fold basic split idea ko extend karta hai
- 5.6.02-Holdout-method — k-fold vs single holdout tradeoffs
- 5.6.04-Stratified-sampling — Folds mein class balance kaise maintain karein
- 5.6.06-Leave-one-out-cross-validation-(LOOCV) — Extreme case jahan
- 5.6.07-Nested-cross-validation — Tuning aur evaluation dono ke liye k-fold use karna
- 5.6.10-Bias-variance-tradeoff-in-model-selection — k bias aur variance ko kyun affect karta hai
- 7.2.03-Hyperparameter-tuning-grid-search — CV grid search ke liye evaluation engine hai
- 8.5.02-Time-series-cross-validation — Aerospace-specific: temporal order respect karna
#flashcards/coding
k-fold cross-validation ka primary purpose kya hai? :: Model generalization performance ka ek zyada robust estimate paana test ke liye data subset ko rotate karke, yeh ensure karte hue ki har data point exactly ek baar test ho.
k-fold CV mein, agar n=1000 samples aur k=5 hai, toh har training set mein kitne samples hain?
k-fold cross-validation score ka formula kya hai? :: jahan har Score_i fold i pe performance metric hai.