Supervised Machine Learning
Transcript
OK. Good morning, everyone. This is Jack Van Horn from the BD2K Training Coordinating Center at the University of [? Southern ?] California. And I'd like to welcome you all to our [INAUDIBLE] Knowledge Guide to the Fundamentals of Data Science. Today we're going to be hearing from Daniela Witten from the University of Washington in Seattle on the subject of supervised machine learning. Dr. Witten earned her Bachelor's degree in math and biology with honors and distinction
from Stanford University in 2005, and then earned her PhD in statistics from Stanford in 2010. Since 2014, Daniela has been an associate professor in statistics and biostatistics at the University of Washington in Seattle. Her research involves the development of statistical machine learning methods for high dimensional data with applications to genomics and to other fields. She is a recipient of numerous awards, including an NDSEG Research Fellowship and NIH Director's
Early Independence Award, a Sloan Research Award, and an NSF Career Award. Her work has been featured in the popular media and Forbes magazine, Elle magazine, on KUOW radio, and as a PopTech Science fellow. She is the co-author with Gareth James, Trevor Hastie, and Rob Tibshirani of the very popular textbook, Introduction to Statistical Learning. She was a member of the Institute of Medicine Committee that released the report on the evolution of translational omics.
So her talk today will be very appropriate to the subject of big data, computing, and statistical learning. Once again, I'd like to remind you that if you have any questions for Dr. Witten during her talk to please submit your questions via the little question submission mechanism here on the right side-- usually on the right side of your display. And we'll gather all those up and ask them at the end of Dr. Witten's presentation. So without further ado, Dr. Witten, thank you very much for presenting to us today. Thanks so much for the introduction. And so today, as we just heard, I'm going to talk about supervised machine learning. And just to give you an overview, machine learning basically falls into two camps-- supervised learning and also unsupervised learning. So the idea behind supervised learning is that we use a data set, x, to predict or to detect an association with a response, y.
So examples of supervised learning include regression where we're trying to predict a quantitative response, classification where we're trying to predict a categorical response. And you're going to hear a lot about supervised learning today from me. But I do briefly just want to mention that the other big type of machine learning is learning today from me. But I do briefly just want to mention that the other big type of machine learning is unsupervised learning. And the idea behind unsupervised learning is that we want to discover the signal in a matrix, X, and no y is available. So unsupervised learning is an incredibly important topic. And that's actually going to be covered in next week's webinar. And today instead, I will just be focusing on supervised learning. So there's a couple different flavors of supervised learning. And maybe the best known flavor is regression.
So the idea behind regression is that you want to predict a quantitative response such as blood pressure, cholesterol level, tumor size. So the thing that makes regression regression is that the response, which I'm going to call y today, is quantitative. So the other main flavor of supervised learning is classification. And in classification, we are trying to predict a categorical response such as tumor versus normal tissue, heart disease versus no heart disease, subtype of disease, and so on.
So the only difference between regression and classification is the type of your response, y. If it's a quantitative response, you're in the regression framework. If it's a categorical or qualitative response, you're in the classification framework. And one thing that I really want to emphasize is that it doesn't matter what type of data matrix, X, you have. So as I'll mention in a couple minutes, the idea is you have a data matrix, X, which contains measurements for your variables or your features. You're trying to predict this response, y. So I don't care if your data matrix, X, is quantitative, if it's categorical. It doesn't matter. It's still going to be regression if your response, y, is quantitative. And it'll still be classification if your response, y, is categorical. So regression and classification are the two main flavors of supervised learning.
There are also other flavors like if you have a response that's a survival time, which is possibly censored because some people might have died, or dropped out of the study, and so on, then that is a slightly different flavor of supervised learning. But the main ideas that apply to regression also hold for these other flavors of supervised learning. And so today, I'm going to just be talking about regression. But again, all of these ideas extend directly to other types of quantitative responses.
So I'm going to start off talking about linear models, because linear models give us an incredibly flexible framework for doing regression. And basically, if you're doing supervised machine learning, 90% of what you need fits within the linear model framework. And then at the end of my talk, I'm going to just speak for a few minutes about a really different type of supervised learning, which is a decision tree approach. So to begin, we're in the linear model setting.
And the idea is that we have n observations. And for the ith observation, I'm going to call the response y sub i. And I'm going to let xi1 through xiP denote the P features for that ith observation. So for example, yi might be blood pressure, which is a quantitative value. And then xi1 through xiP are a bunch of features. Maybe these are biomarkers that we think are predictive of blood pressure. So we want to use these features to predict the response.
And in a linear model, we do this as follows. We say that yi is just equal to intercept beta naught plus the coefficient beta 1, which gets multiplied by the first feature, xi1 and so on all the way through to the Pth feature, where beta P is multiplied by xiP plus a noise term, epsilon. So epsilon sub i is the noise term associated with the ith observation. And the idea is that we need to estimate these unknown coefficients, beta naught through beta P. In other words, we have to fit the model.
So actually there's a lot of ways we could fit this model. And I'm going to start by talking about the best known way to fit this model. And then I'll talk about the fact that that best known way isn't necessarily the best approach, especially in the context of biomedical big data. But first, let's talk about the simplest way to fit a linear model. And if you took STAT 101, then you probably saw the least squares approach for fitting a linear model. And the idea behind least squares is that we look for estimates of the coefficients. If you see a little hat on a coefficient, that means it's an estimate. So I'm looking for a beta naught hat, beta 1 hat, all the way through to beta P hat such that the sum of squared errors is as small as possible. So I'm going to sum the quantity, yi, minus the prediction or the fitted value for my model. I get that by plugging in the coefficient estimates. And then I'm going to square that quantity.
So this entire term here is called a sum of squared errors. It's also called a residual sum of squares. And when I fit a model using these squares, I'm just looking for these coefficient estimates such that this residual sum of squares is as small as possible. A simpler way to represent this is to say that I'm just looking for coefficient estimates such that the sum of yi minus yi hat squared is as small as possible where yi hat is the prediction from my model. Or you can also call it the fitted value from the model. So this is a really natural way to fit a linear model. And it works pretty well, especially in the old fashioned data setting where you have three, or five, or eight variables that you measured and you have a huge number of observations. So this works really well if n is large and P is really small. But as we'll see in a few minutes, often in observations. So this works really well if n is large and P is really small.
But as we'll see in a few minutes, often in the context of biomedical big data, least squares is not the best way to go. But before I get there, here's a picture showing what least squares is actually doing. So here is an example with the response, y, and two predictors, x1 and x2. So the observations are shown as red circles. And the least squares fit to the data is a plane. It's that green-blue plane that you see in the middle here. And the reason that this is a plane is because we have two features. So instead of a least squares line, we have a least squares plane. And these black vertical bars here, they tell us how far yi is from the prediction for yi with our model. So in other words, this black vertical bar tells me the distance, yi minus yi hat. And so this blue least squares plane is actually exactly positioned such that the sum of yi minus yi hat squared is as small as possible.
In other words, so that the sum of squared black lines is as small as possible. And you can sort of imagine that if you shifted the plane up or down, or if you wiggled it a little bit, then the sum of squared black lines would actually increase. OK. You've probably seen least squares regression before. This is something that comes up like in a classical statistics class. But I want to talk now a little bit about the machine learning perspective on what we're doing.
So in machine learning when we talk about fitting a model, we say that we're fitting a model on a training set. So the training set refers to the data that you use to actually fit your model. And given that we fit a model on the training set, we can think about the error of our model on the training set. Usually we think about the mean squared error. And you've actually seen this before. This is just one over n times that residual sum of squares quantity that we saw a couple of slides ago.
So remember the way that we chose the coefficients, beta naught hat, beta 1 hat through beta P hat was actually just to make this quantity as small as possible. So we basically are guaranteed that the mean squared error is going to be a decently small on the training set, because we literally chose these coefficient estimates in order to make this mean squared error small. But what I'm trying to emphasize here is that this mean squared error, right now what I'm showing you is the mean squared error calculated on the training set. And moving forward, I'm going to call this the training error. So this mean squared error, it gives me a representation of how good my model is on the data that I use to fit my model. It does not give me an idea of how good my model would be hypothetically on data that I did not use to fit my model. So if you're familiar with R squared from a linear model, an R squared is just really closely related to the training error.
When R squared is big, your training error is small. There's really actually a one to one mapping between those two quantities. So it turns out that training error, and equivalently R squared, are not a great way to think about how good your model is. And the reason that they're not good is because they will always improve as we fit a more complex model. So like imagine that first you fit a model with three variables and you calculate your training error and then you say, hey actually maybe I should just add in one more variable to my model. Well your training error is going to go down. It's literally guaranteed to go down or maybe stay the same as you increase the number of variables. So training error, we can see right away, there's kind of a problem, because it's going to look better and better and better as we add more and more variables to our model.
And the problem basically is that training error quantifies the fit to the training data. So as you fit a more and more complex model to the training data, your model is going to look better and better on the training data-- actually to the point where if you fit a complex enough model, we can get a perfect fit to the training data with 0 error. But the problem here is that what we really care about is the model's performance not on the training data, but rather on a test set. So for me, a model is good not if it accurately describes the training data, but actually if it accurately describes the test set, because I don't really care about predicting the response on the data that I already collected, because I know the response on the data that I already collected. What I care about is predicting the response on a new observation for which the response is unknown that was not part of my training set.
So if you're coming up with some way to predict survival time on the basis of gene expression among patients with some disease, I already know the survival time for the patients that I'm using to fit my model. So doing a really great job predicting the survival time for the patients who are in my training set is really not useful. But what I need to do is do a really good job predicting survival time for a new patient who walks into my clinic tomorrow or next week. And that's really the idea behind the fact that the training error is getting at the wrong quantity. Instead, I need to be getting at the model's performance on a test set. So just to reiterate, as we fit a more complex model-- in particular, as we increase the number of variables-- the R squared of our linear model will always increase and the training error will always decrease. So that's just really important to realize, because it gives us an idea of the fact that
training error and R squared are telling us the wrong thing. So as I mentioned, what we care about is the performance of the model on observations not used to fit the model. And to quantify this a little bit more, what I really care about is the difference between the response for a test observation and the prediction for a test observation where here, y hat test is the predicted value or the fitted value that I'm going to get if I get a new
test observation and I plug the variables for that test observation into the model that I fit on the training set. So the test error is the average of y test minus y hat test squared over a bunch of test observations. And what I really want is a model for which the test error is small. And so we already saw the first two panels of this plot showing that as the complexity of your model increases, the R squared will increase and the training error will decrease.
But the really critical thing here is that the test error is not going to continue to decrease as you make your model more and more complex. Actually the test error is going to bottom out for some certain level of model complexity, which is indicated here using a red arrow. And after it bottoms out, it's going to start increasing. So you can see that in terms of test error, you don't just want to fit an arbitrarily complex model. In order to make the test error small, there's going to be some intermediate level of model complexity. And actually if you take one thing out of this webinar, it should be this figure. It should be an understanding of the fact that the only thing that matters is test error. Training error is just not relevant. And if you focus on fitting your supervised machine learning models in order to make the training error small, then you've just totally missed the boat, because you'll end up over
here with a tiny training error but a really substantial test error. So I just want to try to illustrate the problem with thinking about training error in a small example. So what I'm showing here on the left is just a small simulated dataset with a single feature, x, on the horizontal axis and a single response, y, on the vertical axis. And the observations are shown in red. And on the left is what you get if you just fit a least squares linear model to the data.
So in this example, there is like 20 observations, one feature. I fit a linear model to the data using least squares. And what I can see is that there's clearly like a nonzero training error here, because these red points aren't exactly on the black least squares line. So the training error is nonzero. But this looks like a pretty good model. I can imagine that if I had a test set, like if I were able to generate more observations from the same distribution that made the red observations, the test error would be pretty modest. This is a pretty decent fit to the data. But now in contrast, let's look over here on the right. So on the right, it's the exact same setup. But now instead of having 20 observations like we had on the left, we just have two observations on the right. And I'm, again, just fitting a linear model, just with one variable, x. But what I see now is that I got a perfect fit to this training data.
So my training error on the right is 0. So we look at this picture on the right and we see that I got 0 training error. But this model on the right, it does not sit well. This model doesn't look good. And if we imagine generating more observations from the same distribution, we'd see this actually is a really bad model. Those additional observations would actually be nowhere close to this black line, because we overfit the data here. We fit a model that was too complex given the amount of data.
With only two observations, you actually can't fit a linear model in a meaningful way. So over here on the right, we got 0 training error, but the test error would actually be a huge. So this is kind of just an intuitive explanation of the fact that training error is not the right thing to be looking at, because like on the right here, the training error is 0 but we've got a really big test error. And this is also an example on the right of the fact that even something that seems really simple like a least squares linear model can actually be too complex for the data, and in a little example with just two observations. So one thing that I just keep on referring to offhand is this notion of model complexity. Model complexity is actually central to absolutely everything in supervised learning. As model complexity increases, training error will decrease. And I've kind of offhand been talking about how we can increase the number of variables
in our model and then the model will get more complex. But actually, the notion of model complexity is much more general than just the number of variables in the model. And we're actually going to see in a few minutes when we look at rigid regression an example of a model that might include a lot of variables. But it's not such a complex model. So in other words, if it's convenient for you to think about model complexity as like the number of variables in the model, then you should just remember that that's one way you can think about model complexity. But it's often not the best way. And model complexity actually is more general than just the number of variables in your model. OK. So as model complexity increases, training error will decrease. But the test error might not. I already said this one. OK. So just to explain in a little more detail what's happening in terms of the model complexity,
there's really two things we need to think about when we think about how good a model is. So the first thing is bias. So the bias is beta hat. It's the average difference between beta and beta hat if we were to repeat the experiment a huge number of times. So bias is basically telling us, how good is our model on average? And as we said, in increasingly complex model, the bias is going to decrease. So this is really good, because I was just saying that as we get a more complex model,
on average, our model is going to be better and better and better. And that's great, because I want a really good model. So that suggests that I should fit a really complex model. But there's a problem, which is that it turns out I don't just care about bias. I also care about something called variance. And the variance of beta hat is the amount by which the beta hats will differ across the experiments. So imagine that I repeated an experiment a huge number of times. And I calculated beta hat every time. So the variance of beta hat is just the amount by which these estimates of beta would vary across the experiments. And the problem is that if I fit an increasingly complex model, the variance of beta hat is going to increase. And the test error, which is the thing that I really care about, is actually the square of the bias plus the variance. So the thing that happens here is that there's a trade off between bias and variance, because
as I increase the model complexity, the squared bias is going to decrease. So that's really good. But the variance is going to increase. And that's really bad. And so typically, if your model complexity is really, really, really low, your bias is going to be huge. And your variance is going to be small. But the sum of something huge and the sum of something small is still huge. So that's bad. And if you fit a super complex model, the bias is going to be really small.
But the variance will be huge. And again, the sum of something small plus huge is really huge. And typically, for some intermediate level of model complexity, you can make both of these relatively small simultaneously. And that'll make your test error small. And that's the sweet spot. So what we want is a level of model complexity that makes this sum, and consequently the test error, small. So in other words, we need a model with small bias and also small variance.
So you can see this bias-variance trade off in a picture. Model complexity from low to high is on the x-axis. Error is on the y-axis. You can see training error in blue, test error in red. So you can see the training error is monotone decreasing in model complexity. But the test error is not. The smallest test error is happening right here for an intermediate level of model complexity. And this figure also shows you that as you get a more complex model, the bias is decreasing.
But the variance is increasing. And for a less complex model, the bias is increasing and the variance is decreasing. So hopefully by now you are on the same page as me that test set error is the thing that we care about. So if we want to make sure that we're fitting a model with a small test set error, then we need a way to estimate the test set error. And basically there are three ways that we can estimate the test error. And as you'll see, they're actually all pretty similar.
So the first one is the validation set approach. And the idea behind the validation set approach is that you have your n observations. You split them into two sets. Train your model on one set and then calculate the test error on the other set. So the validation set approach is great. It's very easy to implement. The only problem is that you arbitrarily are splitting your data into two sets. And if you had done a different split of the data, you might get a slightly different estimate
of test error. People don't really like that. So to overcome that problem, we can do an approach called leave-one-out cross-validation. And the idea behind leave-one-out cross-validation is that we have our n observations. We fit a model using all but the first observation. So you can see we're leaving out the first observation in this top row here, fitting a model on the remaining observations, and then calculate the test error on the first observation. Next we fit a model on all but the second observation and evaluate the test error on the second observation. And so on. We do this n times. Each time, we fit the model on all but one of the observations. And we test on the remaining observation. Then we average the error rates that we get. And that gives us a really good estimate of the test error. So leave-one-out cross-validation is really nice. It's very systematic. It feels thorough, because we leave out every observation one at a time.
But the problem is that often, it's computationally undesirable, because we have to fit n models in order to get an estimate of our model complexity. And if n is a really big number, like we have a really big sample size in our study, then this isn't going to fly. So typically, the preferred approach is something called k-fold cross-validation. And here I'm showing you an example of k-fold cross-validation with k equals five. And the idea is that we split the observations into five sets.
And we're repeatedly going to train the model on four sets and evaluate its performance on the fifth. So for example right here, in blue, you can see my training set. I'm going to fit the model on the training set, evaluate the test error on this test set in orange. And then I'm going to repeat. But I'm going to be changing each time which set of observations I'm considering to be the test set. Average those five error rates. And that gives us an estimate of our test error.
So it's really up to you how you want to estimate test error. Leave-one-out cross-validation, the validation set approach, and k-fold cross-validation are all perfectly fine. The important thing is that when you fit a model, you need to check what its test error is, because you don't want to accidentally come up with a model that has a great training error but a very poor test error. OK. So far, we've talked about linear models that we fit using least squares.
But as I have alluded to, often least squares regression is not a great choice of model. And this can basically happen for two reasons. It can happen because the least squares model is not complex enough or because it's too complex. So I'm going to talk in a couple minutes about what we do if a least squares model is not complex enough for our data. But actually typically, what we see in big biomedical data is that the least squares model is too complex in the sense that there are too many features relative to the number of observations. So for example, suppose that I want to predict a patient's response to chemotherapy. And my predictors are things like the expression levels for a whole lot of genes, maybe for 20,000 genes, protein levels where I've assayed some huge number of proteins, or maybe I've done DNA sequencing on genes that are potentially implicated in breast cancer.
So in each of these situations, chances are we have a huge number of features or variables that we're using as predictors in our model. And if we fit a least squares model in this setting, we're actually going to get really bad test error. Our training error would look awesome, but the test error would be really bad, because we'd be overfitting the data. So if we fit a model that's too complex, then that's known as overfitting. Basically a model that has a low training error but a high test error is a model that we've overfit to the data. So typically, least squares is going to overfit the data in the context of big biomedical data. And so what we need is an alternative to least squares. We need a way to fit a linear model, but somehow get less complexity than we get if we fit least squares using the P variables. So ridge regression and the lasso are two methods for fitting a linear model that are
alternatives to least squares. And they yield a less complex model by shrinking the regression coefficients. And so as we're going to see, these are really just minor modifications to the least squares fitting approach. But they can give us much better results in cases when least squares fits too complex of a model. So ridge regression and the lasso are examples of what are known as regularization approaches or penalization approaches. And there are many other examples of such methods.
But I'm just going to focus on ridge regression and the lasso today, because those are the two best known approaches. I also just want to mention that these regularization approaches are actually a topic of extensive contemporary research in statistical machine learning today. OK. So first of all, ridge regression. The idea behind ridge regression is that we're looking for coefficients, beta naught through beta P, that minimize our residual sum of squares.
So this is just like what we had in the least squares setting. But also there's an additional term here, which is a tuning parameter lambda times the sum over the features of beta j squared. So again, this is just like the least squares optimization problem except that now there is an additional term. And so here, lambda is a non-negative tuning parameter. We can see right away that if lambda is 0, then this term just disappears and we're just fitting a least squares model.
But as we increase lambda, this term is going to tend to decrease. The sum of beta j hat squared is going to decrease. And basically the way that you can think about it is that there are two terms. This first term quantifies how well I'm fitting the training data. And this term quantifies how complex my model is. You can actually think of sum of beta j squared as a way to quantify your model complexity. So when lambda is really small or when lambda is 0, I'm just focusing on getting a really
good fit to the training data with this lost term. But when lambda is bigger, I'm going to be focusing more on getting a really simple model or a model that's not too complex with this penalty term. And actually in the limit, when lambda becomes arbitrarily large, you can see that these beta j's are going to actually approach 0, because if lambda is just super huge, then you're going to want your sum of beta j squares to be really tiny so that this penalty term doesn't get too big.
So you can see that lambda really controls the complexity of your model. When lambda is really big, you're going to get a really, really, really simple model with just tiny coefficient estimates. So this plot gives you a graphical representation of what ridge regression gives you as you vary the tuning parameter, lambda. So here I am showing lambda on the x-axis. The y-axis shows coefficients. And this is a little example data set with a bunch of features, most of which are small.
And those are shown in gray. And then four features that are larger are shown in various colors. So in order to understand this figure, let's take a look over here all the way on the left side where my cursor is. So lambda is around 0.01 here. And when lambda is 0.01, like the black coefficient is around negative 280, the red one is around 420. But as I increase lambda, for example as I make lambda equal to one right here, the black coefficient is slightly smaller.
The red coefficient is slightly smaller. OK. These coefficients are slightly smaller. And as I make lambda really big, take a look over here. Like when lambda is 100, now all the coefficients are quite a bit smaller. And over here where lambda is 10,000, all the coefficients are really close to 0. So when we perform ridge regression, our results are actually a function of this tuning parameter, lambda. And if you want to know what would happen with a given value of lambda, you just go to that position on the x-axis. And you look at the value of the coefficients for that particular lambda. So over here, we're fitting a really simple model with very low variance, because all the coefficients are 0. That's very low variance, but it's going to have big bias. So overall, the test error is going to be really big. Over here, probably the test error is going to be big also, because this model has low bias but high variance over here. And over here, things are more intermediate.
So probably the best value of the test error is going to be around here. But in order to find out, we'd have to use one of the approaches that I mentioned earlier for estimating the test error. So one thing that I want you to notice is that over here when lambda is really big, all those coefficients become very close to 0. But they're actually not exactly 0. Maybe they're like 10 to the negative three or something. We can't quite see in this figure. But none of the coefficients are exactly 0. They're just becoming closer and closer to 0. So in practice, when you're performing ridge regression, you're going to choose the tuning parameter in order to minimize the test error. And then the value of lambda that we want is the one with the smallest test error. And then we would run ridge regression on the full data using that same value of lambda. So what I mean by this is like suppose you're choosing lambda using the validation set approach.
So you fit the model on half of the observations for a bunch of values of lambda, evaluate each of those models on the other half of the observations, identify the value of lambda that gave you the smallest error rate. And now using that value of lambda, you need to rerun ridge regression on all of the observations. So the important thing to know about ridge regression is that while it can give us a model that's not very complex when lambda is large, that final model is going to contain
all of the variables no matter what. And sometimes this isn't desirable. So like imagine that you're trying to come up with a way to use gene expression values to predict a patient's survival time. You might want to develop that model using all 20,000 genes. But at the end of the day, you might want your final model to only involve a handful of genes, because a final model involving just a handful of genes would be easier to translate into clinical practice, for example.
It would be less expensive to come up with a diagnostic involving just 20 genes rather than a diagnostic involving 20,000 genes. So if we want to fit a model that involves just a subset of the features, then ridge regression isn't going to take us there. So instead, we can do what's called the lasso. A lasso is just a small tweak to ridge regression that gives us a model with mostly 0 elements. So in other words, the resulting model is sparse. And another way to phrase this is we can say that the lasso performs feature selection. So this is the optimization problem for the lasso. We're going to look for coefficients that minimize, once again, this residual sum of squares term that by now you've seen a lot of times, plus a penalty. This looks just like the ridge regression penalty with just one difference. In ridge regression, we had a beta j squared term here. Now I've got an absolute value of beta j.
And this is a really small difference. And it seems like it shouldn't matter. But actually the difference is small, but important, because whereas ridge regression never gives you any coefficient estimates exactly equal to 0, the lasso will. So like ridge regression, we've got this tuning parameter, lambda, that controls model complexity. When lambda equals 0, we're just back at least squares. But most importantly, for an intermediate value of lambda, we can actually get some coefficient estimates that are exactly equal to 0. And that's the whole reason to ever do the lasso is if you want coefficient estimates that are exactly 0-- in other words, if you want a sparse model. So when lambda is very large, the sum of the beta j hats will be exactly 0. In contrast to ridge regression, where the best you'll ever see is the sum of the beta j hats might be close to 0. So we saw a figure like this for ridge regression. And now here's the same figure for the lasso.
And what I want you to notice here is for example, check out what happens when lambda is 500. So if lambda equals 500, the black coefficient is nonzero. The yellow, red, and blue ones are nonzero. But all of these gray coefficients have estimates that are exactly 0. So right here with lambda equals 500, my lasso model only involves four features. So this is really nice, because it means that, again, if I'm trying to develop a predictor of response to chemotherapy, my predictive model here only involves four of my 20,000 features. And that's pretty great in a lot of settings. If instead I had lambda equals 5,000, now I have a fitted model that only involves the blue feature. All the other features are exactly 0. So again, as we saw with ridge regression, I've got to choose this parameter, lambda. The way that I typically would choose it would be based on estimating the test error using
one of those approaches that we saw a few minutes ago. And that would tell us what value of lambda to use. But for example, if that approach told us to choose lambda over here, then our final model would involve only four features. OK. So that's it for ridge regression and the lasso. I want to just briefly very fast mention another approach for fitting a linear model that you may have heard of. And this is called principle components regression. And it's really a different take on how we can fit a less complex model than what we get using least squares. So the idea behind principal components regression is as usual, we have n observations and P features. What that means is that our observations live in a P dimensional space. But it turns out that not all those dimensions are equally useful. So for example, some of the dimensions might be completely redundant with each other.
And that would be the case if our features are highly correlated. Other dimensions might be uninformative. And that would correspond to noise features. So the question is, can we find a low dimensional representation of the variables that captures most of the action? So instead of having to deal with P dimensions, can we actually just find n dimensions that do a pretty good job of capturing what's happening in the data? So this is broadly referred to as a dimension reduction approach.
There are a lot of dimension reduction approaches out there. The two best known such approaches are principal components regression and partial least squares. And right now, I'm just going to mention principal components regression. But if you've heard of partial least squares, then it's a similar type of approach. So the idea behind principal components regression is that I'm going to look for the variables that I'll call z1 through zn. And these are linear combinations of my original P predictors. So in other words, I'm looking for coefficients, [INAUDIBLE] mj, such that zm is just a linear combination of x's. And then once I've identified these new dimensions, z1 through zm, I'm just going to fit a least squares model. But instead of fitting the model using my original features, I'm going to fit the model with my new features, z1 through zm. So this is really simple.
I'm just telling you do least squares, but don't use the original features. Use new features. OK. Well what are these new features supposed to be? Well these new features should be the principal components of the data. So I'm telling you to take the first m principal components of your data. Fit a least squares model using those as features. And you're done. You just did principal components regression. OK. So what are principal components? Well you're going to see these in more detail next week in the webinar on unsupervised learning. But the idea behind principal components is that they're linear combinations of your variables that contain as much variability of your data as possible. So they're basically just a low dimensional representation of your data that captures as much of the variability as possible. So in an example with two variables and observations shown in purple, this green line represents
the first principal component. And you can see the first principal component is sort of the direction along which your data varies the most. So if I had to summarize these purple observations in just one dimension, then this green line would give me the best possible summary. So the idea is when you do principal components regression, you're actually getting a model that's linear in the original predictors. So it is a linear model in the original predictors. But it's not the same as the least squares model that you get if you ran these squares on the original predictors. You're actually getting a much less complex model, provided that m is less than P. It's important to know that principal components regression doesn't involve feature selection in the sense that all of the original predictors are involved in your final model. The number of principal components that you have controls model complexity.

Ask anything
We've read this whole study set, so questions about it get the best answers.