Description Public fields Methods Examples
Fairness testing of an ML model
Fairness testing of an ML model
predictor
(Predictor)
The object (created with iml::Predictor$new()
) holding the machine learning model and the data.
df
(data_frame
)
The dataset or dataframe for the prediction
sensitive_attribute
(string
)
The name of the sensitive attribute.
n_generations
(integerish(1)
)
The number of generations. Default is 175L
.
cfs
(data_frame
)
A datframe containing counterfactuals (plausible)
org_cfs
(data_frame
)
A dataframe containing all counterfactuals
pred_diff
(data_frame
)
A dataframe containing the prediction differences
new()
Create a new FairnessTest object.
FairnessTest$new( predictor = NULL, df = NULL, sensitive_attribute = NULL, n_generations = 175L )
predictor
(Predictor)
The object (created with iml::Predictor$new()
) holding the machine learning model and the data.
df
(data_frame
)
The dataset or dataframe
sensitive_attribute
(string
)
The name of the protected attribute.
n_generations
(integerish(1)
)
The number of generations. Default is 175L
.
generate_counterfactuals()
Creates a dataframe with the plausible counterfactuals
FairnessTest$generate_counterfactuals( x_interest, desired_level, desired_prob, fixed_features = NULL )
x_interest
(data.table(1)
| data.frame(1)
)
A single row with the observation of interest.
desired_level
(character(1)
| NULL
)
The desired level of the protected attribute.
If NULL
(default) then predictor_protected$class
is taken.
desired_prob
(numeric(1)
| numeric(2)
)
The desired predicted probability of the desired_level
. It can be a numeric scalar or a vector with two
numeric values that specify a probability range.
For hard classification tasks this can be set to 0
or 1
, respectively.
fixed_features
(character()
| NULL
)
Names of features that are not allowed to change. NULL
(default) allows to change all features.
A dataframe with the plausible counterfactuals
get_prediction_difference()
Creates a dataframe with the differences of predictions
FairnessTest$get_prediction_difference(x_interest)
x_interest
(data.table(1)
| data.frame(1)
)
A single row with the observation of interest.
A dataframe with the differences of predictions of the original instance and the counterfactuals
plot_tSNE()
Creates a plot of the distribution
FairnessTest$plot_tSNE(x_interest, factor_variable = NULL)
x_interest
(data.table(1)
| data.frame(1)
)
A single row with the observation of interest.
factor_variable
(string
)
name of another factor variable that we want to see in the plot
get_prediction()
prints the prediction of the plausible counterfactuals
FairnessTest$get_prediction()
prediction_percentages()
calculates the percentage for the predictions
FairnessTest$prediction_percentages(x_interest)
x_interest
(data.table(1)
| data.frame(1)
)
A single row with the observation of interest.
the percentage for the predictions of generated counterfactuals
get_mpd()
returns mean of the prediction differences
FairnessTest$get_mpd()
mean of the prediction diferences
get_cfactuals_count()
Gives the total count of counterfactuals
FairnessTest$get_cfactuals_count()
clone()
The objects of this class are cloneable with this method.
FairnessTest$clone(deep = FALSE)
deep
Whether to make a deep clone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | if (require("randomForest")) {
# data pre-processing
compas <- fairml::compas
compas <- compas %>% drop_na()
compas <- compas %>% distinct()
# Train a model
set.seed(142)
rf = randomForest(two_year_recid ~ ., data = compas[-17L, ])
# Create a predictor object
predictor = iml::Predictor$new(rf, type = "prob")
# Create a FairnessTest object
fairness_obj = FairnessTest$new(predictor, df = compas, sensitive_attribute = "race", n_generations = 175)
# Generate plausible counterfactuals
cfatuals = fairness_obj$generate_counterfactuals(x_interest, desired_level = "Caucasian", desired_prob = c(0.5,1))
# Find differences of the prediction of counterfactuals and original instance
pred_diff = fairness_obj$get_prediction_difference(x_interest)
# Print the results
print(pred_diff)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.