Sampling from an Array Practice Problem
This data science coding problem helps you practice Random Sampling & Generators, sampling from an array, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Random Sampling & Generators.
- Problem ID: 117
- Problem key: 117-sampling-from-an-array
- URL: https://datacrack.app/solve/117-sampling-from-an-array
- Difficulty: easy
- Topic: Random Sampling & Generators
- Module: NumPy Foundations
Problem Statement
# 🧩 Sampling from an Array
---
### 🎯 Goal
`np.random.choice` draws random samples from a given array — with or without replacement. This is the NumPy equivalent of drawing cards from a deck or selecting random rows from a dataset. It is used for bootstrapping, data augmentation, and creating train/test splits.
---
### 🔍 Sampling with `np.random.choice`
```python
np.random.seed(42)
data = [10, 20, 30, 40, 50]
np.random.choice(data, size=3, replace=True) # with replacement
np.random.choice(data, size=3, replace=False) # without replacement
```
| Parameter | Meaning |
|:----------|:--------|
| `data` | Source array to sample from |
| `size` | Number of samples |
| `replace` | `True` = can pick same element twice; `False` = no repeats |
> **Warning:** When `replace=False`, `n_samples` cannot be larger than `len(data)`.
---
### 💻 Task
Implement `sample_from_array(data, n_samples, replace, seed)` that samples from the array and returns the **sorted list of unique sampled values**.
---
### 📥 Input
- `data`: list of values to sample from
- `n_samples`: int — number of samples to draw
- `replace`: bool — sample with or without replacement
- `seed`: int — random seed
### 📤 Output
- Sorted list of unique values that appeared in the sample
---
### 🧩 Starter Code
```python
import numpy as np
def sample_from_array(data, n_samples, replace, seed):
"""
Sample from an array and return sorted unique values.
Args:
data (list): Source array
n_samples (int): Number of samples to draw
replace (bool): With or without replacement
seed (int): Random seed
Returns:
list: Sorted unique sampled values
"""
# 🧠 TODO:
pass
```
---
### 💡 Example
```python
sample_from_array([10, 20, 30, 40, 50], 10000, True, 42)
# Expected: [10, 20, 30, 40, 50] — with enough samples, all values appear
```
---
### 🔑 Key Concepts
- `replace=True` allows the same element to be picked multiple times (bootstrapping)
- `replace=False` ensures no duplicates (like shuffling a subset)
- When `n_samples == len(data)` and `replace=False`, it gives a random permutation of the dataStarter Code
import numpy as np
def sample_from_array(data, n_samples, replace, seed):
"""
Sample from an array and return sorted unique values.
Args:
data (list): Source array
n_samples (int): Number of samples to draw
replace (bool): With or without replacement
seed (int): Random seed
Returns:
list: Sorted unique sampled values
"""
# 🧠 TODO:
pass