CounterfactualMethodClassif: Base class for Counterfactual Explanation Methods for...

CounterfactualMethodClassifR 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 with iml::Predictor$new()) holding the machine learning model and the data.

lower

(numeric() | NULL)
Vector of minimum values for numeric features. If NULL (default), the element for each numeric feature in lower is taken as its minimum value in predictor$data$X. If not NULL, it should be named with the corresponding feature names.

upper

(numeric() | NULL)
Vector of maximum values for numeric features. If NULL (default), the element for each numeric feature in upper is taken as its maximum value in predictor$data$X. If not NULL, 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, and data and return a double matrix with nrow(x) rows and nrow(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. If NULL (default) then predictor$class is taken.

desired_prob

(numeric(1) | numeric(2))
The desired predicted probability of the desired_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 to 0 or 1, 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.


counterfactuals documentation built on March 31, 2023, 7:17 p.m.