Leave-One-Out Cross-Validation Practice Problem
This data science coding problem helps you practice Model Validation, leave-one-out cross-validation, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Model Validation.
- Problem ID: 153
- Problem key: 153-leave-one-out-cross-validation
- URL: https://datacrack.app/solve/153-leave-one-out-cross-validation
- Difficulty: easy
- Topic: Model Validation
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Leave-One-Out Cross-Validation
---
### 🎯 Goal
Create **Leave-One-Out Cross-Validation** splits, where each example becomes the validation set exactly once.
---
### 📖 Introduction
Leave-One-Out Cross-Validation, often written as **LOOCV**, is a special case of K-Fold Cross-Validation.
In ordinary K-Fold Cross-Validation, we choose some value of `k`, such as `5` or `10`.
In Leave-One-Out Cross-Validation:
$$
k = n
$$
where `n` is the number of examples.
That means each validation set contains exactly one example. So if there are `n` examples, LOOCV creates `n` validation rounds.
> **Note:** LOOCV uses a lot of training rounds, so it can be expensive for large datasets.
---
### 💻 Task
Implement `leave_one_out_indices` from scratch.
Steps:
1. Create indices from `0` to `n_samples - 1`.
2. For each index `i`, make `[i]` the validation indices.
3. Use every other index as the training indices.
4. Return the list of `[train_indices, val_indices]` pairs.
---
### 📥 Input / 📤 Output
**Input**
- `n_samples` (`int`): number of examples in the dataset
**Output**
- `list`: a list of validation rounds
- Each round should be `[train_indices, val_indices]`
- `val_indices` should contain exactly one index
---
### 🧩 Starter Code
```python
def leave_one_out_indices(n_samples):
"""
Return train/validation index splits for Leave-One-Out Cross-Validation.
"""
# TODO 1: Create an empty list of rounds
# TODO 2: Loop through every sample index
# TODO 3: Use that one index as validation
# TODO 4: Use all remaining indices as training
pass
```
---
### 💡 Example
```python
leave_one_out_indices(3)
```
**Expected Output**
```python
[
[[1, 2], [0]],
[[0, 2], [1]],
[[0, 1], [2]]
]
```
---
### 🧭 Hint
For each validation index `i`, the training indices are all indices `j` where `j != i`.
Starter Code
def leave_one_out_indices(n_samples):
"""
Return train/validation index splits for Leave-One-Out Cross-Validation.
"""
# TODO 1: Create an empty list of rounds
# TODO 2: Loop through every sample index
# TODO 3: Use that one index as validation
# TODO 4: Use all remaining indices as training
pass