1.11 Machine Learning Overview | Cost function formula --[Machine Learning | Andrew Ng]
 In order to implement linear regression, the first key step is for us to define something called a cost function. This is something we'll build in this video. And the cost function will tell us how well the model is doing so that we can try to get it to do better. Let's look at what this means. Recall that you have a training set that contains input features x and output targets y. And the model you're going to use to fit this training set is this linear function fwb of x equals w times x plus b. To introduce a little bit more terminology, the w and b are called the parameters of the model. In machine learning, parameters of a model are the variables you can adjust during training in order to improve the model. Sometimes, you also hear the parameters w and b referred to as coefficients or as weights. Now, let's take a look at what these parameters w and b do. Depending on the values you've chosen for w and b, you get a different function f of x, which generates a different line on the graph. And remember that we can write f of x as a shorthand for fwb of x. We're going to take a look at some plots of f of x on a chart. Maybe you're already familiar with drawing lines on charts, but even if this is a review for you, I hope this will help you build intuition on how w and b, the parameters, determine f. When w is equal to 0 and b is equal to 1.5, then f looks like this horizontal line. In this case, the function f of x is 0 times x plus 1.5, so f is always a constant value. It always predicts 1.5 for the estimated value of y. So y hat is always equal to b. And here, b is also called the y-intercept, because that's where it crosses the vertical axis or the y-axis on this graph. As a second example, if w is 0.5 and b is equal to 0, then f of x is 0.5 times x. When x is 0, the prediction is also 0. And when x is 2, then the prediction is 0.5 times 2, which is 1. So you get a line that looks like this. And notice that the slope is 0.5 divided by 1, so the value of w gives you the slope of the line, which is 0.5. And finally, if w equals 0.5 and b equals 1, then f of x is 0.5 times x plus 1. And when x is 0, then f of x equals b, which is 1, so the line intersects the vertical axis at b, the y-intercept. Also when x is 2, then f of x is 2, so the line looks like this. Again, the slope is 0.5 divided by 1, so the value of w gives you the slope, which is 0.5. Recall that you have a training set, like the one shown here, with linear regression, what you want to do is to choose values for the parameters w and b so that the straight line you get from the function f somehow fits the data well, like maybe this line shown here. And when I say that the line fits the data, visually you can think of this to mean that the line defined by f is roughly passing through or somewhat close to the training examples as compared to other possible lines that are not as close to these points. And just to remind you of some notation, a training example like this point here is defined by x super strip i, y super strip i, where y is the target. For a given input x i, the function f also makes a predicted value for y, and the value that it predicts for y is y hat i, shown here. For our choice of a model, f of x i is w times x i plus b. Stated differently, the prediction y hat i is f of w b of x i, where for the model where using f of x i is equal to w x i plus b. So now the question is, how do you find values for w and b so that the prediction y hat i is close to the true target y i for many or maybe all training examples x i y i? To answer that question, let's first take a look at how to measure how well a line fits the training data. To do that, we're going to construct our cost function. The cost function takes the prediction y hat and compares it to the target y by taking y hat minus y. This difference is called the error. We're measuring how far off the prediction is from the target. Next let's compute the square of this error. Also we're going to want to compute this term for different training examples i in the training set. So when measuring the error for example i, we'll compute this squared error term. Finally, we want to measure the error across the entire training set. In particular, let's sum up the squared errors like this. We'll sum from i equals 1, 2, 3, all the way up to m. And remember that m is the number of training examples, which is 47 for this data set. Notice that if we have more training examples, m is larger and your cost function will calculate a bigger number since it's summing over more examples. So to build a cost function that doesn't automatically get bigger as the training set size gets larger, by convention, we will compute the average squared error instead of the total squared error. And we do that by dividing by m like this. Okay, we're nearly there. Just one last thing. By convention, the cost function that machine learning people use actually divides by two times m. The extra division by two is just meant to make some of our later calculations a little bit neater. But the cost function still works whether you include this division by two or not. So this expression right here is the cost function. And we're going to write J of WB to refer to the cost function. This is also called the squared error cost function. And it's called this because you're taking the square of these error terms. In machine learning, different people will use different cost functions for different applications. But the squared error cost function is by far the most commonly used one for linear regression. And for that matter, for all regression problems, where it seems to give good results for many applications. So just as a reminder, the prediction y hat is equal to the output of the model f at x. So we can rewrite the cost function J of WB as one over two m times the sum from i equals one to m of f of x i minus y i, the quantity squared. Eventually, we're going to want to find values of W and B that make the cost function small. But before going there, let's first gain more intuition about what J of WB is really computing. At this point, you might be thinking we've done a whole lot of math to define the cost function. But what exactly is it doing? Let's go on to the next video, where we'll step through one example of what the cost function is really computing. That I hope will help you build intuition about what it means if J of WB is large versus if the cost J is small. Let's go on to the next video.
[{"start": 0.0, "end": 6.32, "text": " In order to implement linear regression, the first key step is for us to define something"}, {"start": 6.32, "end": 8.32, "text": " called a cost function."}, {"start": 8.32, "end": 10.8, "text": " This is something we'll build in this video."}, {"start": 10.8, "end": 16.04, "text": " And the cost function will tell us how well the model is doing so that we can try to get"}, {"start": 16.04, "end": 17.68, "text": " it to do better."}, {"start": 17.68, "end": 19.68, "text": " Let's look at what this means."}, {"start": 19.68, "end": 26.6, "text": " Recall that you have a training set that contains input features x and output targets y."}, {"start": 26.6, "end": 33.68, "text": " And the model you're going to use to fit this training set is this linear function fwb of"}, {"start": 33.68, "end": 37.32, "text": " x equals w times x plus b."}, {"start": 37.32, "end": 43.480000000000004, "text": " To introduce a little bit more terminology, the w and b are called the parameters of the"}, {"start": 43.480000000000004, "end": 44.88, "text": " model."}, {"start": 44.88, "end": 51.08, "text": " In machine learning, parameters of a model are the variables you can adjust during training"}, {"start": 51.08, "end": 53.84, "text": " in order to improve the model."}, {"start": 53.84, "end": 61.64, "text": " Sometimes, you also hear the parameters w and b referred to as coefficients or as weights."}, {"start": 61.64, "end": 68.28, "text": " Now, let's take a look at what these parameters w and b do."}, {"start": 68.28, "end": 73.56, "text": " Depending on the values you've chosen for w and b, you get a different function f of"}, {"start": 73.56, "end": 77.56, "text": " x, which generates a different line on the graph."}, {"start": 77.56, "end": 84.84, "text": " And remember that we can write f of x as a shorthand for fwb of x."}, {"start": 84.84, "end": 90.0, "text": " We're going to take a look at some plots of f of x on a chart."}, {"start": 90.0, "end": 94.18, "text": " Maybe you're already familiar with drawing lines on charts, but even if this is a review"}, {"start": 94.18, "end": 100.4, "text": " for you, I hope this will help you build intuition on how w and b, the parameters, determine"}, {"start": 100.4, "end": 102.72, "text": " f."}, {"start": 102.72, "end": 111.0, "text": " When w is equal to 0 and b is equal to 1.5, then f looks like this horizontal line."}, {"start": 111.0, "end": 120.6, "text": " In this case, the function f of x is 0 times x plus 1.5, so f is always a constant value."}, {"start": 120.6, "end": 125.75999999999999, "text": " It always predicts 1.5 for the estimated value of y."}, {"start": 125.75999999999999, "end": 129.12, "text": " So y hat is always equal to b."}, {"start": 129.12, "end": 134.72, "text": " And here, b is also called the y-intercept, because that's where it crosses the vertical"}, {"start": 134.72, "end": 138.72, "text": " axis or the y-axis on this graph."}, {"start": 138.72, "end": 148.96, "text": " As a second example, if w is 0.5 and b is equal to 0, then f of x is 0.5 times x."}, {"start": 148.96, "end": 152.56, "text": " When x is 0, the prediction is also 0."}, {"start": 152.56, "end": 158.52, "text": " And when x is 2, then the prediction is 0.5 times 2, which is 1."}, {"start": 158.52, "end": 160.88000000000002, "text": " So you get a line that looks like this."}, {"start": 160.88000000000002, "end": 169.56, "text": " And notice that the slope is 0.5 divided by 1, so the value of w gives you the slope of"}, {"start": 169.56, "end": 173.44, "text": " the line, which is 0.5."}, {"start": 173.44, "end": 184.68, "text": " And finally, if w equals 0.5 and b equals 1, then f of x is 0.5 times x plus 1."}, {"start": 184.68, "end": 191.72, "text": " And when x is 0, then f of x equals b, which is 1, so the line intersects the vertical"}, {"start": 191.72, "end": 195.16, "text": " axis at b, the y-intercept."}, {"start": 195.16, "end": 201.04000000000002, "text": " Also when x is 2, then f of x is 2, so the line looks like this."}, {"start": 201.04000000000002, "end": 209.4, "text": " Again, the slope is 0.5 divided by 1, so the value of w gives you the slope, which is 0.5."}, {"start": 209.4, "end": 215.36, "text": " Recall that you have a training set, like the one shown here, with linear regression,"}, {"start": 215.36, "end": 220.32, "text": " what you want to do is to choose values for the parameters w and b so that the straight"}, {"start": 220.32, "end": 225.88, "text": " line you get from the function f somehow fits the data well, like maybe this line shown"}, {"start": 225.88, "end": 227.28, "text": " here."}, {"start": 227.28, "end": 232.76, "text": " And when I say that the line fits the data, visually you can think of this to mean that"}, {"start": 232.76, "end": 239.88, "text": " the line defined by f is roughly passing through or somewhat close to the training examples"}, {"start": 239.88, "end": 245.79999999999998, "text": " as compared to other possible lines that are not as close to these points."}, {"start": 245.79999999999998, "end": 252.84, "text": " And just to remind you of some notation, a training example like this point here is defined"}, {"start": 252.84, "end": 260.8, "text": " by x super strip i, y super strip i, where y is the target."}, {"start": 260.8, "end": 270.44, "text": " For a given input x i, the function f also makes a predicted value for y, and the value"}, {"start": 270.44, "end": 275.2, "text": " that it predicts for y is y hat i, shown here."}, {"start": 275.2, "end": 281.48, "text": " For our choice of a model, f of x i is w times x i plus b."}, {"start": 281.48, "end": 290.72, "text": " Stated differently, the prediction y hat i is f of w b of x i, where for the model where"}, {"start": 290.72, "end": 299.76000000000005, "text": " using f of x i is equal to w x i plus b."}, {"start": 299.76000000000005, "end": 307.76000000000005, "text": " So now the question is, how do you find values for w and b so that the prediction y hat i"}, {"start": 307.76000000000005, "end": 317.54, "text": " is close to the true target y i for many or maybe all training examples x i y i?"}, {"start": 317.54, "end": 323.08000000000004, "text": " To answer that question, let's first take a look at how to measure how well a line fits"}, {"start": 323.08000000000004, "end": 325.04, "text": " the training data."}, {"start": 325.04, "end": 329.08000000000004, "text": " To do that, we're going to construct our cost function."}, {"start": 329.08000000000004, "end": 336.8, "text": " The cost function takes the prediction y hat and compares it to the target y by taking"}, {"start": 336.8, "end": 339.90000000000003, "text": " y hat minus y."}, {"start": 339.90000000000003, "end": 342.96000000000004, "text": " This difference is called the error."}, {"start": 342.96, "end": 347.96, "text": " We're measuring how far off the prediction is from the target."}, {"start": 347.96, "end": 353.2, "text": " Next let's compute the square of this error."}, {"start": 353.2, "end": 358.24, "text": " Also we're going to want to compute this term for different training examples i in the training"}, {"start": 358.24, "end": 359.41999999999996, "text": " set."}, {"start": 359.41999999999996, "end": 365.59999999999997, "text": " So when measuring the error for example i, we'll compute this squared error term."}, {"start": 365.59999999999997, "end": 370.34, "text": " Finally, we want to measure the error across the entire training set."}, {"start": 370.34, "end": 374.11999999999995, "text": " In particular, let's sum up the squared errors like this."}, {"start": 374.11999999999995, "end": 380.28, "text": " We'll sum from i equals 1, 2, 3, all the way up to m."}, {"start": 380.28, "end": 386.28, "text": " And remember that m is the number of training examples, which is 47 for this data set."}, {"start": 386.28, "end": 391.88, "text": " Notice that if we have more training examples, m is larger and your cost function will calculate"}, {"start": 391.88, "end": 395.91999999999996, "text": " a bigger number since it's summing over more examples."}, {"start": 395.92, "end": 403.24, "text": " So to build a cost function that doesn't automatically get bigger as the training set size gets larger,"}, {"start": 403.24, "end": 409.52000000000004, "text": " by convention, we will compute the average squared error instead of the total squared"}, {"start": 409.52000000000004, "end": 410.52000000000004, "text": " error."}, {"start": 410.52000000000004, "end": 414.24, "text": " And we do that by dividing by m like this."}, {"start": 414.24, "end": 417.52000000000004, "text": " Okay, we're nearly there."}, {"start": 417.52000000000004, "end": 419.24, "text": " Just one last thing."}, {"start": 419.24, "end": 425.44, "text": " By convention, the cost function that machine learning people use actually divides by two"}, {"start": 425.44, "end": 426.64, "text": " times m."}, {"start": 426.64, "end": 431.71999999999997, "text": " The extra division by two is just meant to make some of our later calculations a little"}, {"start": 431.71999999999997, "end": 432.71999999999997, "text": " bit neater."}, {"start": 432.71999999999997, "end": 437.38, "text": " But the cost function still works whether you include this division by two or not."}, {"start": 437.38, "end": 440.68, "text": " So this expression right here is the cost function."}, {"start": 440.68, "end": 448.4, "text": " And we're going to write J of WB to refer to the cost function."}, {"start": 448.4, "end": 452.32, "text": " This is also called the squared error cost function."}, {"start": 452.32, "end": 457.44, "text": " And it's called this because you're taking the square of these error terms."}, {"start": 457.44, "end": 462.48, "text": " In machine learning, different people will use different cost functions for different"}, {"start": 462.48, "end": 463.48, "text": " applications."}, {"start": 463.48, "end": 468.71999999999997, "text": " But the squared error cost function is by far the most commonly used one for linear"}, {"start": 468.71999999999997, "end": 470.12, "text": " regression."}, {"start": 470.12, "end": 475.64, "text": " And for that matter, for all regression problems, where it seems to give good results for many"}, {"start": 475.64, "end": 476.8, "text": " applications."}, {"start": 476.8, "end": 485.72, "text": " So just as a reminder, the prediction y hat is equal to the output of the model f at x."}, {"start": 485.72, "end": 495.76, "text": " So we can rewrite the cost function J of WB as one over two m times the sum from i equals"}, {"start": 495.76, "end": 503.04, "text": " one to m of f of x i minus y i, the quantity squared."}, {"start": 503.04, "end": 510.36, "text": " Eventually, we're going to want to find values of W and B that make the cost function small."}, {"start": 510.36, "end": 518.64, "text": " But before going there, let's first gain more intuition about what J of WB is really computing."}, {"start": 518.64, "end": 523.28, "text": " At this point, you might be thinking we've done a whole lot of math to define the cost"}, {"start": 523.28, "end": 524.4, "text": " function."}, {"start": 524.4, "end": 526.88, "text": " But what exactly is it doing?"}, {"start": 526.88, "end": 531.8000000000001, "text": " Let's go on to the next video, where we'll step through one example of what the cost"}, {"start": 531.8, "end": 533.4, "text": " function is really computing."}, {"start": 533.4, "end": 539.4399999999999, "text": " That I hope will help you build intuition about what it means if J of WB is large versus"}, {"start": 539.4399999999999, "end": 542.04, "text": " if the cost J is small."}, {"start": 542.04, "end": 562.4399999999999, "text": " Let's go on to the next video."}]