6.17 Skewed datasets (optional) | Trading off precision and recall -[Machine Learning | Andrew Ng]
 In the IDU case, we like for learning algorithms to have high precision and high recall. High precision would mean that if it diagnoses a patient with that rare disease, probably the patient does have it and is in accurate diagnosis. And high recall means that if there's a patient with that rare disease, probably the algorithm will correctly identify that they do have that disease. But it turns out that in practice, there's often a trade-off between precision and recall. In this video, we'll take a look at that trade-off and how you can pick a good point along that trade-off. So here are the definitions from the last video on precision and recall. I'll just write them here. What you recall precision is the number of true positives divided by the total number that was predicted positive. And recall is the number of true positives divided by the total actual number of positives. If you're using logistic regression to make predictions, then the logistic regression model will output numbers between 0 and 1. We would typically threshold the outputs of logistic regression at 0.5 and predict 1 if f of x is greater than or equal to 0.5 and predict 0 if it's less than 0.5. But suppose we want to predict that y is equal to 1, that is, the rare disease is present, only if we're very confident. So if our philosophy is whenever we predict that a patient has a disease, we may have to send them for possibly invasive and expensive treatment. So if the consequences of the disease aren't that bad, even if left not treated aggressively, then we may want to predict y equals 1 only if we're very confident. In that case, we may choose to set a higher threshold where we will predict y is 1 only if f of x is greater than or equal to 0.7. So this is saying we'll predict y equals 1 only if we're at least 70% sure rather than just 50% sure. And so this number also becomes 0.7. Notice that these two numbers have to be the same because it's just depending on whether it's greater than or equal to or less than this number that you predict 1 or 0. And by raising this threshold, you predict y equals 1 only if you're pretty confident. And what that means is that precision will increase because whenever you predict 1, you're more likely to be right. So raising the thresholds will result in higher precision, but it also results in lower recall because we're now predicting 1 less often. And so of the total number of patients with the disease, we're going to correctly diagnose fewer of them. So by raising this threshold to 0.7, you end up with higher precision, but lower recall. And in fact, if you want to predict y equals 1 only if you're very, very confident, you can even raise this higher to 0.9. And that results in an even higher precision. And so whenever you predict a patient has a disease, you're probably right. And this will give you a very high precision, but recall will go even further down. On the flip side, suppose we want to avoid missing too many cases of the rare disease. So if what we want is when in doubt, predict y equals 1. This might be the case where if treatment is not too invasive or painful or expensive, but leaving a disease untreated has much worse consequences for the patient. So in that case, you might say, when in doubt, in the interest of safety, let's just predict that they have it and consider them for treatment because untreated cases could be quite bad. If for your application, that is the better way to make decisions, then you would take this threshold and instead lower it, say set it to 0.3. In that case, you predict 1 so long as you think that's maybe a 30% chance or better of the disease being present. And you predict 0 only if you're pretty sure that the disease is absent. And as you can imagine, the impact on precision and recall will be opposite to what you saw appear and lowering this threshold will result in lower precision because we're now looser. We're more willing to predict 1 even if we aren't sure, but to result in higher recall. Because of all the patients that do have that disease, we're probably going to correctly identify more of them. More generally, we have the flexibility to predict 1 only if f is above some threshold. And by choosing this threshold, we can make different trade-offs between precision and recall. And it turns out that for most learning algorithms, there is a trade-off between precision and recall. Precision and recall both go between 0 and 1. And if you were to set a very high threshold, say a threshold of 0.99, then you end up with very high precision but lower recall. And as you reduce the value of this threshold, you then end up with a curve that trades off precision and recall until eventually, if you have a very low threshold, say the threshold equals 0.01, then you end up with very low precision but relatively high recall. And sometimes by plotting this curve, you can then try to pick a threshold which corresponds to picking a point on this curve that balances the cost of false positives and false negatives, or that balances the benefits of high precision and high recall. So plotting precision and recall for different values of the threshold allows you to pick a point that you want. Notice that picking the threshold is not something you can really do with cross-validation because it's up to you to specify the best points. For many applications, manually picking the threshold to trade off precision and recall will be what you end up doing. It turns out that if you want to automatically trade off precision and recall rather than have to do so yourself, there is another metric called the F1 score that is sometimes used to automatically combine precision and recall to help you pick the best value or the best trade off between the two. One challenge with precision and recall is you're now evaluating your algorithms using two different metrics. So if you've trained three different algorithms and the precision and recall numbers look like this, it's not that obvious how to pick which algorithm to use. If there was an algorithm that's better on precision and better on recall, then you probably want to go with that one. But in this example, algorithm 2 has the highest precision, but algorithm 3 has the highest recall and algorithm 1 kind of trades off the two in between. So no one algorithm is obviously the best choice. So in order to help you decide which algorithm to pick, it may be useful to find a way to combine precision and recall into a single score. So you can just look at which algorithm has the highest score and maybe go with that one. One way you could combine precision and recall is to take the average. This turns out not to be a good way, so I don't really recommend this, but if you were to take the average, you'd get 0.45, 0.4, and 0.5. But it turns out that computing the average and picking the algorithm with the highest average between precision and recall doesn't work that well because this algorithm has very, very low precision. And in fact, this corresponds maybe to an algorithm that actually does print y equals one and diagnoses all patients as having the disease. That's why its recall is perfect, but the precision is really low. So algorithm 3 is actually not a particularly useful algorithm, even though the average between precision and recall is quite high. So let's not use the average between precision and recall. Instead, the most common way of combining precision and recall is to compute something called the F1 score. And the F1 score is a way of combining P and R, precision and recall, but that gives more emphasis to whichever of these values is lower. Because it turns out if an algorithm has very low precision or very low recall, it's probably not that useful. So the F1 score is a way of computing an average of sorts that pays more attention to whichever is lower. And the formula for computing F1 score is this. We're going to compute one over P and one over R and average them and then take the inverse of that. So rather than averaging P and R, precision and recall, we're going to average one over P and one over R and then take one over that. And if you simplify this equation, it can also be computed as follows. But by averaging one over P and one over R, this gives a much greater emphasis to if either P or R turns out to be very small. If you were to compute the F1 score for these three algorithms, you find that the F1 score for algorithm one is 0.444 and for the second algorithm is 0.175. And you notice that 0.175 is much closer to the lower value than the higher value. And for the third algorithm is 0.0392. And so F1 score gives a way to trade off precision and recall. And in this case, it'll tell us that maybe the first algorithm is better than the second or the third algorithms. And by the way, in math, this equation is also called the harmonic mean of P and R. And the harmonic mean is a way of taking an average that emphasizes the smaller values more. But for the purposes of the class, you don't need to worry about that terminology of the harmonic mean. So congratulations on getting to the last video of this week. And thank you also for sticking with me through these two optional videos. In this week, you've learned a lot of practical tips, practical advice for how to build a machine learning system. And by applying these ideas, I think you'd be very effective at building machine learning algorithms. Next week, we'll come back to talk about another very powerful machine learning algorithm. In fact, of the advanced techniques that are widely used in many commercial production settings, I think at the top of the list would be neural networks and decision trees. So next week, we'll talk about decision trees, which I think will be another very powerful technique that you could have used to build many successful applications as well. So I look forward to seeing you next week.
[{"start": 0.0, "end": 6.92, "text": " In the IDU case, we like for learning algorithms to have high precision and high recall."}, {"start": 6.92, "end": 11.96, "text": " High precision would mean that if it diagnoses a patient with that rare disease, probably"}, {"start": 11.96, "end": 15.36, "text": " the patient does have it and is in accurate diagnosis."}, {"start": 15.36, "end": 20.16, "text": " And high recall means that if there's a patient with that rare disease, probably the algorithm"}, {"start": 20.16, "end": 23.92, "text": " will correctly identify that they do have that disease."}, {"start": 23.92, "end": 29.88, "text": " But it turns out that in practice, there's often a trade-off between precision and recall."}, {"start": 29.88, "end": 34.68, "text": " In this video, we'll take a look at that trade-off and how you can pick a good point along that"}, {"start": 34.68, "end": 36.12, "text": " trade-off."}, {"start": 36.12, "end": 40.36, "text": " So here are the definitions from the last video on precision and recall."}, {"start": 40.36, "end": 42.28, "text": " I'll just write them here."}, {"start": 42.28, "end": 47.0, "text": " What you recall precision is the number of true positives divided by the total number"}, {"start": 47.0, "end": 49.08, "text": " that was predicted positive."}, {"start": 49.08, "end": 56.56, "text": " And recall is the number of true positives divided by the total actual number of positives."}, {"start": 56.56, "end": 63.64, "text": " If you're using logistic regression to make predictions, then the logistic regression"}, {"start": 63.64, "end": 67.76, "text": " model will output numbers between 0 and 1."}, {"start": 67.76, "end": 75.52000000000001, "text": " We would typically threshold the outputs of logistic regression at 0.5 and predict 1 if"}, {"start": 75.52000000000001, "end": 82.48, "text": " f of x is greater than or equal to 0.5 and predict 0 if it's less than 0.5."}, {"start": 82.48, "end": 88.88000000000001, "text": " But suppose we want to predict that y is equal to 1, that is, the rare disease is present,"}, {"start": 88.88000000000001, "end": 90.48, "text": " only if we're very confident."}, {"start": 90.48, "end": 95.28, "text": " So if our philosophy is whenever we predict that a patient has a disease, we may have"}, {"start": 95.28, "end": 101.10000000000001, "text": " to send them for possibly invasive and expensive treatment."}, {"start": 101.10000000000001, "end": 107.72, "text": " So if the consequences of the disease aren't that bad, even if left not treated aggressively,"}, {"start": 107.72, "end": 112.24000000000001, "text": " then we may want to predict y equals 1 only if we're very confident."}, {"start": 112.24, "end": 118.91999999999999, "text": " In that case, we may choose to set a higher threshold where we will predict y is 1 only"}, {"start": 118.91999999999999, "end": 122.83999999999999, "text": " if f of x is greater than or equal to 0.7."}, {"start": 122.83999999999999, "end": 128.51999999999998, "text": " So this is saying we'll predict y equals 1 only if we're at least 70% sure rather than"}, {"start": 128.51999999999998, "end": 130.72, "text": " just 50% sure."}, {"start": 130.72, "end": 134.48, "text": " And so this number also becomes 0.7."}, {"start": 134.48, "end": 138.44, "text": " Notice that these two numbers have to be the same because it's just depending on whether"}, {"start": 138.44, "end": 144.88, "text": " it's greater than or equal to or less than this number that you predict 1 or 0."}, {"start": 144.88, "end": 151.24, "text": " And by raising this threshold, you predict y equals 1 only if you're pretty confident."}, {"start": 151.24, "end": 157.92, "text": " And what that means is that precision will increase because whenever you predict 1, you're"}, {"start": 157.92, "end": 160.4, "text": " more likely to be right."}, {"start": 160.4, "end": 168.4, "text": " So raising the thresholds will result in higher precision, but it also results in lower recall"}, {"start": 168.4, "end": 172.56, "text": " because we're now predicting 1 less often."}, {"start": 172.56, "end": 179.48000000000002, "text": " And so of the total number of patients with the disease, we're going to correctly diagnose"}, {"start": 179.48000000000002, "end": 181.24, "text": " fewer of them."}, {"start": 181.24, "end": 189.92000000000002, "text": " So by raising this threshold to 0.7, you end up with higher precision, but lower recall."}, {"start": 189.92000000000002, "end": 194.84, "text": " And in fact, if you want to predict y equals 1 only if you're very, very confident, you"}, {"start": 194.84, "end": 198.84, "text": " can even raise this higher to 0.9."}, {"start": 198.84, "end": 201.92000000000002, "text": " And that results in an even higher precision."}, {"start": 201.92000000000002, "end": 205.56, "text": " And so whenever you predict a patient has a disease, you're probably right."}, {"start": 205.56, "end": 210.96, "text": " And this will give you a very high precision, but recall will go even further down."}, {"start": 210.96, "end": 217.72, "text": " On the flip side, suppose we want to avoid missing too many cases of the rare disease."}, {"start": 217.72, "end": 223.96, "text": " So if what we want is when in doubt, predict y equals 1."}, {"start": 223.96, "end": 231.32000000000002, "text": " This might be the case where if treatment is not too invasive or painful or expensive,"}, {"start": 231.32000000000002, "end": 235.72, "text": " but leaving a disease untreated has much worse consequences for the patient."}, {"start": 235.72, "end": 240.68, "text": " So in that case, you might say, when in doubt, in the interest of safety, let's just predict"}, {"start": 240.68, "end": 246.76000000000002, "text": " that they have it and consider them for treatment because untreated cases could be quite bad."}, {"start": 246.76000000000002, "end": 252.08, "text": " If for your application, that is the better way to make decisions, then you would take"}, {"start": 252.08, "end": 257.68, "text": " this threshold and instead lower it, say set it to 0.3."}, {"start": 257.68, "end": 263.28000000000003, "text": " In that case, you predict 1 so long as you think that's maybe a 30% chance or better"}, {"start": 263.28000000000003, "end": 265.72, "text": " of the disease being present."}, {"start": 265.72, "end": 270.68, "text": " And you predict 0 only if you're pretty sure that the disease is absent."}, {"start": 270.68, "end": 277.46000000000004, "text": " And as you can imagine, the impact on precision and recall will be opposite to what you saw"}, {"start": 277.46, "end": 287.4, "text": " appear and lowering this threshold will result in lower precision because we're now looser."}, {"start": 287.4, "end": 295.41999999999996, "text": " We're more willing to predict 1 even if we aren't sure, but to result in higher recall."}, {"start": 295.41999999999996, "end": 299.52, "text": " Because of all the patients that do have that disease, we're probably going to correctly"}, {"start": 299.52, "end": 301.82, "text": " identify more of them."}, {"start": 301.82, "end": 309.2, "text": " More generally, we have the flexibility to predict 1 only if f is above some threshold."}, {"start": 309.2, "end": 313.84, "text": " And by choosing this threshold, we can make different trade-offs between precision and"}, {"start": 313.84, "end": 314.84, "text": " recall."}, {"start": 314.84, "end": 321.08, "text": " And it turns out that for most learning algorithms, there is a trade-off between precision and"}, {"start": 321.08, "end": 322.08, "text": " recall."}, {"start": 322.08, "end": 326.4, "text": " Precision and recall both go between 0 and 1."}, {"start": 326.4, "end": 333.79999999999995, "text": " And if you were to set a very high threshold, say a threshold of 0.99, then you end up with"}, {"start": 333.79999999999995, "end": 337.32, "text": " very high precision but lower recall."}, {"start": 337.32, "end": 344.23999999999995, "text": " And as you reduce the value of this threshold, you then end up with a curve that trades off"}, {"start": 344.23999999999995, "end": 349.96, "text": " precision and recall until eventually, if you have a very low threshold, say the threshold"}, {"start": 349.96, "end": 357.0, "text": " equals 0.01, then you end up with very low precision but relatively high recall."}, {"start": 357.0, "end": 362.12, "text": " And sometimes by plotting this curve, you can then try to pick a threshold which corresponds"}, {"start": 362.12, "end": 368.32, "text": " to picking a point on this curve that balances the cost of false positives and false negatives,"}, {"start": 368.32, "end": 372.67999999999995, "text": " or that balances the benefits of high precision and high recall."}, {"start": 372.67999999999995, "end": 378.88, "text": " So plotting precision and recall for different values of the threshold allows you to pick"}, {"start": 378.88, "end": 381.96, "text": " a point that you want."}, {"start": 381.96, "end": 391.12, "text": " Notice that picking the threshold is not something you can really do with cross-validation because"}, {"start": 391.12, "end": 394.44, "text": " it's up to you to specify the best points."}, {"start": 394.44, "end": 399.52, "text": " For many applications, manually picking the threshold to trade off precision and recall"}, {"start": 399.52, "end": 402.64, "text": " will be what you end up doing."}, {"start": 402.64, "end": 407.6, "text": " It turns out that if you want to automatically trade off precision and recall rather than"}, {"start": 407.6, "end": 414.36, "text": " have to do so yourself, there is another metric called the F1 score that is sometimes used"}, {"start": 414.36, "end": 420.20000000000005, "text": " to automatically combine precision and recall to help you pick the best value or the best"}, {"start": 420.20000000000005, "end": 421.92, "text": " trade off between the two."}, {"start": 421.92, "end": 427.12, "text": " One challenge with precision and recall is you're now evaluating your algorithms using"}, {"start": 427.12, "end": 429.04, "text": " two different metrics."}, {"start": 429.04, "end": 433.88, "text": " So if you've trained three different algorithms and the precision and recall numbers look"}, {"start": 433.88, "end": 439.68, "text": " like this, it's not that obvious how to pick which algorithm to use."}, {"start": 439.68, "end": 444.0, "text": " If there was an algorithm that's better on precision and better on recall, then you probably"}, {"start": 444.0, "end": 445.64, "text": " want to go with that one."}, {"start": 445.64, "end": 452.2, "text": " But in this example, algorithm 2 has the highest precision, but algorithm 3 has the highest"}, {"start": 452.2, "end": 456.36, "text": " recall and algorithm 1 kind of trades off the two in between."}, {"start": 456.36, "end": 461.08, "text": " So no one algorithm is obviously the best choice."}, {"start": 461.08, "end": 467.84, "text": " So in order to help you decide which algorithm to pick, it may be useful to find a way to"}, {"start": 467.84, "end": 471.88, "text": " combine precision and recall into a single score."}, {"start": 471.88, "end": 479.0, "text": " So you can just look at which algorithm has the highest score and maybe go with that one."}, {"start": 479.0, "end": 483.52, "text": " One way you could combine precision and recall is to take the average."}, {"start": 483.52, "end": 487.28, "text": " This turns out not to be a good way, so I don't really recommend this, but if you were"}, {"start": 487.28, "end": 493.03999999999996, "text": " to take the average, you'd get 0.45, 0.4, and 0.5."}, {"start": 493.03999999999996, "end": 496.94, "text": " But it turns out that computing the average and picking the algorithm with the highest"}, {"start": 496.94, "end": 501.78, "text": " average between precision and recall doesn't work that well because this algorithm has"}, {"start": 501.78, "end": 503.96, "text": " very, very low precision."}, {"start": 503.96, "end": 510.46, "text": " And in fact, this corresponds maybe to an algorithm that actually does print y equals"}, {"start": 510.46, "end": 514.86, "text": " one and diagnoses all patients as having the disease."}, {"start": 514.86, "end": 518.5600000000001, "text": " That's why its recall is perfect, but the precision is really low."}, {"start": 518.5600000000001, "end": 524.08, "text": " So algorithm 3 is actually not a particularly useful algorithm, even though the average"}, {"start": 524.08, "end": 528.32, "text": " between precision and recall is quite high."}, {"start": 528.32, "end": 533.2, "text": " So let's not use the average between precision and recall."}, {"start": 533.2, "end": 538.44, "text": " Instead, the most common way of combining precision and recall is to compute something"}, {"start": 538.44, "end": 541.08, "text": " called the F1 score."}, {"start": 541.08, "end": 546.36, "text": " And the F1 score is a way of combining P and R, precision and recall, but that gives more"}, {"start": 546.36, "end": 549.9200000000001, "text": " emphasis to whichever of these values is lower."}, {"start": 549.9200000000001, "end": 554.2800000000001, "text": " Because it turns out if an algorithm has very low precision or very low recall, it's probably"}, {"start": 554.2800000000001, "end": 556.1, "text": " not that useful."}, {"start": 556.1, "end": 562.74, "text": " So the F1 score is a way of computing an average of sorts that pays more attention to whichever"}, {"start": 562.74, "end": 565.6800000000001, "text": " is lower."}, {"start": 565.6800000000001, "end": 570.48, "text": " And the formula for computing F1 score is this."}, {"start": 570.48, "end": 579.44, "text": " We're going to compute one over P and one over R and average them and then take the"}, {"start": 579.44, "end": 583.12, "text": " inverse of that."}, {"start": 583.12, "end": 588.2, "text": " So rather than averaging P and R, precision and recall, we're going to average one over"}, {"start": 588.2, "end": 592.1800000000001, "text": " P and one over R and then take one over that."}, {"start": 592.1800000000001, "end": 596.64, "text": " And if you simplify this equation, it can also be computed as follows."}, {"start": 596.64, "end": 601.84, "text": " But by averaging one over P and one over R, this gives a much greater emphasis to if either"}, {"start": 601.84, "end": 605.52, "text": " P or R turns out to be very small."}, {"start": 605.52, "end": 610.8, "text": " If you were to compute the F1 score for these three algorithms, you find that the F1 score"}, {"start": 610.8, "end": 617.52, "text": " for algorithm one is 0.444 and for the second algorithm is 0.175."}, {"start": 617.52, "end": 623.4399999999999, "text": " And you notice that 0.175 is much closer to the lower value than the higher value."}, {"start": 623.44, "end": 628.5600000000001, "text": " And for the third algorithm is 0.0392."}, {"start": 628.5600000000001, "end": 633.7600000000001, "text": " And so F1 score gives a way to trade off precision and recall."}, {"start": 633.7600000000001, "end": 638.7600000000001, "text": " And in this case, it'll tell us that maybe the first algorithm is better than the second"}, {"start": 638.7600000000001, "end": 640.7600000000001, "text": " or the third algorithms."}, {"start": 640.7600000000001, "end": 648.12, "text": " And by the way, in math, this equation is also called the harmonic mean of P and R."}, {"start": 648.12, "end": 653.6, "text": " And the harmonic mean is a way of taking an average that emphasizes the smaller values"}, {"start": 653.6, "end": 654.6, "text": " more."}, {"start": 654.6, "end": 657.68, "text": " But for the purposes of the class, you don't need to worry about that terminology of the"}, {"start": 657.68, "end": 659.0600000000001, "text": " harmonic mean."}, {"start": 659.0600000000001, "end": 662.2, "text": " So congratulations on getting to the last video of this week."}, {"start": 662.2, "end": 666.88, "text": " And thank you also for sticking with me through these two optional videos."}, {"start": 666.88, "end": 671.64, "text": " In this week, you've learned a lot of practical tips, practical advice for how to build a"}, {"start": 671.64, "end": 673.6, "text": " machine learning system."}, {"start": 673.6, "end": 678.94, "text": " And by applying these ideas, I think you'd be very effective at building machine learning"}, {"start": 678.94, "end": 681.5600000000001, "text": " algorithms."}, {"start": 681.5600000000001, "end": 686.48, "text": " Next week, we'll come back to talk about another very powerful machine learning algorithm."}, {"start": 686.48, "end": 691.52, "text": " In fact, of the advanced techniques that are widely used in many commercial production"}, {"start": 691.52, "end": 697.08, "text": " settings, I think at the top of the list would be neural networks and decision trees."}, {"start": 697.08, "end": 702.08, "text": " So next week, we'll talk about decision trees, which I think will be another very powerful"}, {"start": 702.08, "end": 707.0, "text": " technique that you could have used to build many successful applications as well."}, {"start": 707.0, "end": 733.08, "text": " So I look forward to seeing you next week."}]