Due: Tues, 12/1, midnight
Over the semester, we’ve studied a variety of ways to predict a response variable from a set of predictors. In this final supervised-learning homework, you’ll bring them together and see how they compare on your own data set.
You’ll tackle two prediction problems: one with a quantitative response (regression), and one with a categorical response (classification). For each problem, you’ll train eight types of models, use cross-validation to find good hyperparameters for each, and compare their performance on the same held-out test observations.
By “best-in-class,” we mean the best model you can find within each algorithm’s family, using a reasonable hyperparameter grid search — not a claim that you’ve discovered the mathematically perfect model.
Return to the data set you’ve been using for your earlier homeworks. Choose:
As before, don’t use row numbers, names, IDs, or other bookkeeping fields as predictors just because they’re available. Avoid categorical predictors with enormous numbers of distinct values; one-hot encoding can turn such a variable into hundreds or thousands of columns.
Use the data-loading code/package you developed for homework #0. Clean and prepare your observations as necessary, then make a single train/test split for each prediction problem (for example, 80% training and 20% test). The test rows will effectively be put into a “vault” and be untouched until your final run to gather metrics on the various models.
All eight regression models must use the same regression training/test split. All eight classification models must use the same classification training/test split. Otherwise, their scores won’t be directly comparable.
You’ll use 5-fold cross-validation on the training
set to tune hyperparameters with GridSearchCV.
After choosing the winning hyperparameters for each model family,
evaluate its fitted model on both the training set and the
untouched test set.
Important: Don’t use the test set to choose predictors, select hyperparameters, or decide which versions of an algorithm to try next. Its purpose is to give a fair final comparison of the models you selected using training data alone.
Handle numerical and categorical predictors appropriately.
Standardize numerical predictors (StandardScaler) for
models where scale matters, particularly linear/logistic regression,
PCA/PCR/PCC, and kNN. Encode categorical predictors
(OneHotEncoder for unordered categories;
OrdinalEncoder only when an order is meaningful).
Tree-based methods don’t generally need standardization, but they do
need appropriate numerical representations of categorical features.
ColumnTransformer and Pipeline will be
useful here. Any scaling, imputation, encoding, or PCA that
learns from the data must be fitted inside each cross-validation
training fold, not on the entire data set before
cross-validation. A pipeline passed to GridSearchCV is the
easiest way to ensure this.
Predict \(y_{num}\) using
each of the following algorithms. For each family,
perform a GridSearchCV over a reasonable range of its
important tuning hyperparameters, with 5-fold
cross-validation and mean squared error (MSE)
as the criterion for selecting the winner.
| Model family | sklearn starting point | Some hyperparameters to explore |
|---|---|---|
| Linear regression (with regularization) | Ridge, Lasso, or
ElasticNet |
feature selection and regularization strength
(alpha/l1_ratio) |
| Principal components regression (PCR) | PCA followed by LinearRegression |
number of components (n_components) |
| k-nearest neighbors | KNeighborsRegressor |
number of neighbors (n_neighbors) |
| Decision tree | DecisionTreeRegressor |
max_depth, min_samples_leaf, and
ccp_alpha |
| Bagged decision trees | BaggingRegressor with decision trees |
n_estimators, and perhaps base tree depth/leaf
size |
| Random forest | RandomForestRegressor |
n_estimators, max_features, and tree
depth/leaf size |
| Boosted decision trees | AdaBoostRegressor or
GradientBoostingRegressor |
n_estimators, learning_rate, and base tree
depth |
| Support Vector Machines | SVR |
kernel (linear or rbf),
C, gamma, epsilon |
You don’t need an enormous Cartesian product of hyperparameter values. Choose grids broad enough to meaningfully explore the options, but small enough to finish in a reasonable amount of time. For PCR, try a meaningful range of component counts up to the largest valid value for your data and CV folds.
In sklearn, GridSearchCV uses
scoring="neg_mean_squared_error" because it expects
larger scores to be better. Negate the resulting value
when reporting MSE (where smaller is
better).
Your program should print, for each of the eight families:
best_params_).Now predict \(y_{cat}\) using
each of the following algorithms. Again, use
GridSearchCV and 5-fold cross-validation
to find good hyperparameters for each model family.
| Model family | sklearn starting point | Some hyperparameters to explore |
|---|---|---|
| Logistic regression (with regularization) | LogisticRegression |
feature selection, C, and l1_ratio |
| Principal components classification (PCC) | PCA followed by LogisticRegression |
number of components (n_components), and possibly
C |
| k-nearest neighbors | KNeighborsClassifier |
number of neighbors (n_neighbors) |
| Decision tree | DecisionTreeClassifier |
max_depth, min_samples_leaf, and
ccp_alpha |
| Bagged decision trees | BaggingClassifier with decision trees |
n_estimators, and perhaps base tree depth/leaf
size |
| Random forest | RandomForestClassifier |
n_estimators, max_features, and tree
depth/leaf size |
| Boosted decision trees | AdaBoostClassifier or
GradientBoostingClassifier |
n_estimators, learning_rate, and base tree
depth |
| Support Vector Machines | SVC |
kernel (linear or rbf),
C, and gamma |
For this assignment, use macro-averaged F1
(scoring="f1_macro") to pick the winning hyperparameters
within each classification family. Unlike accuracy, macro-F1 gives each
response category equal weight, making it especially informative when
class frequencies differ. If all your categories are well balanced,
accuracy and macro-F1 may tell similar stories.
Your program should print, for each of the eight families:
best_params_) and mean cross-validation
macro-F1 (best_score_).Be sure to use the same classification train/test split for all eight families, just as you did for regression.
Create two clearly labeled summary tables, one for regression and one for classification. Each should have eight rows, one per model family, and report the results for its best cross-validated model.
Your regression table should include:
| Model | Best hyperparameters | CV MSE | Training MSE | Test MSE |
|---|---|---|---|---|
| Linear regression | … | … | … | … |
| PCR | … | … | … | … |
| … | … | … | … | … |
Your classification table should include:
| Model | Best hyperparameters | CV macro-F1 | Training accuracy | Test accuracy | Training macro-F1 | Test macro-F1 |
|---|---|---|---|---|---|---|
| Logistic regression | … | … | … | … | … | … |
| PCC | … | … | … | … | … | … |
| … | … | … | … | … | … | … |
You may round numerical scores to a sensible number of decimal places. Make the tables easy to scan: a reader should be able to see which methods did well without hunting through pages of program output.
Look especially at training versus test performance. A very low training MSE, or very high training accuracy/F1, isn’t necessarily a victory if the test results are much worse. That gap may indicate overfitting.
Send me an email with subject line DATA 419: homework #5 turnin and attach the following.
Submit the actual .py source files. Two programs would
be a natural organization: one for regression and one for
classification. You’re welcome to organize the code differently if it
makes sense.
The programs should load your data, preprocess it appropriately, carry out the train/test split and grid searches, print the chosen hyperparameters and requested metrics, and produce the two summary tables. Don’t send screenshots of code.
Include:
This does not need to be a long formal paper. Prefer two readable tables and a concise, insightful paragraph over pages of prose. And if none of the methods works particularly well on your data, say so — that’s a legitimate finding, too.
As always, commit your work to your git repository
before turning it in.
Send email with subject line DATA 419 homework #5 help!! or come to office hours!