rocelib.tasks package

Submodules

rocelib.tasks.ClassificationTask module

class rocelib.tasks.ClassificationTask.ClassificationTask(model, dataset, mm_models=None)[source]

Bases: Task

A specific task type for classification problems that extends the base Task class.

This class provides methods for training the model and retrieving positive instances from the training data.

model

The model to be trained and used for predictions.

_dataset

The dataset used for training the model.

add_evaluation_metric(metric_name, metric_class)[source]
add_recourse_method(method_name, method_class)[source]
convert_datatype(data, target_type)[source]

Converts a Pandas DataFrame to the specified data type.

@param data: pd.DataFrame - The input DataFrame. @param target_type: str - The target data type: “DataFrame”, “NPArray”, or “TTensor”. @return: Converted data in the specified format.

evaluate(methods=None, evaluations=None, visualisation=False, **kwargs)[source]

Evaluates the generated counterfactual explanations using specified evaluation metrics.

@param methods: List of recourse methods to evaluate. @param evaluations: List of evaluation metrics to apply. @return: Dictionary containing evaluation results per method and metric.

Return type:

Dict[str, Dict[str, Any]]

generate(methods=None, type='DataFrame', **kwargs)[source]

Generates counterfactual explanations for the specified methods and stores the results.

@param methods: List of recourse methods (by name) to use for counterfactual generation. If not provided, then counterfactuals will be generated for all methods @param type: The datatype your instances are in e.g. dataframe, nparray, tensor @return: A dictionary from recourse method to a tuple of (Pandas dataframe holding the counterfactual, time taken to generate the counterfactual)

Return type:

Dict[str, Tuple[DataFrame, float]]

generate_for_model_method(model_name, method, type, **kwargs)[source]
Return type:

Tuple[DataFrame, float]

generate_mm(methods=None, type='DataFrame', **kwargs)[source]

Generates counterfactual explanations for the specified methods for each of the stored models and stores the results.

@param methods: List of recourse methods (by name) to use for counterfactual generation. @return: A nested dictionary from recourse method to model name to a tuple of (Pandas dataframe holding the counterfactual, time taken to generate the counterfactual)

Return type:

Dict[str, Dict[str, Tuple[DataFrame, float]]]

get_random_positive_instance(neg_value, column_name='target')[source]

Retrieves a random positive instance from the training data that does not have the specified negative value.

This method continues to sample from the training data until a positive instance is found whose predicted label is not equal to the negative value.

@param neg_value: The value considered negative in the target variable. @param column_name: The name of the target column used to identify positive instances. @return: A Pandas Series representing a random positive instance.

Return type:

Series

save_evals_as_zip(table_data, headers, csv_filename, graph_filename)[source]

rocelib.tasks.Task module

class rocelib.tasks.Task.Task(model, dataset, mm_models=None)[source]

Bases: ABC

An abstract base class representing a general task that involves training a model on a specific dataset.

_dataset

The dataset used for training the model.

Type:

DatasetLoader

__model

The model to be trained and used for predictions.

Type:

TrainableModel

property CEs
property ces

Property to access the training data.

@return: The training data loaded from DatasetLoader.

property dataset

Property to access the training data.

@return: The training data loaded from DatasetLoader.

evaluate(methods, evaluations)[source]
Return type:

Dict[str, Dict[str, Any]]

generate(methods)[source]
Return type:

Dict[str, Tuple[DataFrame, float]]

generate_mm(methods)[source]
Return type:

Dict[str, Tuple[DataFrame, float]]

get_evaluation_metrics()[source]
Return type:

List[str]

get_random_positive_instance(neg_value, column_name='target')[source]

Abstract method to retrieve a random positive instance from the training data.

@param neg_value: The value considered negative in the target variable. @param column_name: The name of the target column. @return: A Pandas Series representing a random positive instance.

Return type:

Series

get_recourse_methods()[source]
Return type:

List[str]

property mm_CEs
property mm_models

Property to access the model.

@return: The model instance that extends TrainableModel

property model

Property to access the model.

@return: The model instance that extends TrainableModel

Module contents