CounterfactualMethodClassif {counterfactuals} | R Documentation |
Base class for Counterfactual Explanation Methods for Classification Tasks
Description
Abstract base class for counterfactual explanation methods for classifcation tasks.
CounterfactualMethodClassif
can only be initialized for classification tasks. Child classes inherit the (public)
$find_counterfactuals()
method, which calls a (private) $run()
method. This $run()
method should be implemented
by the child classes and return the counterfactuals as a data.table
(preferably) or a data.frame
.
Inheritance
Child classes: MOCClassif, WhatIfClassif, NICEClassif
Super class
counterfactuals::CounterfactualMethod
-> CounterfactualMethodClassif
Methods
Public methods
Inherited methods
Method new()
Creates a new CounterfactualMethodClassif
object.
Usage
CounterfactualMethodClassif$new( predictor, lower = NULL, upper = NULL, distance_function = NULL )
Arguments
predictor
(Predictor)
The object (created withiml::Predictor$new()
) holding the machine learning model and the data.lower
(
numeric()
|NULL
)
Vector of minimum values for numeric features. IfNULL
(default), the element for each numeric feature inlower
is taken as its minimum value inpredictor$data$X
. If notNULL
, it should be named with the corresponding feature names.upper
(
numeric()
|NULL
)
Vector of maximum values for numeric features. IfNULL
(default), the element for each numeric feature inupper
is taken as its maximum value inpredictor$data$X
. If notNULL
, it should be named with the corresponding feature names.distance_function
(
function()
|NULL
)
A distance function that may be used by the leaf classes. If specified, the function must have three arguments:x
,y
, anddata
and return adouble
matrix withnrow(x)
rows andnrow(y)
columns.
Method find_counterfactuals()
Runs the counterfactual method and returns the counterfactuals.
It searches for counterfactuals that have a predicted probability in the interval desired_prob
for the
desired_class
.
Usage
CounterfactualMethodClassif$find_counterfactuals( x_interest, desired_class = NULL, desired_prob = c(0.5, 1) )
Arguments
x_interest
(
data.table(1)
|data.frame(1)
)
A single row with the observation of interest.desired_class
(
character(1)
|NULL
)
The desired class. IfNULL
(default) thenpredictor$class
is taken.desired_prob
(
numeric(1)
|numeric(2)
)
The desired predicted probability of thedesired_class
. It can be a numeric scalar or a vector with two numeric values that specify a probability interval. For hard classification tasks this can be set to0
or1
, respectively. A scalar is internally converted to an interval.
Returns
A Counterfactuals object containing the results.
Method clone()
The objects of this class are cloneable with this method.
Usage
CounterfactualMethodClassif$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.