Comparison of Calibration of Classifiers
lr
=
(
Cs
=
(
-
6
,
6
,
101
),
cv
=
10
,
l1_ratios
=
(
0
,),
scoring
=
"neg_log_loss"
,
max_iter
=
1_000
,
use_legacy_attributes
=
False
,
)
gnb
=
()
svc
=
NaivelyCalibratedLinearSVC
(
C
=
1.0
)
rfc
=
(
random_state
=
42
)
clf_list
=
[
(
lr
,
"Logistic Regression"
),
(
gnb
,
"Naive Bayes"
),
(
svc
,
"SVC"
),
(
rfc
,
"Random forest"
),
]
(
4
,
2
)
colors
=
(
"Dark2"
)
ax_calibration_curve
=
fig
.
add_subplot
(
gs
[:
2
,
:
2
])
calibration_displays
=
{}
markers
=
[
"^"
,
"v"
,
"s"
,
"o"
]
for
i
,
(
clf
,
name
)
in
enumerate
(
clf_list
):
clf
.
fit
(
X_train
,
y_train
)
display
=
(
clf
,
X_test
,
y_test
,
n_bins
=
10
,
name
=
name
,
ax
=
ax_calibration_curve
,
color
=
colors
(
i
),
marker
=
markers
[
i
],
)
calibration_displays
[
name
]
=
display
ax_calibration_curve
.
grid
()
ax_calibration_curve
.
set_title
(
"Calibration plots"
)
grid_positions
=
[(
2
,
0
),
(
2
,
1
),
(
3
,
0
),
(
3
,
1
)]
for
i
,
(
_
,
name
)
in
enumerate
(
clf_list
):
row
,
col
=
grid_positions
[
i
]
ax
=
fig
.
add_subplot
(
gs
[
row
,
col
])
ax
.
hist
(
calibration_displays
[
name
]
.
y_prob
,
range
=
(
0
,
1
),
bins
=
10
,
label
=
name
,
color
=
colors
(
i
),
)
ax
.
set
(
title
=
name
,
xlabel
=
"Mean predicted probability"
,
ylabel
=
"Count"
)
()
()
RandomForestClassifier’s prediction histogram
shows peaks at approx. 0.2 and 0.9 probability, while probabilities close to
0 or 1 are very rare. An explanation for this is given by [1]:
“Methods such as bagging and random forests that average
predictions from a base set of models can have difficulty making
predictions near 0 and 1 because variance in the underlying base models
will bias predictions that should be near zero or one away from these
values. Because predictions are restricted to the interval [0, 1], errors
caused by variance tend to be one-sided near zero and one. For example, if
a model should predict p = 0 for a case, the only way bagging can achieve
this is if all bagged trees predict zero. If we add noise to the trees that
bagging is averaging over, this noise will cause some trees to predict
values larger than 0 for this case, thus moving the average prediction of
the bagged ensemble away from 0. We observe this effect most strongly with
random forests because the base-level trees trained with random forests
have relatively high variance due to feature subsetting.” This effect can
make random forests under-confident. Despite this possible bias, note that
the trees themselves are fit by minimizing either the Gini or Entropy
criterion, both of which lead to splits that minimize proper scoring rules:
the Brier score or the log-loss respectively. See the user guide for more details. This can explain why
this model shows a good enough calibration curve on this particular example
dataset. Indeed the Random Forest model is not significantly more
under-confident than the Logistic Regression model.