-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathplot_likelihood_ratios.html
852 lines (787 loc) · 76.3 KB
/
plot_likelihood_ratios.html
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
<!DOCTYPE html>
<!-- data-theme below is forced to be "light" but should be changed if we use pydata-theme-sphinx in the future -->
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" data-content_root="../../" data-theme="light"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" data-content_root="../../" data-theme="light"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="Class Likelihood Ratios to measure classification performance" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://scikit-learn/stable/auto_examples/model_selection/plot_likelihood_ratios.html" />
<meta property="og:site_name" content="scikit-learn" />
<meta property="og:description" content="This example demonstrates the class_likelihood_ratios function, which computes the positive and negative likelihood ratios ( LR+, LR-) to assess the predictive power of a binary classifier. As we w..." />
<meta property="og:image" content="https://scikit-learn.org/stable/_static/scikit-learn-logo-small.png" />
<meta property="og:image:alt" content="scikit-learn" />
<meta name="description" content="This example demonstrates the class_likelihood_ratios function, which computes the positive and negative likelihood ratios ( LR+, LR-) to assess the predictive power of a binary classifier. As we w..." />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Class Likelihood Ratios to measure classification performance — scikit-learn 1.4.2 documentation</title>
<link rel="canonical" href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_likelihood_ratios.html" />
<link rel="shortcut icon" href="../../_static/favicon.ico"/>
<link rel="stylesheet" href="../../_static/css/vendor/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="../../_static/plot_directive.css" type="text/css" />
<link rel="stylesheet" href="../../https://fonts.googleapis.com/css?family=Vibur" type="text/css" />
<link rel="stylesheet" href="../../_static/jupyterlite_sphinx.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-binder.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-dataframe.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-rendered-html.css" type="text/css" />
<link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
<script src="../../_static/js/vendor/jquery-3.6.3.slim.min.js"></script>
<script src="../../_static/js/details-permalink.js"></script>
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
<div class="container-fluid sk-docs-container px-0">
<a class="navbar-brand py-0" href="../../index.html">
<img
class="sk-brand-img"
src="../../_static/scikit-learn-logo-small.png"
alt="logo"/>
</a>
<button
id="sk-navbar-toggler"
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../../install.html">Install</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../../user_guide.html">User Guide</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../../modules/classes.html">API</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" href="../index.html">Examples</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link" target="_blank" rel="noopener noreferrer" href="https://blog.scikit-learn.org/">Community</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../getting_started.html" >Getting Started</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../tutorial/index.html" >Tutorial</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../whats_new/v1.4.html" >What's new</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../glossary.html" >Glossary</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/developers/index.html" target="_blank" rel="noopener noreferrer">Development</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../faq.html" >FAQ</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../support.html" >Support</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../related_projects.html" >Related packages</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../roadmap.html" >Roadmap</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../governance.html" >Governance</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../../about.html" >About us</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-learn/scikit-learn" >GitHub</a>
</li>
<li class="nav-item">
<a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/versions.html" >Other Versions and Download</a>
</li>
<li class="nav-item dropdown nav-more-item-dropdown">
<a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="sk-nav-dropdown-item dropdown-item" href="../../getting_started.html" >Getting Started</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../tutorial/index.html" >Tutorial</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../whats_new/v1.4.html" >What's new</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../glossary.html" >Glossary</a>
<a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/developers/index.html" target="_blank" rel="noopener noreferrer">Development</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../faq.html" >FAQ</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../support.html" >Support</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../related_projects.html" >Related packages</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../roadmap.html" >Roadmap</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../governance.html" >Governance</a>
<a class="sk-nav-dropdown-item dropdown-item" href="../../about.html" >About us</a>
<a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-learn/scikit-learn" >GitHub</a>
<a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/versions.html" >Other Versions and Download</a>
</div>
</li>
</ul>
<div id="searchbox" role="search">
<div class="searchformwrapper">
<form class="search" action="../../search.html" method="get">
<input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
<input class="sk-search-text-btn" type="submit" value="Go" />
</form>
</div>
</div>
</div>
</div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
<input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
<label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
<div id="sk-sidebar-wrapper" class="border-right">
<div class="sk-sidebar-toc-wrapper">
<div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
<a href="plot_grid_search_refit_callable.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Balance model complexity and cross-validated score">Prev</a><a href="index.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Model Selection">Up</a>
<a href="plot_randomized_search.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="Comparing randomized search and grid search for hyperparameter estimation">Next</a>
</div>
<div class="alert alert-danger p-1 mb-2" role="alert">
<p class="text-center mb-0">
<strong>scikit-learn 1.4.2</strong><br/>
<a href="https://scikit-learn.org/dev/versions.html">Other versions</a>
</p>
</div>
<div class="alert alert-warning p-1 mb-2" role="alert">
<p class="text-center mb-0">
Please <a class="font-weight-bold" href="../../about.html#citing-scikit-learn"><string>cite us</string></a> if you use the software.
</p>
</div>
<div class="sk-sidebar-toc">
<ul>
<li><a class="reference internal" href="#">Class Likelihood Ratios to measure classification performance</a><ul>
<li><a class="reference internal" href="#pre-test-vs-post-test-analysis">Pre-test vs. post-test analysis</a></li>
<li><a class="reference internal" href="#cross-validation-of-likelihood-ratios">Cross-validation of likelihood ratios</a></li>
<li><a class="reference internal" href="#invariance-with-respect-to-prevalence">Invariance with respect to prevalence</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div id="sk-page-content-wrapper">
<div class="sk-page-content container-fluid body px-md-3" role="main">
<div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">Note</p>
<p><a class="reference internal" href="#sphx-glr-download-auto-examples-model-selection-plot-likelihood-ratios-py"><span class="std std-ref">Go to the end</span></a>
to download the full example code or to run this example in your browser via JupyterLite or Binder</p>
</div>
<section class="sphx-glr-example-title" id="class-likelihood-ratios-to-measure-classification-performance">
<span id="sphx-glr-auto-examples-model-selection-plot-likelihood-ratios-py"></span><h1>Class Likelihood Ratios to measure classification performance<a class="headerlink" href="#class-likelihood-ratios-to-measure-classification-performance" title="Link to this heading">¶</a></h1>
<p>This example demonstrates the <a class="reference internal" href="../../modules/generated/sklearn.metrics.class_likelihood_ratios.html#sklearn.metrics.class_likelihood_ratios" title="sklearn.metrics.class_likelihood_ratios"><code class="xref py py-func docutils literal notranslate"><span class="pre">class_likelihood_ratios</span></code></a>
function, which computes the positive and negative likelihood ratios (<code class="docutils literal notranslate"><span class="pre">LR+</span></code>,
<code class="docutils literal notranslate"><span class="pre">LR-</span></code>) to assess the predictive power of a binary classifier. As we will see,
these metrics are independent of the proportion between classes in the test set,
which makes them very useful when the available data for a study has a different
class proportion than the target application.</p>
<p>A typical use is a case-control study in medicine, which has nearly balanced
classes while the general population has large class imbalance. In such
application, the pre-test probability of an individual having the target
condition can be chosen to be the prevalence, i.e. the proportion of a
particular population found to be affected by a medical condition. The post-test
probabilities represent then the probability that the condition is truly present
given a positive test result.</p>
<p>In this example we first discuss the link between pre-test and post-test odds
given by the <a class="reference internal" href="../../modules/model_evaluation.html#class-likelihood-ratios"><span class="std std-ref">Class likelihood ratios</span></a>. Then we evaluate their behavior in
some controlled scenarios. In the last section we plot them as a function of the
prevalence of the positive class.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Authors: Arturo Amor <[email protected]></span>
<span class="c1"># Olivier Grisel <[email protected]></span>
</pre></div>
</div>
<section id="pre-test-vs-post-test-analysis">
<h2>Pre-test vs. post-test analysis<a class="headerlink" href="#pre-test-vs-post-test-analysis" title="Link to this heading">¶</a></h2>
<p>Suppose we have a population of subjects with physiological measurements <code class="docutils literal notranslate"><span class="pre">X</span></code>
that can hopefully serve as indirect bio-markers of the disease and actual
disease indicators <code class="docutils literal notranslate"><span class="pre">y</span></code> (ground truth). Most of the people in the population do
not carry the disease but a minority (in this case around 10%) does:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification" class="sphx-glr-backref-module-sklearn-datasets sphx-glr-backref-type-py-function"><span class="n">make_classification</span></a>
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification" class="sphx-glr-backref-module-sklearn-datasets sphx-glr-backref-type-py-function"><span class="n">make_classification</span></a><span class="p">(</span><span class="n">n_samples</span><span class="o">=</span><span class="mi">10_000</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="mf">0.9</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Percentage of people carrying the disease: </span><span class="si">{</span><span class="mi">100</span><span class="o">*</span><span class="n">y</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">%"</span><span class="p">)</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>Percentage of people carrying the disease: 10.37%
</pre></div>
</div>
<p>A machine learning model is built to diagnose if a person with some given
physiological measurements is likely to carry the disease of interest. To
evaluate the model, we need to assess its performance on a held-out test set:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.model_selection.train_test_split.html#sklearn.model_selection.train_test_split" title="sklearn.model_selection.train_test_split" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">train_test_split</span></a>
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.model_selection.train_test_split.html#sklearn.model_selection.train_test_split" title="sklearn.model_selection.train_test_split" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">train_test_split</span></a><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>Then we can fit our diagnosis model and compute the positive likelihood
ratio to evaluate the usefulness of this classifier as a disease diagnosis
tool:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression" class="sphx-glr-backref-module-sklearn-linear_model sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">LogisticRegression</span></a>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.metrics.class_likelihood_ratios.html#sklearn.metrics.class_likelihood_ratios" title="sklearn.metrics.class_likelihood_ratios" class="sphx-glr-backref-module-sklearn-metrics sphx-glr-backref-type-py-function"><span class="n">class_likelihood_ratios</span></a>
<span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression" class="sphx-glr-backref-module-sklearn-linear_model sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">LogisticRegression</span></a><span class="p">()</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">estimator</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
<span class="n">pos_LR</span><span class="p">,</span> <span class="n">neg_LR</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.metrics.class_likelihood_ratios.html#sklearn.metrics.class_likelihood_ratios" title="sklearn.metrics.class_likelihood_ratios" class="sphx-glr-backref-module-sklearn-metrics sphx-glr-backref-type-py-function"><span class="n">class_likelihood_ratios</span></a><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"LR+: </span><span class="si">{</span><span class="n">pos_LR</span><span class="si">:</span><span class="s2">.3f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>LR+: 12.617
</pre></div>
</div>
<p>Since the positive class likelihood ratio is much larger than 1.0, it means
that the machine learning-based diagnosis tool is useful: the post-test odds
that the condition is truly present given a positive test result are more than
12 times larger than the pre-test odds.</p>
</section>
<section id="cross-validation-of-likelihood-ratios">
<h2>Cross-validation of likelihood ratios<a class="headerlink" href="#cross-validation-of-likelihood-ratios" title="Link to this heading">¶</a></h2>
<p>We assess the variability of the measurements for the class likelihood ratios
in some particular cases.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="k">def</span> <span class="nf">scoring</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">estimator</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="n">pos_lr</span><span class="p">,</span> <span class="n">neg_lr</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.metrics.class_likelihood_ratios.html#sklearn.metrics.class_likelihood_ratios" title="sklearn.metrics.class_likelihood_ratios" class="sphx-glr-backref-module-sklearn-metrics sphx-glr-backref-type-py-function"><span class="n">class_likelihood_ratios</span></a><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">raise_warning</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="k">return</span> <span class="p">{</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">:</span> <span class="n">pos_lr</span><span class="p">,</span> <span class="s2">"negative_likelihood_ratio"</span><span class="p">:</span> <span class="n">neg_lr</span><span class="p">}</span>
<span class="k">def</span> <span class="nf">extract_score</span><span class="p">(</span><span class="n">cv_results</span><span class="p">):</span>
<span class="n">lr</span> <span class="o">=</span> <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame" class="sphx-glr-backref-module-pandas sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span></a><span class="p">(</span>
<span class="p">{</span>
<span class="s2">"positive"</span><span class="p">:</span> <span class="n">cv_results</span><span class="p">[</span><span class="s2">"test_positive_likelihood_ratio"</span><span class="p">],</span>
<span class="s2">"negative"</span><span class="p">:</span> <span class="n">cv_results</span><span class="p">[</span><span class="s2">"test_negative_likelihood_ratio"</span><span class="p">],</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">lr</span><span class="o">.</span><span class="n">aggregate</span><span class="p">([</span><span class="s2">"mean"</span><span class="p">,</span> <span class="s2">"std"</span><span class="p">])</span>
</pre></div>
</div>
<p>We first validate the <a class="reference internal" href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogisticRegression</span></code></a> model
with default hyperparameters as used in the previous section.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a>
<span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression" class="sphx-glr-backref-module-sklearn-linear_model sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">LogisticRegression</span></a><span class="p">()</span>
<span class="n">extract_score</span><span class="p">(</span><a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="n">scoring</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
</pre></div>
</div>
<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>positive</th>
<th>negative</th>
</tr>
</thead>
<tbody>
<tr>
<th>mean</th>
<td>16.661086</td>
<td>0.724702</td>
</tr>
<tr>
<th>std</th>
<td>4.383973</td>
<td>0.054045</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br /><p>We confirm that the model is useful: the post-test odds are between 12 and 20
times larger than the pre-test odds.</p>
<p>On the contrary, let’s consider a dummy model that will output random
predictions with similar odds as the average disease prevalence in the
training set:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.dummy</span> <span class="kn">import</span> <a href="../../modules/generated/sklearn.dummy.DummyClassifier.html#sklearn.dummy.DummyClassifier" title="sklearn.dummy.DummyClassifier" class="sphx-glr-backref-module-sklearn-dummy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">DummyClassifier</span></a>
<span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.dummy.DummyClassifier.html#sklearn.dummy.DummyClassifier" title="sklearn.dummy.DummyClassifier" class="sphx-glr-backref-module-sklearn-dummy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">DummyClassifier</span></a><span class="p">(</span><span class="n">strategy</span><span class="o">=</span><span class="s2">"stratified"</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">1234</span><span class="p">)</span>
<span class="n">extract_score</span><span class="p">(</span><a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="n">scoring</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
</pre></div>
</div>
<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>positive</th>
<th>negative</th>
</tr>
</thead>
<tbody>
<tr>
<th>mean</th>
<td>1.108843</td>
<td>0.986989</td>
</tr>
<tr>
<th>std</th>
<td>0.268147</td>
<td>0.034278</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br /><p>Here both class likelihood ratios are compatible with 1.0 which makes this
classifier useless as a diagnostic tool to improve disease detection.</p>
<p>Another option for the dummy model is to always predict the most frequent
class, which in this case is “no-disease”.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.dummy.DummyClassifier.html#sklearn.dummy.DummyClassifier" title="sklearn.dummy.DummyClassifier" class="sphx-glr-backref-module-sklearn-dummy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">DummyClassifier</span></a><span class="p">(</span><span class="n">strategy</span><span class="o">=</span><span class="s2">"most_frequent"</span><span class="p">)</span>
<span class="n">extract_score</span><span class="p">(</span><a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="n">scoring</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
</pre></div>
</div>
<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>positive</th>
<th>negative</th>
</tr>
</thead>
<tbody>
<tr>
<th>mean</th>
<td>NaN</td>
<td>1.0</td>
</tr>
<tr>
<th>std</th>
<td>NaN</td>
<td>0.0</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br /><p>The absence of positive predictions means there will be no true positives nor
false positives, leading to an undefined <code class="docutils literal notranslate"><span class="pre">LR+</span></code> that by no means should be
interpreted as an infinite <code class="docutils literal notranslate"><span class="pre">LR+</span></code> (the classifier perfectly identifying
positive cases). In such situation the
<a class="reference internal" href="../../modules/generated/sklearn.metrics.class_likelihood_ratios.html#sklearn.metrics.class_likelihood_ratios" title="sklearn.metrics.class_likelihood_ratios"><code class="xref py py-func docutils literal notranslate"><span class="pre">class_likelihood_ratios</span></code></a> function returns <code class="docutils literal notranslate"><span class="pre">nan</span></code> and
raises a warning by default. Indeed, the value of <code class="docutils literal notranslate"><span class="pre">LR-</span></code> helps us discard this
model.</p>
<p>A similar scenario may arise when cross-validating highly imbalanced data with
few samples: some folds will have no samples with the disease and therefore
they will output no true positives nor false negatives when used for testing.
Mathematically this leads to an infinite <code class="docutils literal notranslate"><span class="pre">LR+</span></code>, which should also not be
interpreted as the model perfectly identifying positive cases. Such event
leads to a higher variance of the estimated likelihood ratios, but can still
be interpreted as an increment of the post-test odds of having the condition.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression" class="sphx-glr-backref-module-sklearn-linear_model sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">LogisticRegression</span></a><span class="p">()</span>
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification" class="sphx-glr-backref-module-sklearn-datasets sphx-glr-backref-type-py-function"><span class="n">make_classification</span></a><span class="p">(</span><span class="n">n_samples</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="mf">0.9</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">extract_score</span><span class="p">(</span><a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="n">scoring</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
</pre></div>
</div>
<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>positive</th>
<th>negative</th>
</tr>
</thead>
<tbody>
<tr>
<th>mean</th>
<td>17.8000</td>
<td>0.373333</td>
</tr>
<tr>
<th>std</th>
<td>8.5557</td>
<td>0.235430</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br /></section>
<section id="invariance-with-respect-to-prevalence">
<h2>Invariance with respect to prevalence<a class="headerlink" href="#invariance-with-respect-to-prevalence" title="Link to this heading">¶</a></h2>
<p>The likelihood ratios are independent of the disease prevalence and can be
extrapolated between populations regardless of any possible class imbalance,
<strong>as long as the same model is applied to all of them</strong>. Notice that in the
plots below <strong>the decision boundary is constant</strong> (see
<a class="reference internal" href="../svm/plot_separating_hyperplane_unbalanced.html#sphx-glr-auto-examples-svm-plot-separating-hyperplane-unbalanced-py"><span class="std std-ref">SVM: Separating hyperplane for unbalanced classes</span></a> for
a study of the boundary decision for unbalanced classes).</p>
<p>Here we train a <a class="reference internal" href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression"><code class="xref py py-class docutils literal notranslate"><span class="pre">LogisticRegression</span></code></a> base model
on a case-control study with a prevalence of 50%. It is then evaluated over
populations with varying prevalence. We use the
<a class="reference internal" href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_classification</span></code></a> function to ensure the
data-generating process is always the same as shown in the plots below. The
label <code class="docutils literal notranslate"><span class="pre">1</span></code> corresponds to the positive class “disease”, whereas the label <code class="docutils literal notranslate"><span class="pre">0</span></code>
stands for “no-disease”.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" title="collections.defaultdict" class="sphx-glr-backref-module-collections sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">defaultdict</span></a>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">sklearn.inspection</span> <span class="kn">import</span> <span class="n">DecisionBoundaryDisplay</span>
<span class="n">populations</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" title="collections.defaultdict" class="sphx-glr-backref-module-collections sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">defaultdict</span></a><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="n">common_params</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"n_samples"</span><span class="p">:</span> <span class="mi">10_000</span><span class="p">,</span>
<span class="s2">"n_features"</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="s2">"n_informative"</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="s2">"n_redundant"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s2">"random_state"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">weights</span> <span class="o">=</span> <a href="https://numpy.org/doc/stable/reference/generated/numpy.linspace.html#numpy.linspace" title="numpy.linspace" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">linspace</span></a><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="n">weights</span> <span class="o">=</span> <span class="n">weights</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="c1"># fit and evaluate base model on balanced classes</span>
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification" class="sphx-glr-backref-module-sklearn-datasets sphx-glr-backref-type-py-function"><span class="n">make_classification</span></a><span class="p">(</span><span class="o">**</span><span class="n">common_params</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">])</span>
<span class="n">estimator</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" title="sklearn.linear_model.LogisticRegression" class="sphx-glr-backref-module-sklearn-linear_model sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">LogisticRegression</span></a><span class="p">()</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">lr_base</span> <span class="o">=</span> <span class="n">extract_score</span><span class="p">(</span><a href="../../modules/generated/sklearn.model_selection.cross_validate.html#sklearn.model_selection.cross_validate" title="sklearn.model_selection.cross_validate" class="sphx-glr-backref-module-sklearn-model_selection sphx-glr-backref-type-py-function"><span class="n">cross_validate</span></a><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="n">scoring</span><span class="p">,</span> <span class="n">cv</span><span class="o">=</span><span class="mi">10</span><span class="p">))</span>
<span class="n">pos_lr_base</span><span class="p">,</span> <span class="n">pos_lr_base_std</span> <span class="o">=</span> <span class="n">lr_base</span><span class="p">[</span><span class="s2">"positive"</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
<span class="n">neg_lr_base</span><span class="p">,</span> <span class="n">neg_lr_base_std</span> <span class="o">=</span> <span class="n">lr_base</span><span class="p">[</span><span class="s2">"negative"</span><span class="p">]</span><span class="o">.</span><span class="n">values</span>
</pre></div>
</div>
<p>We will now show the decision boundary for each level of prevalence. Note that
we only plot a subset of the original data to better assess the linear model
decision boundary.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="n">fig</span><span class="p">,</span> <span class="n">axs</span> <span class="o">=</span> <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="n">nrows</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">12</span><span class="p">))</span>
<span class="k">for</span> <span class="n">ax</span><span class="p">,</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">weight</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">axs</span><span class="o">.</span><span class="n">ravel</span><span class="p">(),</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">weights</span><span class="p">)):</span>
<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.datasets.make_classification.html#sklearn.datasets.make_classification" title="sklearn.datasets.make_classification" class="sphx-glr-backref-module-sklearn-datasets sphx-glr-backref-type-py-function"><span class="n">make_classification</span></a><span class="p">(</span>
<span class="o">**</span><span class="n">common_params</span><span class="p">,</span>
<span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="n">weight</span><span class="p">,</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">weight</span><span class="p">],</span>
<span class="p">)</span>
<span class="n">prevalence</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="n">populations</span><span class="p">[</span><span class="s2">"prevalence"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">prevalence</span><span class="p">)</span>
<span class="n">populations</span><span class="p">[</span><span class="s2">"X"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="n">populations</span><span class="p">[</span><span class="s2">"y"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="c1"># down-sample for plotting</span>
<span class="n">rng</span> <span class="o">=</span> <a href="https://numpy.org/doc/stable/reference/random/legacy.html#numpy.random.RandomState" title="numpy.random.RandomState" class="sphx-glr-backref-module-numpy-random sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">RandomState</span></a><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">plot_indices</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><a href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html#numpy.arange" title="numpy.arange" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">arange</span></a><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">size</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">X_plot</span><span class="p">,</span> <span class="n">y_plot</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">plot_indices</span><span class="p">],</span> <span class="n">y</span><span class="p">[</span><span class="n">plot_indices</span><span class="p">]</span>
<span class="c1"># plot fixed decision boundary of base model with varying prevalence</span>
<span class="n">disp</span> <span class="o">=</span> <a href="../../modules/generated/sklearn.inspection.DecisionBoundaryDisplay.html#sklearn.inspection.DecisionBoundaryDisplay.from_estimator" title="sklearn.inspection.DecisionBoundaryDisplay.from_estimator" class="sphx-glr-backref-module-sklearn-inspection-DecisionBoundaryDisplay sphx-glr-backref-type-py-method"><span class="n">DecisionBoundaryDisplay</span><span class="o">.</span><span class="n">from_estimator</span></a><span class="p">(</span>
<span class="n">estimator</span><span class="p">,</span>
<span class="n">X_plot</span><span class="p">,</span>
<span class="n">response_method</span><span class="o">=</span><span class="s2">"predict"</span><span class="p">,</span>
<span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">scatter</span> <span class="o">=</span> <span class="n">disp</span><span class="o">.</span><span class="n">ax_</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">X_plot</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">X_plot</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">c</span><span class="o">=</span><span class="n">y_plot</span><span class="p">,</span> <span class="n">edgecolor</span><span class="o">=</span><span class="s2">"k"</span><span class="p">)</span>
<span class="n">disp</span><span class="o">.</span><span class="n">ax_</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="sa">f</span><span class="s2">"prevalence = </span><span class="si">{</span><span class="n">y_plot</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">disp</span><span class="o">.</span><span class="n">ax_</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="o">*</span><span class="n">scatter</span><span class="o">.</span><span class="n">legend_elements</span><span class="p">())</span>
</pre></div>
</div>
<img src="../../_images/sphx_glr_plot_likelihood_ratios_001.png" srcset="../../_images/sphx_glr_plot_likelihood_ratios_001.png" alt="prevalence = 0.22, prevalence = 0.34, prevalence = 0.45, prevalence = 0.60, prevalence = 0.76, prevalence = 0.88" class = "sphx-glr-single-img"/><p>We define a function for bootstrapping.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">scoring_on_bootstrap</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">n_bootstrap</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
<span class="n">results_for_prevalence</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" title="collections.defaultdict" class="sphx-glr-backref-module-collections sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">defaultdict</span></a><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_bootstrap</span><span class="p">):</span>
<span class="n">bootstrap_indices</span> <span class="o">=</span> <span class="n">rng</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span>
<a href="https://numpy.org/doc/stable/reference/generated/numpy.arange.html#numpy.arange" title="numpy.arange" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">arange</span></a><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">size</span><span class="o">=</span><span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">replace</span><span class="o">=</span><span class="kc">True</span>
<span class="p">)</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">scoring</span><span class="p">(</span>
<span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">[</span><span class="n">bootstrap_indices</span><span class="p">],</span> <span class="n">y</span><span class="p">[</span><span class="n">bootstrap_indices</span><span class="p">]</span>
<span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">results_for_prevalence</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame" class="sphx-glr-backref-module-pandas sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span></a><span class="p">(</span><span class="n">results_for_prevalence</span><span class="p">)</span>
</pre></div>
</div>
<p>We score the base model for each prevalence using bootstrapping.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" title="collections.defaultdict" class="sphx-glr-backref-module-collections sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">defaultdict</span></a><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="n">n_bootstrap</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">rng</span> <span class="o">=</span> <a href="https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.default_rng" title="numpy.random.default_rng" class="sphx-glr-backref-module-numpy-random sphx-glr-backref-type-py-function"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">default_rng</span></a><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">prevalence</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span>
<span class="n">populations</span><span class="p">[</span><span class="s2">"prevalence"</span><span class="p">],</span> <span class="n">populations</span><span class="p">[</span><span class="s2">"X"</span><span class="p">],</span> <span class="n">populations</span><span class="p">[</span><span class="s2">"y"</span><span class="p">]</span>
<span class="p">):</span>
<span class="n">results_for_prevalence</span> <span class="o">=</span> <span class="n">scoring_on_bootstrap</span><span class="p">(</span>
<span class="n">estimator</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">rng</span><span class="p">,</span> <span class="n">n_bootstrap</span><span class="o">=</span><span class="n">n_bootstrap</span>
<span class="p">)</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"prevalence"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">prevalence</span><span class="p">)</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"metrics"</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="n">results_for_prevalence</span><span class="o">.</span><span class="n">aggregate</span><span class="p">([</span><span class="s2">"mean"</span><span class="p">,</span> <span class="s2">"std"</span><span class="p">])</span><span class="o">.</span><span class="n">unstack</span><span class="p">()</span>
<span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame" class="sphx-glr-backref-module-pandas sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span></a><span class="p">(</span><span class="n">results</span><span class="p">[</span><span class="s2">"metrics"</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="n">results</span><span class="p">[</span><span class="s2">"prevalence"</span><span class="p">])</span>
<span class="n">results</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">"prevalence"</span>
<span class="n">results</span>
</pre></div>
</div>
<div class="output_subarea output_html rendered_html output_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead tr th {
text-align: left;
}
.dataframe thead tr:last-of-type th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr>
<th></th>
<th colspan="2" halign="left">positive_likelihood_ratio</th>
<th colspan="2" halign="left">negative_likelihood_ratio</th>
</tr>
<tr>
<th></th>
<th>mean</th>
<th>std</th>
<th>mean</th>
<th>std</th>
</tr>
<tr>
<th>prevalence</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>0.2039</th>
<td>4.507943</td>
<td>0.113516</td>
<td>0.207667</td>
<td>0.009778</td>
</tr>
<tr>
<th>0.3419</th>
<td>4.443238</td>
<td>0.125140</td>
<td>0.198766</td>
<td>0.008915</td>
</tr>
<tr>
<th>0.4809</th>
<td>4.421087</td>
<td>0.123828</td>
<td>0.192913</td>
<td>0.006360</td>
</tr>
<tr>
<th>0.6196</th>
<td>4.409717</td>
<td>0.164009</td>
<td>0.193949</td>
<td>0.005861</td>
</tr>
<tr>
<th>0.7578</th>
<td>4.334795</td>
<td>0.175298</td>
<td>0.189267</td>
<td>0.005840</td>
</tr>
<tr>
<th>0.8963</th>
<td>4.197666</td>
<td>0.238955</td>
<td>0.185654</td>
<td>0.005027</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br /><p>In the plots below we observe that the class likelihood ratios re-computed
with different prevalences are indeed constant within one standard deviation
of those computed with on balanced classes.</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="n">fig</span><span class="p">,</span> <span class="p">(</span><span class="n">ax1</span><span class="p">,</span> <span class="n">ax2</span><span class="p">)</span> <span class="o">=</span> <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="n">nrows</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">ncols</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax1</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"r"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"extrapolation through populations"</span>
<span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">pos_lr_base</span> <span class="o">+</span> <span class="n">pos_lr_base_std</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"r"</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">"--"</span><span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span>
<span class="n">y</span><span class="o">=</span><span class="n">pos_lr_base</span> <span class="o">-</span> <span class="n">pos_lr_base_std</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s2">"r"</span><span class="p">,</span>
<span class="n">linestyle</span><span class="o">=</span><span class="s2">"--"</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="s2">"base model confidence band"</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span>
<span class="n">results</span><span class="o">.</span><span class="n">index</span><span class="p">,</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span>
<span class="o">-</span> <span class="n">results</span><span class="p">[</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">][</span><span class="s2">"std"</span><span class="p">],</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span>
<span class="o">+</span> <span class="n">results</span><span class="p">[</span><span class="s2">"positive_likelihood_ratio"</span><span class="p">][</span><span class="s2">"std"</span><span class="p">],</span>
<span class="n">color</span><span class="o">=</span><span class="s2">"r"</span><span class="p">,</span>
<span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">set</span><span class="p">(</span>
<span class="n">title</span><span class="o">=</span><span class="s2">"Positive likelihood ratio"</span><span class="p">,</span>
<span class="n">ylabel</span><span class="o">=</span><span class="s2">"LR+"</span><span class="p">,</span>
<span class="n">ylim</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
<span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s2">"lower right"</span><span class="p">)</span>
<span class="n">ax2</span> <span class="o">=</span> <span class="n">results</span><span class="p">[</span><span class="s2">"negative_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span>
<span class="n">ax</span><span class="o">=</span><span class="n">ax2</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"b"</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">"extrapolation through populations"</span>
<span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">neg_lr_base</span> <span class="o">+</span> <span class="n">neg_lr_base_std</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">"b"</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s2">"--"</span><span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span>
<span class="n">y</span><span class="o">=</span><span class="n">neg_lr_base</span> <span class="o">-</span> <span class="n">neg_lr_base_std</span><span class="p">,</span>
<span class="n">color</span><span class="o">=</span><span class="s2">"b"</span><span class="p">,</span>
<span class="n">linestyle</span><span class="o">=</span><span class="s2">"--"</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="s2">"base model confidence band"</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span>
<span class="n">results</span><span class="o">.</span><span class="n">index</span><span class="p">,</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"negative_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span>
<span class="o">-</span> <span class="n">results</span><span class="p">[</span><span class="s2">"negative_likelihood_ratio"</span><span class="p">][</span><span class="s2">"std"</span><span class="p">],</span>
<span class="n">results</span><span class="p">[</span><span class="s2">"negative_likelihood_ratio"</span><span class="p">][</span><span class="s2">"mean"</span><span class="p">]</span>
<span class="o">+</span> <span class="n">results</span><span class="p">[</span><span class="s2">"negative_likelihood_ratio"</span><span class="p">][</span><span class="s2">"std"</span><span class="p">],</span>
<span class="n">color</span><span class="o">=</span><span class="s2">"b"</span><span class="p">,</span>
<span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">set</span><span class="p">(</span>
<span class="n">title</span><span class="o">=</span><span class="s2">"Negative likelihood ratio"</span><span class="p">,</span>
<span class="n">ylabel</span><span class="o">=</span><span class="s2">"LR-"</span><span class="p">,</span>
<span class="n">ylim</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">],</span>
<span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s2">"lower right"</span><span class="p">)</span>
<a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><span class="n">plt</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<img src="../../_images/sphx_glr_plot_likelihood_ratios_002.png" srcset="../../_images/sphx_glr_plot_likelihood_ratios_002.png" alt="Positive likelihood ratio, Negative likelihood ratio" class = "sphx-glr-single-img"/><p class="sphx-glr-timing"><strong>Total running time of the script:</strong> (0 minutes 2.109 seconds)</p>
<div class="sphx-glr-footer sphx-glr-footer-example docutils container" id="sphx-glr-download-auto-examples-model-selection-plot-likelihood-ratios-py">
<div class="binder-badge docutils container">
<a class="reference external image-reference" href="https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.4.X?urlpath=lab/tree/notebooks/auto_examples/model_selection/plot_likelihood_ratios.ipynb"><img alt="Launch binder" src="../../_images/binder_badge_logo22.svg" width="150px" /></a>
</div>
<div class="lite-badge docutils container">
<a class="reference external image-reference" href="../../lite/lab/?path=auto_examples/model_selection/plot_likelihood_ratios.ipynb"><img alt="Launch JupyterLite" src="../../_images/jupyterlite_badge_logo22.svg" width="150px" /></a>
</div>
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/a396baaf1f191c4fd27c35ae5b37420c/plot_likelihood_ratios.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">plot_likelihood_ratios.ipynb</span></code></a></p>
</div>
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/883c6b4b0cc369a10ddb336c09a9e4dd/plot_likelihood_ratios.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">plot_likelihood_ratios.py</span></code></a></p>
</div>
</div>
<p class="rubric">Related examples</p>
<div class="sphx-glr-thumbnails"><div class="sphx-glr-thumbcontainer" tooltip="Feature scaling through standardization, also called Z-score normalization, is an important pre..."><img alt="" src="../../_images/sphx_glr_plot_scaling_importance_thumb.png" />
<p><a class="reference internal" href="../preprocessing/plot_scaling_importance.html#sphx-glr-auto-examples-preprocessing-plot-scaling-importance-py"><span class="std std-ref">Importance of Feature Scaling</span></a></p>
<div class="sphx-glr-thumbnail-title">Importance of Feature Scaling</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This example compares two different bayesian regressors:"><img alt="" src="../../_images/sphx_glr_plot_ard_thumb.png" />
<p><a class="reference internal" href="../linear_model/plot_ard.html#sphx-glr-auto-examples-linear-model-plot-ard-py"><span class="std std-ref">Comparing Linear Bayesian Regressors</span></a></p>
<div class="sphx-glr-thumbnail-title">Comparing Linear Bayesian Regressors</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This example shows how to use KNeighborsClassifier. We train such a classifier on the iris data..."><img alt="" src="../../_images/sphx_glr_plot_classification_thumb.png" />
<p><a class="reference internal" href="../neighbors/plot_classification.html#sphx-glr-auto-examples-neighbors-plot-classification-py"><span class="std std-ref">Nearest Neighbors Classification</span></a></p>
<div class="sphx-glr-thumbnail-title">Nearest Neighbors Classification</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This example shows how to use cross_val_predict together with PredictionErrorDisplay to visuali..."><img alt="" src="../../_images/sphx_glr_plot_cv_predict_thumb.png" />
<p><a class="reference internal" href="plot_cv_predict.html#sphx-glr-auto-examples-model-selection-plot-cv-predict-py"><span class="std std-ref">Plotting Cross-Validated Predictions</span></a></p>
<div class="sphx-glr-thumbnail-title">Plotting Cross-Validated Predictions</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="In this example, we will compare the training times and prediction performances of HistGradient..."><img alt="" src="../../_images/sphx_glr_plot_gradient_boosting_categorical_thumb.png" />
<p><a class="reference internal" href="../ensemble/plot_gradient_boosting_categorical.html#sphx-glr-auto-examples-ensemble-plot-gradient-boosting-categorical-py"><span class="std std-ref">Categorical Feature Support in Gradient Boosting</span></a></p>
<div class="sphx-glr-thumbnail-title">Categorical Feature Support in Gradient Boosting</div>
</div></div><p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</section>
</section>
</div>
<div class="container">
<footer class="sk-content-footer">
© 2007 - 2024, scikit-learn developers (BSD License).
<a href="../../_sources/auto_examples/model_selection/plot_likelihood_ratios.rst.txt" rel="nofollow">Show this page source</a>
</footer>
</div>
</div>
</div>
<script src="../../_static/js/vendor/bootstrap.min.js"></script>
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-22606712-2', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<script defer data-domain="scikit-learn.org" src="https://views.scientific-python.org/js/script.js">
</script>
<script src="../../_static/clipboard.min.js"></script>
<script src="../../_static/copybutton.js"></script>
<script>
$(document).ready(function() {
/* Add a [>>>] button on the top-right corner of code samples to hide
* the >>> and ... prompts and the output and thus make the code
* copyable. */
var div = $('.highlight-python .highlight,' +
'.highlight-python3 .highlight,' +
'.highlight-pycon .highlight,' +
'.highlight-default .highlight')
var pre = div.find('pre');
// get the styles from the current theme
pre.parent().parent().css('position', 'relative');
// create and add the button to all the code blocks that contain >>>
div.each(function(index) {
var jthis = $(this);
// tracebacks (.gt) contain bare text elements that need to be
// wrapped in a span to work with .nextUntil() (see later)
jthis.find('pre:has(.gt)').contents().filter(function() {
return ((this.nodeType == 3) && (this.data.trim().length > 0));
}).wrap('<span>');
});
/*** Add permalink buttons next to glossary terms ***/
$('dl.glossary > dt[id]').append(function() {
return ('<a class="headerlink" href="#' +
this.getAttribute('id') +
'" title="Permalink to this term">¶</a>');
});
});
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<script src="https://scikit-learn.org/versionwarning.js"></script>
</body>
</html>