<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivani Rana on Medium]]></title>
        <description><![CDATA[Stories by Shivani Rana on Medium]]></description>
        <link>https://medium.com/@shivanirana03443?source=rss-2598d1b3bdf8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Xn7m98-Aa3IBcJKL.jpg</url>
            <title>Stories by Shivani Rana on Medium</title>
            <link>https://medium.com/@shivanirana03443?source=rss-2598d1b3bdf8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:50:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivanirana03443/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[1. Cross-Validation Explained: How to Evaluate Machine Learning Models Properly]]></title>
            <link>https://medium.com/@shivanirana03443/1-cross-validation-explained-how-to-evaluate-machine-learning-models-properly-55db7bd08fbd?source=rss-2598d1b3bdf8------2</link>
            <guid isPermaLink="false">https://medium.com/p/55db7bd08fbd</guid>
            <dc:creator><![CDATA[Shivani Rana]]></dc:creator>
            <pubDate>Sat, 20 Dec 2025 06:19:41 GMT</pubDate>
            <atom:updated>2025-12-20T06:19:41.748Z</atom:updated>
            <content:encoded><![CDATA[<p>A single train-test split can be misleading. Cross-validation evaluates your model multiple times with different data splits, giving you a more reliable estimate of how it will perform on unseen data.</p><p>One of the most important questions in machine learning is simple:</p><p><strong>How do we know if a model will perform well on unseen data?</strong></p><p>Training a model to fit data is rarely the hard part. The real challenge is <strong>evaluating it correctly.</strong></p><p>In many ML tutorials, we’re simply told which data points belong to the training set and which belong to the testing set. The split is aready done for us.</p><p>But in practice, this raises a fundamental question:</p><p><strong>How do we decide which points should be used for training and which should be used for testing?</strong></p><h3>The Problem: A Single Split Is Arbitrary</h3><p>If we randomly choose a training and test set, the model’s performance can depend heavily on <em>which specific points</em> were selected.</p><p>A different random split may lead to a different result.</p><p>This makes evaluation fragile:</p><ul><li>The estimate depends on chance</li><li>One unlucky split can be misleading</li><li>We get little insight into model stability</li></ul><p><strong>Relying on a single train–test split is often not enough.</strong></p><h3>The Idea Behind Cross-Validation</h3><p>When we are not explicitly told what should be used for training and testing, <strong>cross-validation provides a systematic solution</strong>.</p><p>Rather than worrying too much about finding the <em>best</em> points for training and the <em>best</em> points for testing, cross-validation changes the question entirely.</p><p>It uses <strong>all data points for both purposes</strong>, but <strong>in an iterative way</strong>.</p><p>Each data point:</p><ul><li>Is used for training in some steps</li><li>Is used for validation in other steps</li></ul><p>In other words, we evaluate the model repeatedly under different splits and then combine the results.</p><h3>How Cross-Validation Works Conceptually</h3><p>Cross-validation divides the dataset into multiple subsets.</p><p>At each step:</p><ul><li>One subset is held out for validation</li><li>The remaining subsets are used for training</li><li>Model performance is recorded</li></ul><p>This process is repeated several times, each time with a different validation subset.</p><p>Instead of trusting one split, we trust a <strong>pattern across many splits</strong>.</p><p>This gives us:</p><ul><li>A more stable estimate of performance</li><li>Insight into variability</li><li>A better basis for comparing models</li></ul><h3>K-Fold Cross-Validation</h3><p>The most common approach is <strong>k-fold cross-validation</strong>, where the dataset is divided into k approximately equal-sized folds.</p><p>The model is trained k times:</p><ul><li>Each time using k−1 folds for training</li><li>And the remaining fold for validation</li></ul><p>The final performance is obtained by averaging the results across all folds.</p><h3>Example with 5-Fold Cross-Validation</h3><p>Imagine you have 100 data points divided into 5 folds of 20 points each:</p><p><strong>Iteration 1:</strong> Train on folds 2, 3, 4, 5 → Validate on fold 1<br> <strong>Iteration 2:</strong> Train on folds 1, 3, 4, 5 → Validate on fold 2<br> <strong>Iteration 3:</strong> Train on folds 1, 2, 4, 5 → Validate on fold 3<br> <strong>Iteration 4:</strong> Train on folds 1, 2, 3, 5 → Validate on fold 4<br> <strong>Iteration 5:</strong> Train on folds 1, 2, 3, 4 → Validate on fold 5</p><p>You get 5 performance scores, and the final score is typically their average.</p><h3>Why 10-Fold Cross-Validation Is Common</h3><p>In practice, <strong>10-fold cross-validation</strong> is widely used.</p><p>It offers a good trade-off:</p><ul><li>Enough folds to reduce bias</li><li>Manageable computational cost</li><li>Reasonably stable estimates</li></ul><p>For many datasets, it provides a reliable default evaluation strategy.</p><h3>Leave-One-Out Cross-Validation (LOOCV)</h3><p>In leave-one-out cross-validation, each data point is used as the validation set once.</p><p>At each step:</p><ul><li>One point is held out</li><li>All remaining points are used for training</li></ul><p>This method:</p><ul><li>Maximizes training data usage</li><li>Is useful for very small datasets</li><li>Provides nearly unbiased estimates</li></ul><p>However, it can be computationally expensive for large datasets and may lead to high variance in estimates.</p><h3>Data Leakage: A Crucial Consideration</h3><p>For cross-validation to be meaningful, we must avoid <strong>data leakage</strong>.</p><p>Data leakage occurs when information from the validation data unintentionally influences the training process.</p><p>Common causes include:</p><ul><li>Scaling or normalization done on the full dataset</li><li>Feature engineering performed before splitting</li><li>Encodings that use target information globally</li></ul><p>When leakage occurs, validation performance no longer represents unseen data — it becomes artificially inflated.</p><p><strong>The golden rule:</strong> All data-dependent steps (scaling, feature selection, etc.) must be performed separately inside each training fold.</p><h3>Correct vs Incorrect Approach</h3><p>❌ <strong>Incorrect:</strong></p><pre>1. Scale entire dataset<br>2. Split into folds<br>3. Train and validate<br></pre><p>✅ <strong>Correct:</strong></p><pre>1. Split into folds<br>2. For each fold:<br>   - Scale training data<br>   - Apply same scaling to validation data<br>   - Train and validate</pre><h3>Cross-Validation and Overfitting</h3><p>Another key purpose of cross-validation is understanding <strong>overfitting</strong>.</p><p>A model that performs extremely well on training data but shows inconsistent performance across validation folds may be overfitting.</p><p>Cross-validation helps reveal:</p><ul><li>Whether performance is stable</li><li>How sensitive the model is to the data</li><li>Whether improvements are robust or accidental</li></ul><p><strong>Large variation across folds is often a warning sign.</strong> If your model gets 95% accuracy on some folds and 70% on others, it suggests the model hasn’t learned generalizable patterns.</p><h4>An Example: 6 Data Points with 3 Groups</h4><p>Consider a small dataset with <strong>6 data points</strong>, belonging to <strong>3 </strong>patients:</p><ul><li><strong>Patient A:</strong> measurements 1 and 2</li><li><strong>Patient B:</strong> measurements 3 and 4</li><li><strong>Patient C:</strong> measurements 5 and 6</li></ul><p>If we perform random k-fold cross-validation, measurements from the same patient may appear in both training and validation sets.</p><p>The model might perform well simply because it has already seen very similar examples from the same patient — not because it generalizes to new patients.</p><p><strong>Solution:</strong> Use <strong>grouped cross-validation</strong>, which ensures all measurements from the same patient stay together in either training or validation, never both.</p><p>This principle applies whenever your data has natural groups: patients, customers, time series from the same source, etc.</p><h3>Which Method Should You Use?</h3><p>A simple practical guideline:</p><ul><li><strong>Small datasets (&lt; 100 samples):</strong> Leave-one-out cross-validation</li><li><strong>Medium to large datasets:</strong> k-fold cross-validation (often 10-fold)</li><li><strong>Grouped data:</strong> Grouped k-fold cross-validation</li><li><strong>Time series:</strong> Time series split (respects temporal order)</li></ul><p>The right choice depends on your dataset size, computational resources, and data structure.</p><h3>Quick Implementation Example</h3><p>Here’s how simple cross-validation is with scikit-learn:</p><p>python</p><pre>from sklearn.model_selection import cross_val_score<br>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.datasets import load_iris</pre><pre># Load data<br>X, y = load_iris(return_X_y=True)</pre><pre># Create model<br>model = RandomForestClassifier()</pre><pre># Perform 10-fold cross-validation<br>scores = cross_val_score(model, X, y, cv=10)</pre><pre>print(f&quot;Accuracy: {scores.mean():.2f} (+/- {scores.std():.2f})&quot;)<br># Output: Accuracy: 0.95 (+/- 0.04)</pre><p>The mean gives you the average performance, and the standard deviation shows variability across folds.</p><h3>Key Takeaways</h3><p>Cross-validation is essential for honest model evaluation. It helps you:</p><ol><li>Get more reliable performance estimates than a single split</li><li>Detect overfitting through performance variability</li><li>Avoid data leakage by properly structuring your pipeline</li><li>Compare models fairly on the same validation framework</li></ol><p>Remember: A model is only as good as how well you evaluate it. Cross-validation helps ensure you’re measuring what matters — performance on truly unseen data.</p><h3>or Interview Prep</h3><p><strong>Q: Explain cross-validation and why we use it.</strong><br> A: It’s a method to estimate model performance by iteratively using different subsets of data for training and validation. It reduces the randomness of a single train-test split and provides more stable performance estimates.</p><p><strong>Q: What is k-fold cross-validation? Why is 10-fold common?</strong><br> A: Dataset split into k folds; train on k-1, validate on 1; repeat k times. 10-fold is a balance between bias, variance, and computational cost.</p><p><strong>Q: What is leave-one-out cross-validation? When should you use it?</strong><br> A: Each data point is used as validation once; good for small datasets, but computationally expensive for large datasets.</p><p><strong>Q: What is data leakage and why is it a problem in cross-validation?</strong><br> A: When validation information leaks into training, giving artificially high performance. All data-dependent steps must be fit only on training folds.</p><p><strong>Q: How do you choose between k-fold CV and LOOCV?</strong><br> A: LOOCV for very small datasets; k-fold (commonly 10) for larger datasets to save computation while maintaining stability.</p><p><strong>Q: What would you do if cross-validation results are inconsistent?</strong><br> A: Investigate variance across folds, check for overfitting, consider grouped splits if data has natural groups, collect more data if possible, or try simpler models.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55db7bd08fbd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>