3.7 Regularization to Reduce Overfitting | The problem of Overfitting -[Machine Learning|Andrew Ng]
 Now you've seen a couple of different learning algorithms, linear regression and logistic regression. They work well for many tasks, but sometimes in an application the algorithm could run into a problem called overfitting, which can cause it to perform poorly. What I'd like to do in this video is to show you what is overfitting, as well as a closely related almost opposite problem called underfitting. And in the next videos after this, I'll share with you some techniques for addressing overfitting. In particular, there's a method called regularization. Very useful technique, I use it all the time, but regularization will help you minimize this overfitting problem and get your learning algorithms to work much better. So let's take a look at what is overfitting. To help us understand what is overfitting, let's take a look at a few examples. Let's go back to our original example of predicting housing prices with linear regression, where you want to predict the price as a function of the size of a house. To help us understand what is overfitting, let's take a look at a linear regression example. And I'm going to go back to our original running example of predicting housing prices with linear regression. Suppose your data set looks like this, with the input feature x being the size of the house and the value y they're trying to predict the price of the house. One thing you could do is fit a linear function to this data. And if you do that, you get a straight line fit to the data that maybe looks like this. But this isn't a very good model. Looking at the data, it seems pretty clear that as the size of the house increases, the housing prices kind of flatten out. So this algorithm does not fit the training data very well. The technical term for this is the model is underfitting the training data. Another term is the algorithm has high bias. You may have read in the news about some learning algorithms, really unfortunately, demonstrating bias against certain ethnicities or certain genders. In machine learning, the term bias has multiple meanings. Checking learning algorithms for bias based on characteristics such as gender or ethnicity is absolutely critical. But the term bias has a second technical meaning as well, which is the one I'm using here, which is if the algorithm has underfit the data, meaning that it's just not even able to fit the training set that well, that there's a clear pattern in the training data that the algorithm is just unable to capture. Another way to think of this form of bias is as if the learning algorithm has a very strong preconception or we say a very strong bias that the housing prices are going to be a completely linear function of the size, despite data to the contrary. So this preconception that the data is linear causes it to fit a straight line that fits the data poorly, leading it to underfit the data. Now let's look at a second variation of a model, which is if you instead fit a quadratic function to the data with two features, x and x squared, then when you fit the parameters w1 and w2, you can get a curve that fits the data somewhat better. Maybe it looks like this. So if you were to get a new house that's not in this set of five training examples, this model would probably do quite well on that new house. So if you're a real estate agent, the idea that you want your learning algorithm to do well, even on examples that are not on the training set, that's called generalization. Technically, we say that you want your learning algorithm to generalize well, which means to make good predictions even on brand new examples that it has never seen before. So this quadratic model seems to fit the training set not perfectly, but pretty well. And I think it will generalize well to new examples. Now let's look at the other extreme. What if you were to fit a fourth order polynomial to the data? So you have x, x squared, x cubed, and x to the fourth all as features. With this fourth order polynomial, you can actually fit the curve that passes through all five of the training examples exactly. And you might get a curve that looks like this. This on one hand seems to do an extremely good job fitting the training data, because it passes through all of the training data perfectly. In fact, you'll be able to choose parameters that will result in the cost function being exactly equal to zero, because the errors are zero on all five training examples. But this is a very wiggly curve. It's going up and down all over the place. And if you have this house size right here, the model would predict that this house is cheaper than houses that are smaller than it. So we don't think that this is a particularly good model for predicting housing prices. The technical term is that we'll say this model has overfit the data, or this model has an overfitting problem. Because even though it fits the training set very well, it has fit the data almost too well, hence is overfit. And it does not look like this model will generalize to new examples that has never seen before. Another term for this is that the algorithm has high variance. In machine learning, many people will use the terms overfit and high variance almost interchangeably, and we use the terms underfit and high bias almost interchangeably. The intuition behind overfitting or high variance is that the algorithm is trying very, very hard to fit every single training example. And it turns out that if your training set were just even a little bit different, say one house was priced just a little bit more, a little bit less, then the function that the algorithm fits could end up being totally different. So if two different machine learning engineers were to fit this fourth order polynomial model to just slightly different data sets, they could end up with totally different predictions or highly variable predictions. And that's why we say the algorithm has high variance. Using this rightmost model with the one in the middle for the same house, it seems the middle model gives the much more reasonable prediction for price. There isn't really a name for this case in the middle, but I'm just going to call this just right because it is neither underfit nor overfit. So we can say that the goal of machine learning is to find a model that hopefully is neither underfitting nor overfitting. In other words, hopefully a model that has neither high bias nor high variance. When I think about underfitting and overfitting, high bias and high variance, I'm sometimes reminded of the children's story of Goldilocks and the three bears. In this children's tale, a girl called Goldilocks visits the home of a bear family. There's a bowl of porridge that's too cold to taste and so that's no good. There's also a bowl of porridge that's too hot to eat. So that's no good either. But there's a bowl of porridge that is neither too cold nor too hot. The temperature is in the middle, which is just right to eat. So to recap, if you have too many features, like the full water polynomial on the right, then the model may fit the training set well, but almost too well or overfit in the high variance. On the flip side, if you have too few features, then in this example, like the one on the left, it underfits and has high bias. And in this example, using quadratic features x and x squared, that seems to be just right. So far, we've looked at underfitting and overfitting for linear regression model. Similarly, overfitting applies to classification as well. Here's a classification example with two features x1 and x2, where x1 is maybe the tumor size and x2 is the age of patient. And we're trying to classify if a tumor is malignant or benign, as denoted by these crosses and circles. One thing you can do is fit a logistic regression model, just a simple model like this, where as usual, g is the sigmoid function, and this term here inside is z. So if you do that, you end up with a straight line as the decision boundary. This is the line where z is equal to zero that separates the positive and negative examples. This straight line doesn't look terrible. It looks kind of okay, but it doesn't look like a very good fit to the data either. So this is an example of underfitting or of high bias. Let's look at another example. If you were to add to your features these quadratic terms, then z becomes this new term in the middle and the decision boundary, that is where z equals zero, can look more like this, more like an ellipse or part of an ellipse. And this is a pretty good fit to the data, even though it does not perfectly classify every single training sample in the training set. Notice how some of these crosses get classified among the circles. But this model looks pretty good. I'm going to call it just right, and it looks like this will generalize pretty well to new patients. And finally, at the other extreme, if you were to fit a very high order polynomial with many, many features like these, then the model may try really hard and can't hold or twist itself to find a decision boundary that fits your training data perfectly. Having all these higher order polynomial features allows the algorithm to choose this really overly complex decision boundary. If the features are tumor size and age, and you're trying to classify tumors as malignant or benign, then this doesn't really look like a very good model for making predictions. So once again, this is an instance of overfitting and high variance, because this model, despite doing very well in the training set, doesn't look like it will generalize well to new examples. So now you've seen how an algorithm can underfit or have high bias or overfit and have high variance. You may want to know how you can get a model that is just right. In the next video, we'll look at some ways you can address the issue of overfitting. We'll also touch on some ideas relevant for accessing underfitting. Let's go on to the next video.
[{"start": 0.0, "end": 7.28, "text": " Now you've seen a couple of different learning algorithms, linear regression and logistic"}, {"start": 7.28, "end": 8.28, "text": " regression."}, {"start": 8.28, "end": 13.88, "text": " They work well for many tasks, but sometimes in an application the algorithm could run"}, {"start": 13.88, "end": 19.6, "text": " into a problem called overfitting, which can cause it to perform poorly."}, {"start": 19.6, "end": 24.84, "text": " What I'd like to do in this video is to show you what is overfitting, as well as a closely"}, {"start": 24.84, "end": 28.88, "text": " related almost opposite problem called underfitting."}, {"start": 28.88, "end": 34.879999999999995, "text": " And in the next videos after this, I'll share with you some techniques for addressing overfitting."}, {"start": 34.879999999999995, "end": 37.84, "text": " In particular, there's a method called regularization."}, {"start": 37.84, "end": 42.64, "text": " Very useful technique, I use it all the time, but regularization will help you minimize"}, {"start": 42.64, "end": 47.4, "text": " this overfitting problem and get your learning algorithms to work much better."}, {"start": 47.4, "end": 51.879999999999995, "text": " So let's take a look at what is overfitting."}, {"start": 51.879999999999995, "end": 58.84, "text": " To help us understand what is overfitting, let's take a look at a few examples."}, {"start": 58.84, "end": 65.24000000000001, "text": " Let's go back to our original example of predicting housing prices with linear regression, where"}, {"start": 65.24000000000001, "end": 70.16, "text": " you want to predict the price as a function of the size of a house."}, {"start": 70.16, "end": 78.28, "text": " To help us understand what is overfitting, let's take a look at a linear regression example."}, {"start": 78.28, "end": 82.80000000000001, "text": " And I'm going to go back to our original running example of predicting housing prices with"}, {"start": 82.80000000000001, "end": 85.5, "text": " linear regression."}, {"start": 85.5, "end": 91.12, "text": " Suppose your data set looks like this, with the input feature x being the size of the"}, {"start": 91.12, "end": 96.56, "text": " house and the value y they're trying to predict the price of the house."}, {"start": 96.56, "end": 101.48, "text": " One thing you could do is fit a linear function to this data."}, {"start": 101.48, "end": 107.64, "text": " And if you do that, you get a straight line fit to the data that maybe looks like this."}, {"start": 107.64, "end": 110.36, "text": " But this isn't a very good model."}, {"start": 110.36, "end": 115.76, "text": " Looking at the data, it seems pretty clear that as the size of the house increases, the"}, {"start": 115.76, "end": 119.08, "text": " housing prices kind of flatten out."}, {"start": 119.08, "end": 124.2, "text": " So this algorithm does not fit the training data very well."}, {"start": 124.2, "end": 131.4, "text": " The technical term for this is the model is underfitting the training data."}, {"start": 131.4, "end": 136.57999999999998, "text": " Another term is the algorithm has high bias."}, {"start": 136.58, "end": 142.52, "text": " You may have read in the news about some learning algorithms, really unfortunately, demonstrating"}, {"start": 142.52, "end": 146.44, "text": " bias against certain ethnicities or certain genders."}, {"start": 146.44, "end": 152.08, "text": " In machine learning, the term bias has multiple meanings."}, {"start": 152.08, "end": 158.4, "text": " Checking learning algorithms for bias based on characteristics such as gender or ethnicity"}, {"start": 158.4, "end": 161.20000000000002, "text": " is absolutely critical."}, {"start": 161.2, "end": 167.56, "text": " But the term bias has a second technical meaning as well, which is the one I'm using here,"}, {"start": 167.56, "end": 172.28, "text": " which is if the algorithm has underfit the data, meaning that it's just not even able"}, {"start": 172.28, "end": 178.12, "text": " to fit the training set that well, that there's a clear pattern in the training data that"}, {"start": 178.12, "end": 181.79999999999998, "text": " the algorithm is just unable to capture."}, {"start": 181.79999999999998, "end": 187.32, "text": " Another way to think of this form of bias is as if the learning algorithm has a very"}, {"start": 187.32, "end": 192.64, "text": " strong preconception or we say a very strong bias that the housing prices are going to"}, {"start": 192.64, "end": 199.94, "text": " be a completely linear function of the size, despite data to the contrary."}, {"start": 199.94, "end": 206.24, "text": " So this preconception that the data is linear causes it to fit a straight line that fits"}, {"start": 206.24, "end": 210.92, "text": " the data poorly, leading it to underfit the data."}, {"start": 210.92, "end": 219.32, "text": " Now let's look at a second variation of a model, which is if you instead fit a quadratic"}, {"start": 219.32, "end": 225.6, "text": " function to the data with two features, x and x squared, then when you fit the parameters"}, {"start": 225.6, "end": 233.0, "text": " w1 and w2, you can get a curve that fits the data somewhat better."}, {"start": 233.0, "end": 235.88, "text": " Maybe it looks like this."}, {"start": 235.88, "end": 242.57999999999998, "text": " So if you were to get a new house that's not in this set of five training examples, this"}, {"start": 242.57999999999998, "end": 247.06, "text": " model would probably do quite well on that new house."}, {"start": 247.06, "end": 251.74, "text": " So if you're a real estate agent, the idea that you want your learning algorithm to do"}, {"start": 251.74, "end": 258.12, "text": " well, even on examples that are not on the training set, that's called generalization."}, {"start": 258.12, "end": 263.6, "text": " Technically, we say that you want your learning algorithm to generalize well, which means"}, {"start": 263.6, "end": 269.04, "text": " to make good predictions even on brand new examples that it has never seen before."}, {"start": 269.04, "end": 274.76000000000005, "text": " So this quadratic model seems to fit the training set not perfectly, but pretty well."}, {"start": 274.76000000000005, "end": 278.92, "text": " And I think it will generalize well to new examples."}, {"start": 278.92, "end": 281.96000000000004, "text": " Now let's look at the other extreme."}, {"start": 281.96000000000004, "end": 286.04, "text": " What if you were to fit a fourth order polynomial to the data?"}, {"start": 286.04, "end": 292.44, "text": " So you have x, x squared, x cubed, and x to the fourth all as features."}, {"start": 292.44, "end": 296.16, "text": " With this fourth order polynomial, you can actually fit the curve that passes through"}, {"start": 296.16, "end": 299.64, "text": " all five of the training examples exactly."}, {"start": 299.64, "end": 303.62, "text": " And you might get a curve that looks like this."}, {"start": 303.62, "end": 308.96, "text": " This on one hand seems to do an extremely good job fitting the training data, because"}, {"start": 308.96, "end": 312.84, "text": " it passes through all of the training data perfectly."}, {"start": 312.84, "end": 317.48, "text": " In fact, you'll be able to choose parameters that will result in the cost function being"}, {"start": 317.48, "end": 324.40000000000003, "text": " exactly equal to zero, because the errors are zero on all five training examples."}, {"start": 324.40000000000003, "end": 326.96000000000004, "text": " But this is a very wiggly curve."}, {"start": 326.96000000000004, "end": 329.88, "text": " It's going up and down all over the place."}, {"start": 329.88, "end": 335.20000000000005, "text": " And if you have this house size right here, the model would predict that this house is"}, {"start": 335.20000000000005, "end": 339.44, "text": " cheaper than houses that are smaller than it."}, {"start": 339.44, "end": 346.24, "text": " So we don't think that this is a particularly good model for predicting housing prices."}, {"start": 346.24, "end": 352.52, "text": " The technical term is that we'll say this model has overfit the data, or this model"}, {"start": 352.52, "end": 355.08, "text": " has an overfitting problem."}, {"start": 355.08, "end": 359.64, "text": " Because even though it fits the training set very well, it has fit the data almost too"}, {"start": 359.64, "end": 362.12, "text": " well, hence is overfit."}, {"start": 362.12, "end": 366.58, "text": " And it does not look like this model will generalize to new examples that has never"}, {"start": 366.58, "end": 368.6, "text": " seen before."}, {"start": 368.6, "end": 374.64, "text": " Another term for this is that the algorithm has high variance."}, {"start": 374.64, "end": 379.64, "text": " In machine learning, many people will use the terms overfit and high variance almost"}, {"start": 379.64, "end": 386.28, "text": " interchangeably, and we use the terms underfit and high bias almost interchangeably."}, {"start": 386.28, "end": 390.88, "text": " The intuition behind overfitting or high variance is that the algorithm is trying very, very"}, {"start": 390.88, "end": 394.64, "text": " hard to fit every single training example."}, {"start": 394.64, "end": 399.64, "text": " And it turns out that if your training set were just even a little bit different, say"}, {"start": 399.64, "end": 404.91999999999996, "text": " one house was priced just a little bit more, a little bit less, then the function that"}, {"start": 404.91999999999996, "end": 409.2, "text": " the algorithm fits could end up being totally different."}, {"start": 409.2, "end": 416.03999999999996, "text": " So if two different machine learning engineers were to fit this fourth order polynomial model"}, {"start": 416.03999999999996, "end": 420.36, "text": " to just slightly different data sets, they could end up with totally different predictions"}, {"start": 420.36, "end": 423.15999999999997, "text": " or highly variable predictions."}, {"start": 423.15999999999997, "end": 428.76, "text": " And that's why we say the algorithm has high variance."}, {"start": 428.76, "end": 434.36, "text": " Using this rightmost model with the one in the middle for the same house, it seems the"}, {"start": 434.36, "end": 439.15999999999997, "text": " middle model gives the much more reasonable prediction for price."}, {"start": 439.15999999999997, "end": 443.44, "text": " There isn't really a name for this case in the middle, but I'm just going to call this"}, {"start": 443.44, "end": 448.48, "text": " just right because it is neither underfit nor overfit."}, {"start": 448.48, "end": 453.56, "text": " So we can say that the goal of machine learning is to find a model that hopefully is neither"}, {"start": 453.56, "end": 455.84, "text": " underfitting nor overfitting."}, {"start": 455.84, "end": 462.91999999999996, "text": " In other words, hopefully a model that has neither high bias nor high variance."}, {"start": 462.91999999999996, "end": 468.32, "text": " When I think about underfitting and overfitting, high bias and high variance, I'm sometimes"}, {"start": 468.32, "end": 474.28, "text": " reminded of the children's story of Goldilocks and the three bears."}, {"start": 474.28, "end": 481.0, "text": " In this children's tale, a girl called Goldilocks visits the home of a bear family."}, {"start": 481.0, "end": 486.84, "text": " There's a bowl of porridge that's too cold to taste and so that's no good."}, {"start": 486.84, "end": 490.52, "text": " There's also a bowl of porridge that's too hot to eat."}, {"start": 490.52, "end": 492.28, "text": " So that's no good either."}, {"start": 492.28, "end": 496.0, "text": " But there's a bowl of porridge that is neither too cold nor too hot."}, {"start": 496.0, "end": 500.76, "text": " The temperature is in the middle, which is just right to eat."}, {"start": 500.76, "end": 506.12, "text": " So to recap, if you have too many features, like the full water polynomial on the right,"}, {"start": 506.12, "end": 511.32, "text": " then the model may fit the training set well, but almost too well or overfit in the high"}, {"start": 511.32, "end": 512.72, "text": " variance."}, {"start": 512.72, "end": 517.24, "text": " On the flip side, if you have too few features, then in this example, like the one on the"}, {"start": 517.24, "end": 520.68, "text": " left, it underfits and has high bias."}, {"start": 520.68, "end": 527.78, "text": " And in this example, using quadratic features x and x squared, that seems to be just right."}, {"start": 527.78, "end": 532.84, "text": " So far, we've looked at underfitting and overfitting for linear regression model."}, {"start": 532.84, "end": 537.0, "text": " Similarly, overfitting applies to classification as well."}, {"start": 537.0, "end": 543.2800000000001, "text": " Here's a classification example with two features x1 and x2, where x1 is maybe the tumor size"}, {"start": 543.2800000000001, "end": 546.38, "text": " and x2 is the age of patient."}, {"start": 546.38, "end": 552.32, "text": " And we're trying to classify if a tumor is malignant or benign, as denoted by these crosses"}, {"start": 552.32, "end": 554.2800000000001, "text": " and circles."}, {"start": 554.2800000000001, "end": 561.0, "text": " One thing you can do is fit a logistic regression model, just a simple model like this, where"}, {"start": 561.0, "end": 569.76, "text": " as usual, g is the sigmoid function, and this term here inside is z."}, {"start": 569.76, "end": 576.54, "text": " So if you do that, you end up with a straight line as the decision boundary."}, {"start": 576.54, "end": 582.5, "text": " This is the line where z is equal to zero that separates the positive and negative examples."}, {"start": 582.5, "end": 584.28, "text": " This straight line doesn't look terrible."}, {"start": 584.28, "end": 589.22, "text": " It looks kind of okay, but it doesn't look like a very good fit to the data either."}, {"start": 589.22, "end": 595.0, "text": " So this is an example of underfitting or of high bias."}, {"start": 595.0, "end": 597.12, "text": " Let's look at another example."}, {"start": 597.12, "end": 603.08, "text": " If you were to add to your features these quadratic terms, then z becomes this new term"}, {"start": 603.08, "end": 609.2, "text": " in the middle and the decision boundary, that is where z equals zero, can look more like"}, {"start": 609.2, "end": 613.52, "text": " this, more like an ellipse or part of an ellipse."}, {"start": 613.52, "end": 618.32, "text": " And this is a pretty good fit to the data, even though it does not perfectly classify"}, {"start": 618.32, "end": 621.6, "text": " every single training sample in the training set."}, {"start": 621.6, "end": 626.0400000000001, "text": " Notice how some of these crosses get classified among the circles."}, {"start": 626.0400000000001, "end": 627.88, "text": " But this model looks pretty good."}, {"start": 627.88, "end": 632.36, "text": " I'm going to call it just right, and it looks like this will generalize pretty well to new"}, {"start": 632.36, "end": 634.5600000000001, "text": " patients."}, {"start": 634.5600000000001, "end": 640.72, "text": " And finally, at the other extreme, if you were to fit a very high order polynomial with"}, {"start": 640.72, "end": 647.8800000000001, "text": " many, many features like these, then the model may try really hard and can't hold or twist"}, {"start": 647.88, "end": 654.04, "text": " itself to find a decision boundary that fits your training data perfectly."}, {"start": 654.04, "end": 660.04, "text": " Having all these higher order polynomial features allows the algorithm to choose this really"}, {"start": 660.04, "end": 664.04, "text": " overly complex decision boundary."}, {"start": 664.04, "end": 669.48, "text": " If the features are tumor size and age, and you're trying to classify tumors as malignant"}, {"start": 669.48, "end": 675.64, "text": " or benign, then this doesn't really look like a very good model for making predictions."}, {"start": 675.64, "end": 681.6, "text": " So once again, this is an instance of overfitting and high variance, because this model, despite"}, {"start": 681.6, "end": 687.98, "text": " doing very well in the training set, doesn't look like it will generalize well to new examples."}, {"start": 687.98, "end": 693.52, "text": " So now you've seen how an algorithm can underfit or have high bias or overfit and have high"}, {"start": 693.52, "end": 695.28, "text": " variance."}, {"start": 695.28, "end": 699.4, "text": " You may want to know how you can get a model that is just right."}, {"start": 699.4, "end": 704.84, "text": " In the next video, we'll look at some ways you can address the issue of overfitting."}, {"start": 704.84, "end": 709.32, "text": " We'll also touch on some ideas relevant for accessing underfitting."}, {"start": 709.32, "end": 736.32, "text": " Let's go on to the next video."}]