I’ve pushed yesterday’s code to the class repo (see sec_8_1_1.py, sec_8_1_1_cv.py, sec_8_1_2.py, and sec_8_1_2_cv.py). Also, here are the diagrams we studied in class (click to expand):
![]() |
![]() |
|
|
|
I got this very illuminating response from one of my Stats Bros:
The bias/variance inherent in the PC is “baked in” before there is any model for the y variable. I think you want to think of the PC part as just a different set of x-values — a different measurement of the data — and hence any discussion of overfitting is unrelated. Think of it as: you measured height and weight in inches and pounds, then decided to do centimeters and kilos. Neither choice impacts whether you are overfitting the prediction variable.
This comes to us from Dave Marchette, Ph.D., retired but formerly a chief scientist at Dahlgren Naval Base.
You may remember that Bree stymied me in class last week when she asked (Stephen’s paraphrase): “we’re attempting to use some number M of principal components < p, which will help us get a more parsimonious model with fewer parameters to fit. However, to compute the loadings for the principal components, aren't we fitting coefficients for each PC in its linear combination of all the features?" Here's ChatGPT's response:
You’re absolutely correct that we’re estimating lots of coefficients when we compute principal components. In fact, we might estimate more coefficients overall than ordinary regression would!But the point of PCR isn’t to minimize the total number of numerical quantities we calculate. It’s to minimize the number of degrees of freedom available to fit the response variable.
PCA estimates its loadings without looking at the response variable. Once those directions are established, PCR has only \(M\) slope coefficients it can adjust to predict \(y\), rather than \(p\).
Thus, PCR reduces the flexibility of the predictive model, even though it doesn’t necessarily reduce the total number of coefficients computed during training.
This is something I’m going to have to mull over. It’s not immediately obvious to me that “oh, but we’re not involving the response variable, only the predictors” answers the question satisfactorily. I’m going to ask a couple statistics oracles I know: stay tuned.
To reiterate from various lectures throughout the semester, the spring class I’ve been talking about which will essentially be a neural-net-infused continuation of our class is:
DATA 470D5: Machine Learning
(Its CRN, if you care about such things, is 22448.)
In the near future, this class will (curriculum approval process pending) be offered as a regular, on-the-books course with its own course number: DATA 429. In that future world, the two classes will be titled:
You technically don’t need to care about that future world, but I just want you to know that the material I teach for DATA 470D5 this spring is exactly the same material I will, henceforth, teach in DATA 429 in subsequent semester.
The competition has ended and the final leaderboard results are posted! Props to Eden&Belen, Molly, and Eraj, our top three finishers!
Homework #4, a return to your own semester data set, has been posted! It is due Nov 7th at midnight. It involves applying a number of techniques we’ve explored in class to your own data, and you are of course welcome to use anything you like in the class github repo to inspire or orient you.
Send questions!
I’ve pushed the code from today’s class to the class github repo, including sec6_3.py which has the PCR/PCA code for the WNBA data set.
Also, as I mentioned in class today, we’re definitely not going to have two responsive readings this semester (I’m still holding out hope for one) so I went ahead and gave everyone the +50XP for this “missing” assignment on the scoreboard.
I was reminded by a Kaggler today that although I showed you guys sample code to add regularization for linear regression (Lasso and Ridge) I never explained how to do this for classification (logistic regression).
First off, fun fact: it turns out that sklearn’s LogisticRegression already uses regularization by default! Specifically, it uses L2 (ridge) regularization.
To control the regularization in LogisticRegression, there are three important parameters:
C: Controls the strength of regularization. Unlike alpha in Ridge and Lasso, smaller values mean stronger regularization. The default is C=1.0.l1_ratio: Controls the type of regularization:
0: Pure L2 (ridge)1: Pure L1 (lasso)solver: Specifies the optimization algorithm. Use solver="saga" if you want L1 or elastic net regularization.Here are some examples:
from sklearn.linear_model import LogisticRegression
# Ridge (L2) regularization — the default
model = LogisticRegression(C=1.0)
# Stronger ridge regularization
model = LogisticRegression(C=0.1)
# Lasso (L1) regularization
model = LogisticRegression(C=1.0, l1_ratio=1, solver="saga")
# Elastic net (50% L1, 50% L2)
model = LogisticRegression(C=1.0, l1_ratio=0.5, solver="saga")
# No regularization at all
model = LogisticRegression(C=float("inf"))
Important: You should generally scale your numerical features before applying regularization, since the penalty depends on the magnitudes of the coefficients.