library("mcboost")
library("mlr3")
set.seed(83007)
As a brief introduction we show how to use mcboost in only 6 lines of code. For our example, we use the data from the sonar binary classification task. We instantiate a MCBoost
instance by specifying a auditor_fitter
. This auditor_fitter
defines the splits into groups in each boosting iteration based on the obtained residuals. In this example, we choose a Tree
based model. Afterwards, we run the $multicalibrate()
method on our data to start multi-calibration. We only use the first 200 samples of the sonar data set to train our multi-calibrated model.
tsk("sonar")
tsk = tsk$data(cols = tsk$feature_names)
d = tsk$data(cols = tsk$target_names)[[1]]
l = MCBoost$new(auditor_fitter = "TreeAuditorFitter")
mc =$multicalibrate(d[1:200,], l[1:200]) mc
After the calibration, we use the model to predict on the left-out data (8 observations).
$predict_probs(d[201:208,]) mc
Internally mcboost runs the following procedure max_iter
times:
init_predictor
in the first iteration.res = y - y_hat
num_buckets
according to y_hat
.auditor_fitter
) (here calledc(x)
) on the data in each bucket with target variable r
.misscal = mean(c(x) * res(x))
misscal > alpha
: For the bucket with highest misscal
, update the model using the prediction c(x)
. else: Stop the procedureA lot more details can be found either in the code, or in the corresponding publications.
First we download the data and create an mlr3
classification task:
library(data.table)
fread(
adult_train ="https://raw.githubusercontent.com/Yorko/mlcourse.ai/master/data/adult_train.csv",
stringsAsFactors = TRUE
)$Country = NULL
adult_train$fnlwgt = NULL
adult_train TaskClassif$new("adult_train", adult_train, target = "Target") train_tsk =
We removed the features Country
and fnlwgt
since we expect them to have no predictive power. fnlwgt
means final weight and aims to allocate similar weights to people with similar demographic characteristics, while Country
has 42 distinct levels but 89 % of the observations are from the United States.
Then we do basic preprocessing:
library(mlr3pipelines)
po("collapsefactors", no_collapse_above_prevalence = 0.0006) %>>%
pipe = po("fixfactors") %>>%
po("encode") %>>%
po("imputehist")
pipe$train(train_tsk)[[1]] prep_task =
In order to simulate settings where a sensitive feature is not available, we remove the (dummy encoded) feature Race
from the training task.
$set_col_roles(c("Race.Amer.Indian.Eskimo", "Race.Asian.Pac.Islander", "Race.Black", "Race.Other", "Race.White"), remove_from = "feature") prep_task
Now we fit a random forest
.
library(mlr3learners)
lrn("classif.ranger", num.trees = 10L, predict_type = "prob")
l =$train(prep_task) l
A simple way to use the predictions from any model
in mcboost is to wrap the predict function and provide it as an initial predictor. This can be done from any model / any library. Note, that we have to make sure, that our init_predictor
returns a numeric vector of predictions.
function(data) {
init_predictor =$predict_newdata(data)$prob[, 2]
l }
As mcboost requires the data to be provided in X, y
format (a data.table
or data.frame
of features and a vector of labels), we create those two objects.
prep_task$data(cols = prep_task$feature_names)
data = 1 - one_hot(prep_task$data(cols = prep_task$target_names)[[1]]) labels =
We use a ridge regularized linear regression model as the auditor.
MCBoost$new(auditor_fitter = "RidgeAuditorFitter", init_predictor = init_predictor)
mc =$multicalibrate(data, labels) mc
The print
method additionally lists the average auditor values in the different buckets in each iteration:
mc
fread(
adult_test ="https://raw.githubusercontent.com/Yorko/mlcourse.ai/master/data/adult_test.csv",
stringsAsFactors = TRUE
)$Country = NULL
adult_test$fnlwgt = NULL
adult_test
# The first row seems to have an error
adult_test[Target != "",]
adult_test =$Target = droplevels(adult_test$Target)
adult_test
# Note, that we have to convert columns from numeric to integer here:
train_tsk$feature_types[type == "integer", id]
sdc =:= lapply(.SD, as.integer), .SDcols = sdc]
adult_test[, (sdc)
TaskClassif$new("adult_test", adult_test, target = "Target")
test_tsk = pipe$predict(test_tsk)[[1]] prep_test =
Now, we can again extract X, y
.
prep_test$data(cols = prep_test$feature_names)
test_data = 1 - one_hot(prep_test$data(cols = prep_test$target_names)[[1]]) test_labels =
and predict.
mc$predict_probs(test_data) prs =
The accuracy of the multi-calibrated model
mean(round(prs) == test_labels)
is similar to the non-calibrated model.
mean(round(init_predictor(test_data)) == test_labels)
But if we have a look at the bias for the different subpopulations of feature Race
, we can see that the predictions got more calibrated. Note that we did not explicitly give neither the initial model nor the auditor access to the feature Race
.
# Get bias per subgroup for multi-calibrated predictor
$biasmc = (prs - test_labels)
adult_testabs(mean(biasmc)), .N), by = .(Race)]
adult_test[, .(# Get bias per subgroup for initial predictor
$biasinit = (init_predictor(test_data) - test_labels)
adult_testabs(mean(biasinit)), .N), by = .(Race)] adult_test[, .(
We can also obtain the auditor effect after multicalibration. This indicates “how much” each observation has been affected by multi-calibration (on average across iterations).
mc$auditor_effect(test_data)
ae =hist(ae)
We can see that there are a few instances with more pronounced effects, while most have actually only a low effect.
In order to get more insights, we compute quantiles of the less and more effected population (median as cut-point) and analyze differences.
apply(test_data[ae >= median(ae[ae > 0]),], 2, quantile)
effect = apply(test_data[ae < median(ae[ae>0]),], 2, quantile)
no_effect = apply((effect-no_effect), 2, mean)
difference => 0.1] difference[difference
There seems to be a difference in some variables like Education
and Marital_Status
.
We can further analyze the individuals:
>= median(ae[ae>0]), names(which(difference > 0.1)), with = FALSE] test_data[ae
Multi-calibration is an iterative procedure. The t
parameter can be used to predict using only the first t
iterations. This then predicts using only the first t
iterations of the multi-calibration procedure.
mc$predict_probs(test_data, t = 3L) prs =
mcboost
does not require your model to be a mlr3
model. As an input, mcboost
expects a function init_predictor
that takes as input data
and returns a prediction.
tsk("sonar")
tsk = tsk$data()[, Class := as.integer(Class) - 1L]
data = glm(data = data, formula = Class ~ .) mod =
The init_predictor
could then use the glm
model:
function(data) {
init_predictor =predict(mod, data)
}
… and we can calibrate this predictor.
data[, -1]
d = data$Class
l = MCBoost$new(init_predictor = init_predictor)
mc =$multicalibrate(d[1:200,], l[1:200])
mc$predict_probs(d[201:208,]) mc
Very often MCBoost
’s calibration is very aggressive and tends to overfit. This section tries to introduce a method to regularize against this overfitting.
In this section we use a Cross-Validated
learner that predicts on held-out data during the training phase. This idea is based on Wolpert (1992)’s Stacked Generalization. Other, simpler methods include choosing a smaller step size eta
or reducing the number of iters
.
tsk("sonar") tsk =
As an init_predictor
we again use a ranger
model from mlr3 and construct an init predictor using the convenience function provided by mcboost
.
lrn("classif.ranger", predict_type = "prob")
learner =$train(tsk)
learner mlr3_init_predictor(learner) init_predictor =
… and we can calibrate this predictor. This time, we use a CVTreeAuditorFitter
instead of a TreeAuditorFitter
. This allows us to avoid overfitting similar to a technique coined stacked generalization
first described by Wolpert in 1992. Note, that this can sometimes take a little longer since each learner is cross-validated using 3
folds (default).
data[, -1]
d = data$Class
l = MCBoost$new(init_predictor = init_predictor, auditor_fitter=CVTreeAuditorFitter$new(), max_iter = 2L)
mc =$multicalibrate(d[1:200,], l[1:200])
mc$predict_probs(d[201:208,]) mc
We can also use a fresh chunk of the validation data in each iteration. mcboost
implements two strategies, "bootstrap"
and "split"
. While "split"
simply splits up the data, "bootstrap"
draws a new bootstrap sample of the data in each iteration.
tsk("sonar") tsk =
Again, we use a ranger
mlr3 model as our initial predictor:
lrn("classif.ranger", predict_type = "prob")
learner =$train(tsk)
learner mlr3_init_predictor(learner) init_predictor =
and we can now calibrate:
data[, -1]
d = data$Class
l = MCBoost$new(
mc =init_predictor = init_predictor,
auditor_fitter= TreeAuditorFitter$new(),
iter_sampling = "bootstrap"
)$multicalibrate(d[1:200,], l[1:200])
mc$predict_probs(d[201:208,]) mc
For this example, we use the sonar dataset once again:
tsk("sonar")
tsk = tsk$data(cols = tsk$feature_names)
data = tsk$data(cols = tsk$target_names)[[1]] labels =
The Subpop-fitter can be easily adjusted by constructing it from a LearnerAuditorFitter
. This allows for using any mlr3 learner. See here for a list of available learners.
LearnerAuditorFitter$new(lrn("regr.rpart", minsplit = 10L))
rf = MCBoost$new(auditor_fitter = rf)
mc =$multicalibrate(data, labels) mc
The TreeAuditorFitter
and RidgeAuditorFitter
are two instantiations of this Fitter with pre-defined learners. By providing their character strings the fitter could be automatically constructed.
In some occasions, instead of using a Learner
, we might want to use a fixed set of subgroups. Those can either be defined from the data itself or provided from the outside.
Splitting via the dataset
In order to split the data into groups according to a set of columns, we use a SubpopAuditorFitter
together with a list of subpops
. Those define the group splits to multi-calibrate on. These splits can be either a character
string, referencing a binary variable in the data or a function
that, when evaluated on the data, returns a binary vector.
In order to showcase both options, we add a binary variable to our data
:
:= sample(c(1, 0), nrow(data), replace = TRUE)] data[, Bin
SubpopAuditorFitter$new(list(
rf ="Bin",
function(data) {data[["V1"]] > 0.2},
function(data) {data[["V1"]] > 0.2 | data[["V3"]] < 0.29}
))
MCBoost$new(auditor_fitter = rf)
mc =$multicalibrate(data, labels) mc
And we can again apply it to predict on new data:
$predict_probs(data) mc
Manually defined masks
If we want to add the splitting from the outside, by supplying binary masks for the rows of the data, we can provide manually defined masks. Note, that the masks have to correspond with the number of rows in the dataset.
SubgroupAuditorFitter$new(list(
rf =rep(c(0, 1), 104),
rep(c(1, 1, 1, 0), 52)
))
MCBoost$new(auditor_fitter = rf)
mc =$multicalibrate(data, labels) mc
During prediction, we now have to supply a set of masks for the prediction data.
list(
predict_masks =rep(c(0, 1), 52),
rep(c(1, 1, 1, 0), 26)
)
$predict_probs(data[1:104,], subgroup_masks = predict_masks) mc
When data has missing values or other non-standard columns, we often have to pre-process data in order to be able to fit models. Those preprocessing steps can be embedded into the SubPopFitter
by using a mlr3pipelines Pipeline. The following code shows a brief example:
tsk("penguins")
tsk =# first we convert to a binary task
tsk$data(cols = c("species", "..row_id"))[species %in% c("Adelie", "Gentoo")][["..row_id"]]
row_ids =$filter(row_ids)$droplevels()
tsk tsk
library("mlr3pipelines")
library("mlr3learners")
# Convert task to X,y
tsk$data(cols = tsk$feature_names)
X = tsk$data(cols = tsk$target_names)
y =
# Our inital model is a pipeline that imputes missings and encodes categoricals
as_learner(po("encode") %>>% po("imputehist") %>>%
init_model = lrn("classif.glmnet", predict_type = "prob"))
# And we fit it on a subset of the data in order to simulate a poorly performing model.
$train(tsk$clone()$filter(row_ids[c(1:9, 160:170)]))
init_model$predict(tsk)$score()
init_model
# We define a pipeline that imputes missings and encodes categoricals
as_learner(po("encode") %>>% po("imputehist") %>>% lrn("regr.rpart"))
auditor =
MCBoost$new(auditor_fitter = auditor, init_predictor = init_model)
mc =$multicalibrate(X, y) mc
and we can observe where it improved:
mc
We abuse the Communities & Crime
dataset in order to showcase how mcboost
can be used in a regression setting.
First we download the data and create an mlr3
regression task:
library(data.table)
library(mlr3oml)
OMLData$new(42730)
oml = oml$data
data =
TaskRegr$new("communities_crime", data, target = "ViolentCrimesPerPop") tsk =
Currently, mcboost only allows to work with targets between 0 and 1. Luckily, our target variable’s values are already in that range, but if they were not, we could simply scale them to [0;1] before our analysis.
summary(data$ViolentCrimesPerPop)
We again split our task into train and test. We do this in mlr3
by simply setting some (here 500) row roles to "holdout"
.
$set_row_roles(sample(tsk$row_roles$use, 500), "holdout") tsk
Then we do basic preprocessing, since we do not have any categorical variables, we only impute NA’s using a histogram approach.
library(mlr3pipelines)
po("imputehist")
pipe = pipe$train(list(tsk))[[1]]
prep_task =
$set_col_roles(c("racepctblack", "racePctWhite", "racePctAsian", "racePctHisp", "community"), remove_from = "feature") prep_task
Now we fit our first Learner
: A random forest
.
library(mlr3learners)
lrn("regr.ranger", num.trees = 10L)
l =$train(prep_task) l
A simple way to use the predictions from any Model
in mcboost is to wrap the predict function and provide it as an initial predictor. This can be done from any model / any library. Note, that we have to make sure, that our init_predictor
returns a numeric vector of predictions.
function(data) {
init_predictor =$predict_newdata(data)$response
l }
As mcboost requires the data to be provided in X, y
format (a data.table
or data.frame
of features and a vector of labels), we create those two objects.
prep_task$data(cols = prep_task$feature_names)
data = prep_task$data(cols = prep_task$target_names)[[1]] labels =
MCBoost$new(auditor_fitter = "RidgeAuditorFitter", init_predictor = init_predictor, eta = 0.1)
mc =$multicalibrate(data, labels) mc
We first create the test task by setting the holdout
rows to use
, and then use our preprocessing pipe's
predict function to also impute missing values for the validation data. Then we again extract features X
and target y
.
tsk$clone()
test_task =$row_roles$use = test_task$row_roles$holdout
test_task pipe$predict(list(test_task))[[1]]
test_task = test_task$data(cols = tsk$feature_names)
test_data = test_task$data(cols = tsk$target_names)[[1]] test_labels =
and predict.
mc$predict_probs(test_data) prs =
Now we can compute the MSE of the multi-calibrated model
mean((prs - test_labels)^2)
and compare to the non-calibrated version:
mean((init_predictor(test_data) - test_labels)^2)
But looking at sub-populations we can see that the predictions got more calibrated. Since we cannot show all subpopulations we only show the MSE for the feature racepctblack
.
$se_mcboost = (prs - test_labels)^2
test_data$se_init = (init_predictor(test_data) - test_labels)^2
test_data
mcboost = mean(se_mcboost), initial = mean(se_init), .N), by = .(racepctblack > 0.5)] test_data[, .(