forked from fengdu78/Coursera-ML-AndrewNg-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11 - 4 - Trading Off Precision and Recall (14 min).srt
2091 lines (1673 loc) · 36.9 KB
/
11 - 4 - Trading Off Precision and Recall (14 min).srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,410 --> 00:00:01,520
In the last video, we talked
在之前的课程中 我们谈到
(字幕整理:中国海洋大学 黄海广,haiguang2000@qq.com )
2
00:00:01,820 --> 00:00:04,130
about precision and recall as
查准率和召回率
3
00:00:04,280 --> 00:00:06,180
an evaluation metric for classification
作为遇到偏斜类问题
4
00:00:06,840 --> 00:00:08,220
problems with skew classes.
的评估度量值
5
00:00:09,530 --> 00:00:11,020
For many applications, we'll want
在很多应用中
6
00:00:11,180 --> 00:00:13,350
to somehow control the trade
我们希望能够保证
7
00:00:13,630 --> 00:00:15,640
off between position and recall.
查准率和召回率的相对平衡
8
00:00:16,500 --> 00:00:17,310
Let me tell you how
在这节课中
9
00:00:17,470 --> 00:00:19,020
to do that and also show
我将告诉你应该怎么做
10
00:00:19,390 --> 00:00:20,520
you some, even more effective
同时也向你展示一些
11
00:00:21,050 --> 00:00:22,810
ways to use precision and
查准率和召回率
12
00:00:22,980 --> 00:00:24,290
recall as an evaluation
作为算法评估度量值的
13
00:00:24,720 --> 00:00:27,380
metric for learning algorithms.
更有效的方式
14
00:00:28,620 --> 00:00:30,180
As a reminder, here are the
回忆一下
15
00:00:30,250 --> 00:00:32,150
definitions of precision and
这是查准率和召回率的定义
16
00:00:32,380 --> 00:00:34,100
recall from the previous video.
我们在上一节中讲到的
17
00:00:35,920 --> 00:00:37,650
Let's continue our cancer classification
让我们继续用
18
00:00:38,680 --> 00:00:39,980
example, where y equals
癌症分类的例子
19
00:00:40,370 --> 00:00:41,790
one if the patient has cancer
如果病人患癌症 则y=1
20
00:00:42,270 --> 00:00:43,310
and y equals zero otherwise.
反之则y=0
21
00:00:44,800 --> 00:00:46,060
And let's say we've trained in
假设我们用
22
00:00:46,360 --> 00:00:48,580
logistic regression classifier, which outputs
逻辑回归模型训练了数据
23
00:00:49,070 --> 00:00:50,690
probabilities between zero and one.
输出概率在0-1之间的值
24
00:00:51,740 --> 00:00:52,830
So, as usual, we're
因此
25
00:00:53,010 --> 00:00:54,690
going to predict one, y equals
我们预测y=1
26
00:00:55,080 --> 00:00:56,290
one if h of x
如果h(x)
27
00:00:56,560 --> 00:00:57,980
is greater than or equal to
大于或等于0.5
28
00:00:58,090 --> 00:00:59,720
0.5 and predict zero if
预测值为0
29
00:01:00,140 --> 00:01:01,570
the hypothesis outputs a value
如果方程输出值
30
00:01:01,820 --> 00:01:03,720
less than 0.5 and this
小于0.5
31
00:01:04,040 --> 00:01:05,400
classifier may give us
这个回归模型
32
00:01:05,710 --> 00:01:08,430
some value for precision and some value for recall.
能够计算查准率和召回率
33
00:01:10,420 --> 00:01:11,860
But now, suppose we want
但是现在
34
00:01:12,140 --> 00:01:13,440
to predict that a patient
假如我们希望
35
00:01:13,730 --> 00:01:15,510
has cancer only if we're
在我们非常确信地情况下
36
00:01:15,750 --> 00:01:17,190
very confident that they really do.
才预测一个病人得了癌症
37
00:01:18,010 --> 00:01:18,900
Because you know if you go
因为你知道
38
00:01:19,140 --> 00:01:20,180
to a patient and you tell
如果你告诉一个病人
39
00:01:20,480 --> 00:01:21,570
them that they have cancer, it's
告诉他们你得了癌症
40
00:01:21,710 --> 00:01:22,450
going to give them a huge
他们会非常震惊
41
00:01:22,680 --> 00:01:23,860
shock because this is seriously
因为这是一个
42
00:01:24,220 --> 00:01:25,610
bad news and they may
非常坏的消息
43
00:01:25,700 --> 00:01:27,080
end up going through a pretty
而且他们会经历一段
44
00:01:27,660 --> 00:01:29,570
painful treatment process and so on.
非常痛苦的治疗过程
45
00:01:29,780 --> 00:01:30,770
And so maybe we want to
因此我们希望
46
00:01:30,980 --> 00:01:31,880
tell someone that we think
只有在我们非常确信的情况下
47
00:01:32,090 --> 00:01:34,240
they have cancer only if they're very confident.
才告诉这个人他得了癌症
48
00:01:36,230 --> 00:01:37,210
One way to do this would
这样做的一种方法
49
00:01:37,320 --> 00:01:38,940
be to modify the algorithm, so
是修改算法
50
00:01:39,120 --> 00:01:40,290
that instead of setting the
我们不再将临界值
51
00:01:40,710 --> 00:01:42,270
threshold at 0.5, we
设为0.5
52
00:01:42,820 --> 00:01:44,360
might instead say that we'll
也许 我们只在
53
00:01:44,510 --> 00:01:45,370
predict that y is equal
h(x)的值大于或等于0.7
54
00:01:46,330 --> 00:01:48,630
to 1, only if H of
的情况下
55
00:01:48,700 --> 00:01:50,200
x is greater than or equal to 0.7.
才预测y=1
56
00:01:50,490 --> 00:01:51,620
So this, I think
因此
57
00:01:52,360 --> 00:01:53,400
will tell someone if they
我们会告诉一个人
58
00:01:53,510 --> 00:01:54,530
have cancer only if we think
他得了癌症
59
00:01:54,810 --> 00:01:56,280
there's a greater than, greater
在我们认为
60
00:01:56,730 --> 00:01:59,060
than or equal to 70% that they have cancer.
他有大于等于70%得癌症的概率情况下
61
00:02:00,830 --> 00:02:02,000
And if you do this then
如果你这么做
62
00:02:02,850 --> 00:02:03,740
you're predicting some of this
那么你只在
63
00:02:03,840 --> 00:02:04,990
cancer only when you're
非常确信地情况下
64
00:02:05,100 --> 00:02:07,230
more confident, and so
才预测癌症
65
00:02:07,520 --> 00:02:08,830
you end up with a classifier
那么你的回归模型
66
00:02:09,920 --> 00:02:13,410
that has higher precision, because
会有较高的查准率
67
00:02:14,140 --> 00:02:15,300
all the patients that you're
因为所有你准备
68
00:02:15,450 --> 00:02:16,630
going to and say, you know,
告诉他们
69
00:02:16,860 --> 00:02:18,220
we think you have cancer, all
患有癌症的病人
70
00:02:18,440 --> 00:02:19,760
of those patients are now
所有这些人
71
00:02:20,350 --> 00:02:21,420
pretty, once they hear, pretty
有比较高的可能性
72
00:02:21,720 --> 00:02:23,100
confident they actually have cancer.
他们真的患有癌症
73
00:02:24,260 --> 00:02:26,050
And so, a higher fraction of
你预测患有癌症的病人中
74
00:02:26,150 --> 00:02:27,460
the patients that you predict to
有较大比率的人
75
00:02:27,530 --> 00:02:28,990
have cancer, will actually turn
他们确实患有癌症
76
00:02:29,280 --> 00:02:30,720
out to have cancer, because in
因为这是我们
77
00:02:31,000 --> 00:02:32,870
making those predictions we are pretty confident.
在非常确信的情况下做出的预测
78
00:02:34,510 --> 00:02:36,360
But in contrast, this classifier will
与之相反
79
00:02:36,540 --> 00:02:38,530
have lower recall, because
这个回归模型会有较低的召回率
80
00:02:39,140 --> 00:02:40,220
now we are going
因为
81
00:02:40,340 --> 00:02:41,650
to make predictions, we are
当我们做预测的时候
82
00:02:41,740 --> 00:02:44,180
going to predict y equals one, on a smaller number of patients.
我们只给很小一部分的病人预测y=1
83
00:02:45,090 --> 00:02:45,920
Now we could even take this further.
现在我们把这个情况夸大一下
84
00:02:46,330 --> 00:02:47,520
Instead of setting the threshold
我们不再把临界值
85
00:02:48,080 --> 00:02:49,210
at 0.7, we can set
设在0.7
86
00:02:49,490 --> 00:02:51,550
this at 0.9 and we'll predict
我们把它设为0.9
87
00:02:52,430 --> 00:02:53,270
y1 only if we are
我们只在至少90%肯定
88
00:02:53,320 --> 00:02:54,560
more than 90% certain that
这个病人患有癌症的情况下
89
00:02:55,380 --> 00:02:57,020
the patient has cancer, and so,
预测y=1
90
00:02:57,600 --> 00:02:58,720
you know, a large fraction that
那么这些病人当中
91
00:02:58,850 --> 00:02:59,820
those patients will turn out
有非常大的比率
92
00:03:00,020 --> 00:03:01,380
to have cancer and so,
真正患有癌症
93
00:03:01,560 --> 00:03:03,060
this is the high precision classifier
因此这是一个高查准率的模型
94
00:03:04,160 --> 00:03:06,090
will have lower recall because we
但是召回率会变低
95
00:03:06,190 --> 00:03:08,550
want to correctly detect that those patients have cancer.
因为我们希望能够正确检测患有癌症的病人
96
00:03:09,310 --> 00:03:10,780
Now consider a different example.
现在考虑一个不同的例子
97
00:03:12,100 --> 00:03:13,200
Suppose we want to avoid
假设我们希望
98
00:03:13,470 --> 00:03:15,530
missing too many actual cases of cancer.
避免遗漏掉患有癌症的人
99
00:03:15,960 --> 00:03:17,480
So we want to avoid the false negatives.
即我们希望避免假阴性
100
00:03:18,600 --> 00:03:19,820
In particular, if a patient
具体地说
101
00:03:20,350 --> 00:03:22,280
actually has cancer, but we
如果一个病人实际患有癌症
102
00:03:22,520 --> 00:03:23,700
fail to tell them that
但是我们并没有告诉他患有癌症
103
00:03:23,860 --> 00:03:25,710
they have cancer, then that can be really bad.
那这可能造成严重后果
104
00:03:25,880 --> 00:03:27,460
Because if we tell
因为
105
00:03:27,760 --> 00:03:28,870
a patient that they don't
如果我们告诉病人他们没有患癌症
106
00:03:29,240 --> 00:03:31,460
have cancer then they are
那么
107
00:03:31,530 --> 00:03:32,870
not going to go for treatment and
他们就不会接受治疗
108
00:03:32,980 --> 00:03:33,890
if it turns out that they
但是如果
109
00:03:34,050 --> 00:03:35,380
have cancer or we fail
他们患有癌症
110
00:03:35,520 --> 00:03:36,410
to tell them they have
我们又没有告诉他们
111
00:03:36,660 --> 00:03:39,060
cancer, well they may not get treated at all.
那么他们就根本不会接受治疗
112
00:03:39,430 --> 00:03:40,520
And so that would be
那么
113
00:03:40,640 --> 00:03:41,820
a really bad outcome because he
这么可能造成严重后果
114
00:03:42,080 --> 00:03:43,050
died because we told them
病人丧失生命
115
00:03:43,140 --> 00:03:44,560
they don't have cancer they failed
因为我们没有告诉他患有癌症
116
00:03:44,670 --> 00:03:46,780
to get treated, but it turns
他没有接受治疗
117
00:03:48,230 --> 00:03:48,790
out that they actually have cancer.
但事实上他又患有癌症
118
00:03:49,260 --> 00:03:50,260
When in doubt, we want to
这种i情况下
119
00:03:50,360 --> 00:03:52,430
predict that y equals one.
我们希望预测y=1
120
00:03:52,720 --> 00:03:54,260
So when in doubt, we want
我们希望
121
00:03:54,480 --> 00:03:55,510
to predict that they have
预测病人患有癌症
122
00:03:55,770 --> 00:03:56,820
cancer so that at least
这样
123
00:03:57,110 --> 00:03:58,150
they look further into it
他们会做进一步的检测
124
00:03:59,400 --> 00:04:00,720
and this can get treated,
然后接受治疗
125
00:04:01,180 --> 00:04:02,750
in case they do turn out to have cancer.
以避免他们真的患有癌症
126
00:04:04,870 --> 00:04:06,300
In this case, rather than setting
在这个例子中
127
00:04:06,750 --> 00:04:08,920
higher probability threshold, we might
我们不再设置高的临界值
128
00:04:09,100 --> 00:04:11,370
instead take this value
我们会设置另一个值
129
00:04:12,270 --> 00:04:13,310
and this then sets it to
将临界值
130
00:04:13,540 --> 00:04:14,710
a lower value, so maybe
设得较低
131
00:04:15,060 --> 00:04:17,390
0.3 like so.
比如0.3
132
00:04:18,760 --> 00:04:19,780
By doing so, we're saying
这样做
133
00:04:20,070 --> 00:04:21,380
that, you know what, if we
我们认为
134
00:04:21,480 --> 00:04:22,190
think there's more than a 30%
他们有大于30%的几率
135
00:04:22,220 --> 00:04:24,660
chance that they have caner, we better
患有癌症
136
00:04:24,890 --> 00:04:26,270
be more conservative and tell
我们以更加保守的方式
137
00:04:26,510 --> 00:04:27,330
them that they may have cancer,
告诉他们患有癌症
138
00:04:27,850 --> 00:04:29,610
so they can seek treatment if necessary.
因此他们能够接受治疗
139
00:04:31,110 --> 00:04:32,630
And in this case, what
在这种情况下
140
00:04:32,790 --> 00:04:34,200
we would have is going to
我们会有一个
141
00:04:35,120 --> 00:04:38,260
be a higher recall classifier,
较高召回率的模型
142
00:04:39,550 --> 00:04:41,440
because we're going to
因为
143
00:04:41,580 --> 00:04:43,330
be correctly flagging a higher
确实患有癌症的病人
144
00:04:43,580 --> 00:04:44,760
fraction of all of
有很大一部分
145
00:04:44,800 --> 00:04:45,920
the patients that actually do have
被我们正确标记出来了
146
00:04:46,130 --> 00:04:47,570
cancer, but we're going
但是
147
00:04:47,740 --> 00:04:51,040
to end up with lower precision,
我们会得到较低的查准率
148
00:04:51,670 --> 00:04:53,490
because the higher fraction of
因为
149
00:04:53,600 --> 00:04:54,700
the patients that we said have
我们预测患有癌症的病人比例越大
150
00:04:54,820 --> 00:04:57,530
cancer, the higher fraction of them will turn out not to have cancer after all.
那么就有较大比例的人其实没有患癌症
151
00:05:00,470 --> 00:05:01,320
And by the way, just as an
顺带一提
152
00:05:01,400 --> 00:05:02,640
aside, when I talk
当我在给
153
00:05:02,920 --> 00:05:04,900
about this to other
别的学生讲这个的时候
154
00:05:05,160 --> 00:05:07,760
students up until before, it's pretty amazing.
令人惊讶的是
155
00:05:08,390 --> 00:05:09,720
Some of my students say is
有的学生问
156
00:05:09,850 --> 00:05:11,960
how I can tell the story both ways.
怎么可以从两面来看这个问题
157
00:05:12,550 --> 00:05:14,220
Why we might want to have
为什么我总是
158
00:05:14,450 --> 00:05:15,490
higher precision or higher recall
只想要高查准率或高召回率
159
00:05:16,130 --> 00:05:18,570
and the story actually seems to work both ways.
但是这看起来可以使两边都提高
160
00:05:19,340 --> 00:05:20,550
But I hope the details of
但是我希望
161
00:05:20,670 --> 00:05:22,720
the algorithm is true and the
算法是正确的
162
00:05:22,990 --> 00:05:24,360
more general principle is, depending
更普遍的一个原则是
163
00:05:24,780 --> 00:05:26,150
on where you want, whether
这取决于你想要什么
164
00:05:26,330 --> 00:05:28,010
you want high precision, lower recall
你想要高查准率 低召回率
165
00:05:28,540 --> 00:05:30,340
or higher recall, lower precision, you
还是高召回率 低查准率
166
00:05:30,450 --> 00:05:32,100
can end up predicting y equals
你可以预测y=1
167
00:05:32,540 --> 00:05:35,040
one when h(x) is greater than some threshold.
当h(x)大于某个临界值
168
00:05:36,590 --> 00:05:39,240
And so, in general, for
因此 总的来说
169
00:05:39,880 --> 00:05:41,330
most classifiers, there is going
对于大多数的回归模型
170
00:05:41,540 --> 00:05:44,200
to be a trade off between precision and recall.
你得权衡查准率和召回率
171
00:05:45,360 --> 00:05:46,540
And as you vary the value
当你改变
172
00:05:47,050 --> 00:05:48,700
of this threshold, this value,
临界值的值时
173
00:05:49,030 --> 00:05:49,850
this special that I have
我在这儿画了一个
174
00:05:49,910 --> 00:05:51,470
joined here, you can actually
临界值
175
00:05:51,790 --> 00:05:53,850
plot us some curve that
你可以画出曲线
176
00:05:54,030 --> 00:05:56,060
trades off precision and
来权衡查准率
177
00:05:56,200 --> 00:05:58,010
recall, where a value
和召回率
178
00:05:58,410 --> 00:06:00,020
up here, this would correspond
这里的一个值
179
00:06:01,360 --> 00:06:02,620
to a very high value of
反应出一个较高的临界值
180
00:06:02,770 --> 00:06:04,490
the threshold, maybe threshold equals
这个临界值可能等于0.99
181
00:06:05,420 --> 00:06:06,790
over 0.99, so that say, predict
我们假设
182
00:06:07,090 --> 00:06:08,270
y equals 1 only where
只在有大于99%的确信度的情况下
183
00:06:08,480 --> 00:06:09,640
no more than 99 percent
才预测y=1
184
00:06:10,290 --> 00:06:11,700
confident, at least 99
至少
185
00:06:11,950 --> 00:06:13,460
percent probability this once, so
有99%的可能性
186
00:06:13,760 --> 00:06:15,390
that will be a precision relatively
因此这个点反应高查准率
187
00:06:15,960 --> 00:06:17,550
low recall, whereas the point
低召回率
188
00:06:17,820 --> 00:06:20,380
down here will correspond to
然而这里的一个点
189
00:06:20,490 --> 00:06:22,240
a value of the threshold that's
反映一个较低的临界值
190
00:06:22,450 --> 00:06:24,940
much lower, maybe 0.01.
比如说0.01
191
00:06:25,520 --> 00:06:26,810
When in doubt at all, put down y1.
毫无疑问 在这里预测y=1
192
00:06:27,120 --> 00:06:28,380
And if you do that, you
如果你这么做
193
00:06:28,520 --> 00:06:29,570
end up with a much
你最后会得到
194
00:06:29,760 --> 00:06:31,730
lower precision higher recall classifier.
很低的查准率 但是较高的召回率
195
00:06:33,350 --> 00:06:34,970
And as you vary the threshold, if
当你改变临界值
196
00:06:35,430 --> 00:06:36,550
you want, you can actually trace
如果你愿意
197
00:06:37,000 --> 00:06:38,280
all the curve from your classifier
你可以画出回归模型的所有曲线
198
00:06:38,930 --> 00:06:41,420
to see the range of different values you can get for precision recall.
来看看你能得到的查准率和召回率的范围
199
00:06:43,050 --> 00:06:43,810
And by the way, the position
顺带一提
200
00:06:44,230 --> 00:06:46,860
recall curve can look like many different shapes.
查准率-召回率曲线可以是各种不同的形状