

W02 / 2026-09-14 / 35 MIN / ENGLISH
Leakage-safe preprocessing and feature meaning
Classroom formative practice. This does not add a weekly graded assignment. The exercise time is included in class practice, not added after the three-hour meeting.
Before you start
- Use a CPU runtime. Save a personal copy or open the downloaded notebook in Jupyter.
- Run the included complete walkthrough first. Read its setup, definitions, and checkpoints.
- Predict before running each exercise cell; then change only the requested factor.
- Keep your actual outputs, configuration, explanation, and limitation. Free tools are sufficient.
TASK 1 / PREDICT - RUN - EXPLAIN
Fit the scaler on training only
Predict the training-only mean, then compare it with a mean fitted using future test values. Change the test values to 1000 and 1200 without changing training.
- Write your prediction in the notebook response field before running.
- Run the starter cell below after the worked walkthrough.
- Make the requested controlled change and record the configuration.
- Explain the mechanism using the actual output, not an invented result.
from sklearn.preprocessing import StandardScaler
train_demo=np.array([[1.],[2.],[3.]])
test_demo=np.array([[100.],[120.]])
safe_scaler=StandardScaler().fit(train_demo)
leaky_scaler=StandardScaler().fit(np.vstack([train_demo,test_demo]))
observations['task1']={'training_mean':float(safe_scaler.mean_[0]),'leaky_mean':float(leaky_scaler.mean_[0])}
print(observations['task1'])Self-check: The safe mean stays 2 when only test values change. The leaky mean changes.
TASK 2 / PREDICT - RUN - EXPLAIN
Freeze the split IDs
Generate two splits of row IDs using the same seed, then use a different seed. Record the actual IDs, not just the seed.
- Write your prediction in the notebook response field before running.
- Run the starter cell below after the worked walkthrough.
- Make the requested controlled change and record the configuration.
- Explain the mechanism using the actual output, not an invented result.
from sklearn.model_selection import train_test_split
row_ids=np.arange(30)
a,b=train_test_split(row_ids,test_size=.2,random_state=2026)
c,d=train_test_split(row_ids,test_size=.2,random_state=2026)
e,f=train_test_split(row_ids,test_size=.2,random_state=2027)
observations['task2']={'same_seed_same_test':bool(np.array_equal(b,d)),'different_seed_same_test':bool(np.array_equal(b,f)),'test_ids':b.tolist()}
print(observations['task2'])Self-check: The same seed reproduces the same test IDs for the same operation and input ordering. Training and test IDs must not overlap.
TASK 3 / PREDICT - RUN - EXPLAIN
Preserve feature order and units
Train the small pipeline below. Compare normal test features with reversed feature columns. Explain why matching array shape is not enough.
- Write your prediction in the notebook response field before running.
- Run the starter cell below after the worked walkthrough.
- Make the requested controlled change and record the configuration.
- Explain the mechanism using the actual output, not an invented result.
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
iris_demo=load_iris()
ix_train,ix_test=train_test_split(np.arange(len(iris_demo.target)),test_size=.25,stratify=iris_demo.target,random_state=2026)
feature_model=make_pipeline(StandardScaler(),LogisticRegression(max_iter=300)).fit(iris_demo.data[ix_train],iris_demo.target[ix_train])
p1=feature_model.predict(iris_demo.data[ix_test]); p2=feature_model.predict(iris_demo.data[ix_test,::-1])
observations['task3']={'changed_predictions':int(np.sum(p1!=p2)),'feature_names':iris_demo.feature_names}
print(observations['task3'])Self-check: Report the observed number of changed predictions. Reversing columns changes feature meaning even when the shape is identical.
Completion checklist
- Three predictions and three explanations.
- One controlled change with its actual outcome.
- One limitation and a brief AI-use/checking disclosure, or "none".
- A downloaded W02-evidence.json, then a two-minute peer explain-back.
The notebook marks incomplete response records as drafts. Completing the fields is not proof that an explanation is correct.
The guided walkthroughs were previously executed. The new exercise extension cells have not yet been execution-tested for this revision. Agent exercises are deterministic simulations; no paid model or consequential external action is used.