Lab 8: Train/Validation/Test Split and Polynomial Regression DS
Date: July 16
Time: 1:00-5:00 PM
TA: Jianhao Huang
Overview
Today we will build on linear regression by asking a deeper modeling question: how do we know whether a model is actually useful on data it has not seen before?
The lab focuses on train/validation/test splits, polynomial regression, transformed features, and multiple regression. You will first revisit a taxi pickup dataset to compare a straight-line model with a polynomial model. Then you will work with English Premier League player data to practice stratified splitting, grouping, feature engineering, dummy variables, and interpretation of regression coefficients.
Goals
By the end of this lab, you should be able to:
- explain why we keep separate training, validation, and testing data;
- use
train_test_split()and understand when stratified splitting is useful; - compare model performance on held-out data using R²;
- use
PolynomialFeaturesto fit a curved relationship with a linear model; - use feature scaling when polynomial features become large;
- build multiple regression models with transformed variables such as age² and
log2(page_views); - convert categorical variables into dummy variables with
pd.get_dummies(); - interpret model coefficients carefully, especially for transformed and dummy variables.
Materials
Download the Lab 8 notebook folder from the password-protected Box materials folder:
Open notebook lab materials on Box
Ask Eric for the password.
Open the folder:
Lab_08_Train_Validation_Test_Split_and_Polynomial_Regression
The folder should include:
| File | Purpose |
|---|---|
COSMOS_Lab_08_Train_Validation_Test_Split_and_Polynomial_Regression.ipynb |
Main Lab 8 notebook. |
dataset_1.txt |
Taxi pickup dataset used for linear and polynomial regression. |
league_data.txt |
English Premier League player dataset used for multiple regression. |
Open the notebook from inside the downloaded Lab 8 folder so the relative paths to dataset_1.txt and league_data.txt work.
Notebook Flow
Work through the notebook in order:
- Review train/validation/test split terminology and the difference between inference and prediction.
- Fit a simple linear regression model to taxi pickup counts by time of day.
- Refit the taxi model using polynomial features and compare held-out R².
- Try feature scaling with
StandardScaler. - Load and inspect the football dataset.
- Use stratified train/test splitting by
region, fixing any missing-value issue that prevents the split. - Use
.groupby()and.agg()to summarize market value, page views, and fantasy points by position and club type. - Build a multiple regression model for
market_valueusing numeric, polynomial, log-transformed, and categorical features. - Recode
position_catwith dummy variables and interpret the new coefficients. - Sketch a forward-selection algorithm using set symmetric difference.
Main Lab Tasks
Taxi Pickup Regression
Use dataset_1.txt to model taxi pickup count as a function of time of day.
You should complete these tasks:
- Load the cab data and split it into training and testing sets.
- Fit a simple
sklearn.linear_model.LinearRegressionmodel usingTimeMin. - Refit the same linear model using
statsmodelsand compare the R² values. - Use
PolynomialFeaturesto create polynomial features such asx,x^2, andx^3. - Fit the polynomial regression model and calculate its test-set R².
- Make a residual plot for the polynomial model and explain what the residuals show.
- Try scaling the polynomial features with
StandardScaler.
Football Multiple Regression
Use league_data.txt to predict a player’s market_value.
You should complete these tasks:
- Load the football dataset and inspect column types, shape, and summary statistics.
- Create a stratified train/test split using
region. - Identify and handle the missing-value issue that prevents stratified splitting from working at first.
- Use
.groupby()to compare market value, page views, and fantasy points across positions and club categories. -
Build the model:
market_value ~ fpl_points + age + age^2 + log2(page_views) + new_signing + big_club + position_cat - Add the
age_squaredandlog_viewstransformed features. - Add a constant term for
statsmodels. - Fit the model with
OLSand evaluate held-out R² withr2_score. - Interpret the coefficients for age, age²,
log2(page_views), andbig_club. - Replace the numeric
position_catfeature with dummy variables and interpretposition_cat_2.
Checkoff
Show the TA:
- The Lab 8 notebook runs from the imports through both datasets.
- The taxi linear model and polynomial model, including their test-set R² values.
- A residual plot for the polynomial taxi model.
- A successful stratified train/test split for the football dataset.
- At least one
.groupby()summary table for the football data. - The football multiple regression summary and held-out R² score.
- Your interpretation of at least two regression coefficients.
- The dummy-variable version of the position model.
Final Questions
Answer these briefly in your group notes:
- Why should the test set stay untouched until final model evaluation?
- What changed when you used polynomial features on the taxi data?
- Why might scaling be helpful after creating polynomial features?
- Why is
position_cata poor feature if we leave it as the numbers 1, 2, 3, and 4? - What is the difference between using regression for prediction and using regression for inference?