lore_sa.bbox.AbstractBBox
- class lore_sa.bbox.AbstractBBox(classifier)[source]
Abstract base class for wrapping black box models.
This class provides a unified interface for different types of black box models (e.g., scikit-learn, Keras, PyTorch) to be explained by LORE. Any black box model must be wrapped in a subclass of AbstractBBox that implements the predict() and predict_proba() methods.
The wrapper allows LORE to interact with any machine learning model in a consistent way, regardless of the underlying framework or implementation.
- classifier
The underlying black box model
Example
To create a wrapper for a custom model:
>>> from lore_sa.bbox import AbstractBBox >>> >>> class MyCustomBBox(AbstractBBox): ... def __init__(self, model): ... self.classifier = model ... ... def predict(self, X): ... return self.classifier.predict(X) ... ... def predict_proba(self, X): ... return self.classifier.predict_proba(X)
See also
sklearn_classifier_bbox.sklearnBBox: Pre-built wrapper for scikit-learn models keras_classifier_wrapper: Pre-built wrapper for Keras models
- __init__(classifier)[source]
Initialize the black box wrapper.
- Parameters:
classifier – The machine learning model to wrap. The model should have methods compatible with the predict() and predict_proba() interface.
Methods
__init__(classifier)Initialize the black box wrapper.
model()Return the underlying black box model.
predict(sample_matrix)Predict class labels for samples (sklearn-like interface).
predict_proba(sample_matrix)Predict class probabilities for samples (sklearn-like interface).
- model()[source]
Return the underlying black box model.
- Returns:
The wrapped classifier/model object.
- abstract predict(sample_matrix: list)[source]
Predict class labels for samples (sklearn-like interface).
This method wraps the underlying model’s predict method to provide a consistent interface for LORE to get class predictions.
- Parameters:
sample_matrix (array-like) – Array of shape (n_samples, n_features) containing the samples to predict. Can be a list, numpy array, or sparse matrix depending on the model’s requirements.
- Returns:
- Array of shape (n_samples,) containing the predicted
class labels for each sample.
- Return type:
np.ndarray
Example
>>> predictions = bbox.predict([[1, 2, 3], [4, 5, 6]]) >>> print(predictions) # e.g., array([0, 1])
- abstract predict_proba(sample_matrix: list)[source]
Predict class probabilities for samples (sklearn-like interface).
This method wraps the underlying model’s predict_proba method to provide probability estimates for each class for the given samples.
- Parameters:
sample_matrix (array-like) – Array of shape (n_samples, n_features) containing the samples to predict. Can be a list, numpy array, or sparse matrix depending on the model’s requirements.
- Returns:
- Array of shape (n_samples, n_classes) containing the
class probabilities for each sample. Each row sums to 1.0.
- Return type:
np.ndarray
Example
>>> probas = bbox.predict_proba([[1, 2, 3], [4, 5, 6]]) >>> print(probas) # e.g., array([[0.8, 0.2], [0.3, 0.7]])
Note
For multi-class classification, the array has shape (n_samples, n_classes). For binary classification, it typically has shape (n_samples, 2).