Understand the idea
Production fits the scale on the exploratory population, then transforms a reproducible sample of at most 500 rows for the hierarchy. The resulting labels belong to those sampled rows.
Illustrative data
Click the highlighted code to change the result.
sample = example.sample(, random_state=42)| original row | length_mm | width_cm |
|---|---|---|
| w1 | 10 | 1 |
| w2 | 11 | 1.3 |
| w3 | 12 | 0.7 |
| w4 | 25 | 6 |
| w5 | 26 | 6.2 |
| w6 | 40 | 2 |
| w7 | 41 | 2.4 |
| w8 | 42 | 1.7 |
| original row | length_mm | width_cm |
|---|---|---|
| w2 | 11 | 1.3 |
| w6 | 40 | 2 |
| w1 | 10 | 1 |
| w8 | 42 | 1.7 |
Sample 4 rows with seed 42, preserving their values and original indices.
What the syntax does
X.sample- Selects rows while preserving their original index labels.
Supplied for this practice
These objects are prepared afresh before your editor runs.
X- Feature dataframe for the declared population, preserving row indices.
X = df[['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']]
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex | year |
|---|---|---|---|---|---|---|
| Torgersen | 39.1 | 18.7 | 181 | 3750 | male | 2007 |
| Torgersen | 39.5 | 17.4 | 186 | 3800 | female | 2007 |
| Torgersen | 40.3 | 18 | 195 | 3250 | female | 2007 |
| Torgersen | 36.7 | 19.3 | 193 | 3450 | female | 2007 |
| Torgersen | 39.3 | 20.6 | 190 | 3650 | male | 2007 |
| Torgersen | 38.9 | 17.8 | 181 | 3625 | female | 2007 |
| Torgersen | 39.2 | 19.6 | 195 | 4675 | male | 2007 |
| Torgersen | 41.1 | 17.6 | 182 | 3200 | female | 2007 |
- Sample 100 Penguin measurement rows with seed 42.
- Store the original values and indices in
answer.
answerHints
Think: Original indices preserve the link between sampled observations and their measurements.
Tools: DataFrame.sample with random_state.
Approach: Take the requested reproducible sample without resetting its index.
Explained solution
answer=X.sample(100,random_state=42)
Retaining row identity lets later assignments and original-unit profiles remain aligned to the sampled population.