/W11
实验Lab: Features for bike demand
- Dataset
- UCI Bike Sharing, hourly (17,379 rows, CC BY 4.0)
- Time
- 3 hours
This week you'll be able to
- Engineer numeric, categorical, datetime, and cyclic features inside a leakage-safe pipeline.
- Target-encode a high-cardinality column without leaking the label.
- Compare filter, wrapper, and embedded selection, and PCA, on the same data.
Goal
Engineer time, weather, and interaction features that a model can use. Then prune them and see what selection and PCA keep.
Data
Hourly rentals from Washington D.C.'s Capital Bikeshare, 2011 and 2012, with weather and calendar columns. The target is cnt, total rentals per hour.
import pandas as pd
bikes = pd.read_csv("https://archive.ics.uci.edu/static/public/275/data.csv", parse_dates=["dteday"])
bikes = bikes.drop(columns=["instant", "casual", "registered"]) # casual + registered = cnt, so they leakTasks
- Split by time: train on 2011 and the first half of 2012, test on the rest. Say why a random split is wrong here.
- Baseline: gradient boosting on the raw columns. Record test RMSE.
- Add datetime features (Procedure 14.3): hour, day of week, weekend, month. Encode hour and month as sine and cosine pairs. Check that hour 23 and hour 0 end up close together.
- Encode
weathersitandseasontwo ways: one-hot and ordinal (Procedure 14.1). Which suits a linear model? Which suits a tree? - Add one interaction you can justify, such as working day × hour. Show with a chart why it should help.
- Put everything in one leakage-safe
ColumnTransformerpipeline (Procedure 14.4). Compare test RMSE with task 2 for a ridge regression and for gradient boosting. - Selection. Drop highly correlated features (Procedure 15.1). Rank by mutual information (Procedure 15.2). Run RFE (Procedure 15.3). Compare the feature sets each method keeps.
- Fit PCA on the scaled features (Procedure 15.5). How many components reach 95% variance? Read the first two loadings (Procedure 15.6).
Deliverable
A notebook with a results table (feature set, model, test RMSE) and a paragraph on which features earned their place.
Done when
- The split is by time, and nothing from the test period is used in fitting.
- Every transform is fitted inside the pipeline.
- The cyclic encoding is checked, not assumed.
Stretch
Engineer lag features of past demand, such as the same hour yesterday. Be careful: at prediction time, which lags would you know?
Research challenge
The research challenge starts this week. Form a pair and claim a metric from the menu before you leave. The talks are in week 12.