1
+ """
2
+ This file deals with the implementation of the Hosmer-Lemeshow plot for the
3
+ assessment of calibration of predicted probabilities.
4
+ """
5
+
1
6
import numpy as np
2
7
from typing import Tuple
3
8
import matplotlib .pyplot as plt
6
11
7
12
8
13
def ntile_name (n : int ) -> str :
9
- """Returns the ntile name corresponding to an ntile integer.
14
+ """
15
+ Returns the ntile name corresponding to an ntile integer.
16
+
10
17
Parameters
11
18
----------
12
19
n : int
13
20
An ntile integer.
21
+
14
22
Returns
15
23
-------
16
24
ntile_name : str
@@ -30,13 +38,16 @@ def ntile_name(n: int) -> str:
30
38
31
39
def make_recarray (y_true : ArrayLike ,
32
40
y_pred : ArrayLike ) -> np .recarray :
33
- """Combines arrays into a recarray.
41
+ """
42
+ Combines arrays into a recarray.
43
+
34
44
Parameters
35
45
----------
36
46
y_true : array
37
47
Observed labels, either 0 or 1.
38
48
y_pred : array
39
49
Predicted probabilities, floats on [0, 1].
50
+
40
51
Returns
41
52
-------
42
53
table : recarray
@@ -53,7 +64,9 @@ def make_recarray(y_true: ArrayLike,
53
64
def hosmer_lemeshow_table (y_true : ArrayLike ,
54
65
y_pred : ArrayLike ,
55
66
n_bins : int = 10 ) -> np .recarray :
56
- """Constructs a Hosmer–Lemeshow table.
67
+ """
68
+ Constructs a Hosmer–Lemeshow table.
69
+
57
70
Parameters
58
71
----------
59
72
y_true : array
@@ -63,6 +76,7 @@ def hosmer_lemeshow_table(y_true: ArrayLike,
63
76
n_bins : int, optional
64
77
The number of groups to create. The default value is 10, which
65
78
corresponds to deciles of predicted probabilities.
79
+
66
80
Returns
67
81
-------
68
82
table : recarray
@@ -100,26 +114,28 @@ def hosmer_lemeshow_plot(y_true: ArrayLike,
100
114
101
115
Parameters
102
116
----------
103
- y_true: ArrayLike
117
+ y_true : ArrayLike
104
118
(n_obs,) shaped array of ground-truth values
105
- y_pred: ArrayLike
119
+ y_pred : ArrayLike
106
120
(n_obs,) shaped array of predicted probabilities
107
- n_bins: int
121
+ n_bins : int
108
122
Number of bins to group observed and predicted probabilities into
109
- colors: Tuple[str, str]
123
+ colors : Tuple[str, str]
110
124
Pair of colors for observed (line) and predicted (vertical bars) probabilities.
111
- annotate_bars: bool
125
+ annotate_bars : bool
112
126
Whether bars should be annotated with the number of observed probabilities in each bin.
113
- title: str
127
+ title : str
114
128
Title to display on top of the calibration plot.
115
- brier_score_annot: str
129
+ brier_score_annot : str
116
130
Optional brier score (95% CI) annotation on the top-left corner.
117
- ax: plt.Axes
131
+ ax : plt.Axes
118
132
A matplotlib Axes object to draw the calibration plot into. If None, an Axes object is created by default.
133
+
119
134
Returns
120
135
-------
121
- Tuple[plt.Figure, plt.Axes]:
122
- Corresponding figure and Axes
136
+ f, ax : Tuple[plt.Figure, plt.Axes]
137
+ f: pyplot figure
138
+ ax: pyplot Axes
123
139
"""
124
140
table = hosmer_lemeshow_table (y_true , y_pred , n_bins )
125
141
# transform observed and predicted frequencies in percentage relative to the bin dimension
0 commit comments