LOCO
- class ordinal_xai.interpretation.loco.LOCO(model, X, y=None, test_size=0.2, random_state=None, use_train_test_split=True)[source]
Bases:
BaseInterpretationLeave-One-Covariate-Out (LOCO) interpretation method for feature importance.
This class implements the LOCO method for analyzing feature importance in ordinal regression models. It measures the impact of each feature by evaluating how model performance changes when the feature is removed from the dataset.
- Parameters:
model (object) – The trained ordinal regression model. Must implement fit and predict methods.
X (pd.DataFrame) – Dataset used for interpretation. Should contain the same features used during model training.
y (pd.Series, optional) – Target labels. Required for evaluation but optional for initialization.
test_size (float, default=0.2) – Proportion of the dataset to include in the test split when using train-test split evaluation.
random_state (int, optional) – Random state for reproducibility of train-test splits.
use_train_test_split (bool, default=True) – Whether to use train-test split for evaluation. If False, the entire dataset is used for both training and evaluation.
- available_metrics
Dictionary of available evaluation metrics and their corresponding functions.
- Type:
dict
- test_size
Proportion of data used for testing.
- Type:
float
- random_state
Random state for reproducibility.
- Type:
int
- use_train_test_split
Whether to use train-test split.
- Type:
bool
- X_train, X_test
Training and test datasets when using train-test split.
- Type:
pd.DataFrame
- y_train, y_test
Training and test labels when using train-test split.
- Type:
pd.Series
- original_predictions
Predictions from the original model.
- Type:
array-like
- original_proba_predictions
Probability predictions from the original model if available.
- Type:
array-like, optional
- original_results
Evaluation results from the original model.
- Type:
dict
- __init__(model, X, y=None, test_size=0.2, random_state=None, use_train_test_split=True)[source]
Initialize the LOCO interpretation method.
- Parameters:
model (object) – The trained ordinal regression model
X (pd.DataFrame) – Dataset used for interpretation
y (pd.Series, optional) – Target labels
test_size (float, default=0.2) – Proportion of the dataset to include in the test split
random_state (int, optional) – Random state for reproducibility
use_train_test_split (bool, default=True) – Whether to use train-test split for evaluation
- _perform_train_test_split()[source]
Perform train-test split on the data and evaluate the original model.
This method: 1. Splits the data into training and test sets 2. Creates a copy of the model for training 3. Fits the model on the training data 4. Makes predictions on the test set 5. Evaluates the model using all available metrics
- Raises:
ValueError – If the model class is not suitable for refitting
- explain(observation_idx=None, feature_subset=None, plot=False, metrics=None, title=True)[source]
Generate LOCO feature importance scores.
This method computes feature importance by measuring how model performance changes when each feature is removed. It can operate in two modes: 1. Global mode (default): Evaluates feature importance across the entire dataset 2. Local mode (when observation_idx is provided): Evaluates feature importance for a specific instance
- Parameters:
observation_idx (int, optional) – Index of specific instance to analyze (local explanation)
feature_subset (list, optional) – List of feature names or indices to analyze
plot (bool, default=False) – Whether to create visualizations
metrics (list, optional) – List of metrics to use for feature importance calculation
title (bool, default=True) – Whether to add a suptitle to the visualization
- Returns:
Dictionary containing feature importance scores for each metric
- Return type:
dict
- _plot_feature_importance(results, metrics=None, title=True)[source]
Create visualizations for feature importance.
This method creates bar plots showing feature importance scores for each metric. The plots are arranged in a grid, with one subplot per metric.
- Parameters:
results (dict) – Dictionary containing all results from the LOCO analysis
metrics (list, optional) – List of metrics to plot (defaults to all in results)
title (bool, default=True) – Whether to add a suptitle to the visualization
Notes
Features are sorted by importance score
Error metrics are shown in red with “Increase” in the title
Other metrics are shown in green with “Drop” in the title
Feature names are rotated 90 degrees for better readability
Score values are displayed in the middle of each bar
- _abc_impl = <_abc._abc_data object>