3.3 Classification | Decision boundary  --[Machine Learning | Andrew Ng]
 In the last video, you learned about the logistic regression model. Now, let's take a look at the decision boundary to get a better sense of how logistic regression is computing its predictions. To recap, here's how the logistic regression model's outputs are computed in two steps. In the first step, you compute z as w dot x plus b. Then you apply the sigmoid function g to this value z, and here again is the formula for the sigmoid function. Another way to write this is we can say f of x is equal to g, the sigmoid function, also called the logistic function, applied to w dot x plus b, where this is of course the value of z. And if you take the definition of the sigmoid function and plug in the definition of z, then you find that f of x is equal to this formula over here, 1 over 1 plus e to the negative z, where z is w x plus b. And you may remember we said in the previous video that we interpret this as the probability that y is equal to 1, given x, and with parameters w and b. And so this is going to be a number, like maybe 0.7 or 0.3. Now what if you want the learning algorithm to predict, is the value of y going to be 0 or 1? Well one thing you might do is set a threshold above which you predict y is 1, or you set y hat, the prediction, to be equal to 1, and below which you might say y hat, my prediction, is going to be equal to 0. So a common choice would be to pick a threshold of 0.5, so that if f of x is greater than or equal to 0.5, then predict y is 1, and we write that prediction as y hat equals 1, or if f of x is less than 0.5, then predict y is 0, or in other words, the prediction y hat is equal to 0. So now let's dive deeper into when the model would predict 1. In other words, when is f of x greater than or equal to 0.5? Well recall that f of x is just equal to g of z, and so f is greater than or equal to 0.5 whenever g of z is greater than or equal to 0.5. But when is g of z greater than or equal to 0.5? Well here's the sigmoid function over here, and so g of z is greater than or equal to 0.5 whenever z is greater than or equal to 0, right? That is, whenever z is on the right half of this axis. And finally, when is z greater than or equal to 0? Well z is equal to w dot x plus b, and so z is greater than or equal to 0 whenever w dot x plus b is greater than or equal to 0. So to recap, what you've seen here is that the model predicts 1 whenever w dot x plus b is greater than or equal to 0. And conversely, when w dot x plus b is less than 0, the algorithm predicts y is 0. So given this, let's now visualize how the model makes predictions. I'm going to take an example of a classification problem where you have two features, x1 and x2 instead of just one feature. Here's a training set where the little red crosses denote the positive examples, and the little blue circles denote negative examples. So the red crosses corresponds to y equals 1, and the blue circles correspond to y equals 0. So the logistic regression model will make predictions using this function, f of x equals g of z, where z is now this expression over here, w1 x1 plus w2 x2 plus b, because we have two features, x1 and x2. And let's just say for this example that the value of the parameters are w1 equals 1, w2 equals 1, and b equals negative 3. And let's now take a look at how logistic regression makes predictions. In particular, let's figure out when wx plus b is greater than or equal to 0, and when wx plus b is less than 0. To figure that out, there's a very interesting line to look at, which is when wx plus b is exactly equal to 0. It turns out that this line is also called the decision boundary, because that's the line where you're just almost neutral about whether y is 0 or y is 1. Now for the values of the parameters w1, w2, and b that we had written down above, this decision boundary is just x1 plus x2 minus 3. And so when is x1 plus x2 minus 3 equal to 0? Well that will correspond to the line x1 plus x2 equals 3. And that is this line shown over here. And so this line turns out to be the decision boundary, where if the features x are to the right of this line, logistic regression would predict 1, and to the left of this line, logistic regression would predict 0. In other words, what we have just visualized is the decision boundary for logistic regression when the parameters w1, w2, and b are 1, 1, and negative 3. Of course, if you had a different choice of the parameters, the decision boundary would be a different line. Now let's look at a more complex example where the decision boundary is no longer a straight line. As before, crosses denote the class y equals 1, and the little circles denote the class y equals 0. Earlier last week, you saw how to use polynomials in linear regression, and you can do the same in logistic regression. So let's set z to be w1 x1 squared plus w2 x2 squared plus b. With this choice of features, polynomial features into logistic regression, so f of x, which equals g of z, is now g of this expression over here. And let's say that we end up choosing w1 and w2 to be 1, and b to be negative 1. So z is equal to 1 times x1 squared plus 1 times x2 squared minus 1. And the decision boundary as before will correspond to when z is equal to 0, and so this expression will be equal to 0 when x1 squared plus x2 squared is equal to 1. And if you plot on the diagram on the left, the curve corresponding to x1 squared plus x2 squared equals 1, this turns out to be this circle. When x1 squared plus x2 squared is greater than or equal to 1, that's this area outside the circle, and that's when you predict y to be 1. Conversely, when x1 squared plus x2 squared is less than 1, that's this area inside the circle, and that's when you would predict y to be 0. So can we come up with even more complex decision boundaries than these? Yes you can. You can do so by having even higher order polynomial terms. Say z is w1 x1 plus w2 x2 plus w3 x1 squared plus w4 x1 x2 plus w5 x2 squared. Then it's possible you can get even more complex decision boundaries. The model can define decision boundaries such as this example, an ellipse that's like this, or with a different choice of the parameters. You can even get more complex decision boundaries which can look like functions that maybe look like that. So this is an example of an even more complex decision boundary than the ones we've seen previously, and this implementation of logistic regression will predict y equals 1 inside this shape, and outside the shape it will predict y equals 0. So with these polynomial features you can get very complex decision boundaries. In other words logistic regression can learn to fit pretty complex data. Although if you were to not include any of these higher order polynomials, so if the only features you use are x1 x2 x3 and so on, then the decision boundary for logistic regression will always be linear, will always be a straight line. In the upcoming optional lab you also get to see the code implementation of the decision boundary. In the example in the lab there will be two features so you can see the decision boundary as a line. So with these visualizations I hope that you now have a sense of the range of possible models you can get with logistic regression. Now that you've seen what f of x can potentially compute, let's take a look at how you can actually train a logistic regression model. We'll start by looking at the cost function for logistic regression and after that figure out how to apply gradient descent to it. Let's go on to the next video.
[{"start": 0.0, "end": 5.2, "text": " In the last video, you learned about the logistic regression model."}, {"start": 5.2, "end": 11.72, "text": " Now, let's take a look at the decision boundary to get a better sense of how logistic regression"}, {"start": 11.72, "end": 13.92, "text": " is computing its predictions."}, {"start": 13.92, "end": 21.04, "text": " To recap, here's how the logistic regression model's outputs are computed in two steps."}, {"start": 21.04, "end": 26.92, "text": " In the first step, you compute z as w dot x plus b."}, {"start": 26.92, "end": 33.120000000000005, "text": " Then you apply the sigmoid function g to this value z, and here again is the formula for"}, {"start": 33.120000000000005, "end": 35.980000000000004, "text": " the sigmoid function."}, {"start": 35.980000000000004, "end": 43.2, "text": " Another way to write this is we can say f of x is equal to g, the sigmoid function,"}, {"start": 43.2, "end": 50.160000000000004, "text": " also called the logistic function, applied to w dot x plus b, where this is of course"}, {"start": 50.160000000000004, "end": 53.120000000000005, "text": " the value of z."}, {"start": 53.12, "end": 59.879999999999995, "text": " And if you take the definition of the sigmoid function and plug in the definition of z,"}, {"start": 59.879999999999995, "end": 67.36, "text": " then you find that f of x is equal to this formula over here, 1 over 1 plus e to the"}, {"start": 67.36, "end": 72.08, "text": " negative z, where z is w x plus b."}, {"start": 72.08, "end": 77.47999999999999, "text": " And you may remember we said in the previous video that we interpret this as the probability"}, {"start": 77.47999999999999, "end": 82.36, "text": " that y is equal to 1, given x, and with parameters w and b."}, {"start": 82.36, "end": 87.84, "text": " And so this is going to be a number, like maybe 0.7 or 0.3."}, {"start": 87.84, "end": 93.24, "text": " Now what if you want the learning algorithm to predict, is the value of y going to be"}, {"start": 93.24, "end": 95.72, "text": " 0 or 1?"}, {"start": 95.72, "end": 102.72, "text": " Well one thing you might do is set a threshold above which you predict y is 1, or you set"}, {"start": 102.72, "end": 109.0, "text": " y hat, the prediction, to be equal to 1, and below which you might say y hat, my prediction,"}, {"start": 109.0, "end": 112.16, "text": " is going to be equal to 0."}, {"start": 112.16, "end": 119.08, "text": " So a common choice would be to pick a threshold of 0.5, so that if f of x is greater than"}, {"start": 119.08, "end": 126.96, "text": " or equal to 0.5, then predict y is 1, and we write that prediction as y hat equals 1,"}, {"start": 126.96, "end": 133.2, "text": " or if f of x is less than 0.5, then predict y is 0, or in other words, the prediction"}, {"start": 133.2, "end": 136.2, "text": " y hat is equal to 0."}, {"start": 136.2, "end": 140.12, "text": " So now let's dive deeper into when the model would predict 1."}, {"start": 140.12, "end": 145.68, "text": " In other words, when is f of x greater than or equal to 0.5?"}, {"start": 145.68, "end": 153.44, "text": " Well recall that f of x is just equal to g of z, and so f is greater than or equal to"}, {"start": 153.44, "end": 159.20000000000002, "text": " 0.5 whenever g of z is greater than or equal to 0.5."}, {"start": 159.20000000000002, "end": 163.8, "text": " But when is g of z greater than or equal to 0.5?"}, {"start": 163.8, "end": 171.08, "text": " Well here's the sigmoid function over here, and so g of z is greater than or equal to"}, {"start": 171.08, "end": 177.48000000000002, "text": " 0.5 whenever z is greater than or equal to 0, right?"}, {"start": 177.48000000000002, "end": 183.4, "text": " That is, whenever z is on the right half of this axis."}, {"start": 183.4, "end": 187.12, "text": " And finally, when is z greater than or equal to 0?"}, {"start": 187.12, "end": 196.24, "text": " Well z is equal to w dot x plus b, and so z is greater than or equal to 0 whenever w"}, {"start": 196.24, "end": 200.4, "text": " dot x plus b is greater than or equal to 0."}, {"start": 200.4, "end": 209.52, "text": " So to recap, what you've seen here is that the model predicts 1 whenever w dot x plus"}, {"start": 209.52, "end": 213.36, "text": " b is greater than or equal to 0."}, {"start": 213.36, "end": 223.36, "text": " And conversely, when w dot x plus b is less than 0, the algorithm predicts y is 0."}, {"start": 223.36, "end": 229.56, "text": " So given this, let's now visualize how the model makes predictions."}, {"start": 229.56, "end": 236.0, "text": " I'm going to take an example of a classification problem where you have two features, x1 and"}, {"start": 236.0, "end": 239.28000000000003, "text": " x2 instead of just one feature."}, {"start": 239.28, "end": 245.08, "text": " Here's a training set where the little red crosses denote the positive examples, and"}, {"start": 245.08, "end": 248.6, "text": " the little blue circles denote negative examples."}, {"start": 248.6, "end": 257.16, "text": " So the red crosses corresponds to y equals 1, and the blue circles correspond to y equals"}, {"start": 257.16, "end": 259.24, "text": " 0."}, {"start": 259.24, "end": 265.32, "text": " So the logistic regression model will make predictions using this function, f of x equals"}, {"start": 265.32, "end": 275.2, "text": " g of z, where z is now this expression over here, w1 x1 plus w2 x2 plus b, because we"}, {"start": 275.2, "end": 278.48, "text": " have two features, x1 and x2."}, {"start": 278.48, "end": 286.52, "text": " And let's just say for this example that the value of the parameters are w1 equals 1, w2"}, {"start": 286.52, "end": 291.0, "text": " equals 1, and b equals negative 3."}, {"start": 291.0, "end": 295.88, "text": " And let's now take a look at how logistic regression makes predictions."}, {"start": 295.88, "end": 301.28, "text": " In particular, let's figure out when wx plus b is greater than or equal to 0, and when"}, {"start": 301.28, "end": 304.8, "text": " wx plus b is less than 0."}, {"start": 304.8, "end": 311.08, "text": " To figure that out, there's a very interesting line to look at, which is when wx plus b is"}, {"start": 311.08, "end": 314.2, "text": " exactly equal to 0."}, {"start": 314.2, "end": 321.08, "text": " It turns out that this line is also called the decision boundary, because that's the"}, {"start": 321.08, "end": 327.59999999999997, "text": " line where you're just almost neutral about whether y is 0 or y is 1."}, {"start": 327.59999999999997, "end": 336.44, "text": " Now for the values of the parameters w1, w2, and b that we had written down above, this"}, {"start": 336.44, "end": 343.59999999999997, "text": " decision boundary is just x1 plus x2 minus 3."}, {"start": 343.6, "end": 350.12, "text": " And so when is x1 plus x2 minus 3 equal to 0?"}, {"start": 350.12, "end": 356.76000000000005, "text": " Well that will correspond to the line x1 plus x2 equals 3."}, {"start": 356.76000000000005, "end": 361.76000000000005, "text": " And that is this line shown over here."}, {"start": 361.76000000000005, "end": 369.26000000000005, "text": " And so this line turns out to be the decision boundary, where if the features x are to the"}, {"start": 369.26, "end": 375.96, "text": " right of this line, logistic regression would predict 1, and to the left of this line, logistic"}, {"start": 375.96, "end": 379.92, "text": " regression would predict 0."}, {"start": 379.92, "end": 386.88, "text": " In other words, what we have just visualized is the decision boundary for logistic regression"}, {"start": 386.88, "end": 392.98, "text": " when the parameters w1, w2, and b are 1, 1, and negative 3."}, {"start": 392.98, "end": 397.4, "text": " Of course, if you had a different choice of the parameters, the decision boundary would"}, {"start": 397.4, "end": 399.76, "text": " be a different line."}, {"start": 399.76, "end": 404.88, "text": " Now let's look at a more complex example where the decision boundary is no longer a straight"}, {"start": 404.88, "end": 406.32, "text": " line."}, {"start": 406.32, "end": 414.03999999999996, "text": " As before, crosses denote the class y equals 1, and the little circles denote the class"}, {"start": 414.03999999999996, "end": 417.84, "text": " y equals 0."}, {"start": 417.84, "end": 424.71999999999997, "text": " Earlier last week, you saw how to use polynomials in linear regression, and you can do the same"}, {"start": 424.72, "end": 427.58000000000004, "text": " in logistic regression."}, {"start": 427.58000000000004, "end": 437.18, "text": " So let's set z to be w1 x1 squared plus w2 x2 squared plus b."}, {"start": 437.18, "end": 442.42, "text": " With this choice of features, polynomial features into logistic regression, so f of x, which"}, {"start": 442.42, "end": 447.48, "text": " equals g of z, is now g of this expression over here."}, {"start": 447.48, "end": 456.54, "text": " And let's say that we end up choosing w1 and w2 to be 1, and b to be negative 1."}, {"start": 456.54, "end": 464.04, "text": " So z is equal to 1 times x1 squared plus 1 times x2 squared minus 1."}, {"start": 464.04, "end": 471.94, "text": " And the decision boundary as before will correspond to when z is equal to 0, and so this expression"}, {"start": 471.94, "end": 477.8, "text": " will be equal to 0 when x1 squared plus x2 squared is equal to 1."}, {"start": 477.8, "end": 483.76, "text": " And if you plot on the diagram on the left, the curve corresponding to x1 squared plus"}, {"start": 483.76, "end": 489.44, "text": " x2 squared equals 1, this turns out to be this circle."}, {"start": 489.44, "end": 494.74, "text": " When x1 squared plus x2 squared is greater than or equal to 1, that's this area outside"}, {"start": 494.74, "end": 499.56, "text": " the circle, and that's when you predict y to be 1."}, {"start": 499.56, "end": 506.96, "text": " Conversely, when x1 squared plus x2 squared is less than 1, that's this area inside the"}, {"start": 506.96, "end": 511.94, "text": " circle, and that's when you would predict y to be 0."}, {"start": 511.94, "end": 516.92, "text": " So can we come up with even more complex decision boundaries than these?"}, {"start": 516.92, "end": 518.28, "text": " Yes you can."}, {"start": 518.28, "end": 522.62, "text": " You can do so by having even higher order polynomial terms."}, {"start": 522.62, "end": 533.8, "text": " Say z is w1 x1 plus w2 x2 plus w3 x1 squared plus w4 x1 x2 plus w5 x2 squared."}, {"start": 533.8, "end": 538.24, "text": " Then it's possible you can get even more complex decision boundaries."}, {"start": 538.24, "end": 545.48, "text": " The model can define decision boundaries such as this example, an ellipse that's like this,"}, {"start": 545.48, "end": 549.48, "text": " or with a different choice of the parameters."}, {"start": 549.48, "end": 554.24, "text": " You can even get more complex decision boundaries which can look like functions that maybe look"}, {"start": 554.24, "end": 556.66, "text": " like that."}, {"start": 556.66, "end": 561.84, "text": " So this is an example of an even more complex decision boundary than the ones we've seen"}, {"start": 561.84, "end": 569.5600000000001, "text": " previously, and this implementation of logistic regression will predict y equals 1 inside"}, {"start": 569.5600000000001, "end": 574.84, "text": " this shape, and outside the shape it will predict y equals 0."}, {"start": 574.84, "end": 580.48, "text": " So with these polynomial features you can get very complex decision boundaries."}, {"start": 580.48, "end": 586.0, "text": " In other words logistic regression can learn to fit pretty complex data."}, {"start": 586.0, "end": 590.84, "text": " Although if you were to not include any of these higher order polynomials, so if the"}, {"start": 590.84, "end": 596.8000000000001, "text": " only features you use are x1 x2 x3 and so on, then the decision boundary for logistic"}, {"start": 596.8000000000001, "end": 601.7800000000001, "text": " regression will always be linear, will always be a straight line."}, {"start": 601.78, "end": 607.6, "text": " In the upcoming optional lab you also get to see the code implementation of the decision"}, {"start": 607.6, "end": 609.02, "text": " boundary."}, {"start": 609.02, "end": 614.04, "text": " In the example in the lab there will be two features so you can see the decision boundary"}, {"start": 614.04, "end": 615.74, "text": " as a line."}, {"start": 615.74, "end": 620.0, "text": " So with these visualizations I hope that you now have a sense of the range of possible"}, {"start": 620.0, "end": 623.9399999999999, "text": " models you can get with logistic regression."}, {"start": 623.9399999999999, "end": 629.28, "text": " Now that you've seen what f of x can potentially compute, let's take a look at how you can"}, {"start": 629.28, "end": 632.72, "text": " actually train a logistic regression model."}, {"start": 632.72, "end": 637.52, "text": " We'll start by looking at the cost function for logistic regression and after that figure"}, {"start": 637.52, "end": 640.24, "text": " out how to apply gradient descent to it."}, {"start": 640.24, "end": 660.28, "text": " Let's go on to the next video."}]