Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions shapash/backend/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, model: Any, preprocessing: Any | None = None) -> None:
----------
model : Any
Model used.
preprocessing : Any, optional
preprocessing : category_encoders, ColumnTransformer, list or dict, optional
The processing apply to the original data.
"""
self.model = model
Expand Down Expand Up @@ -210,7 +210,7 @@ def _needs_preprocessing(result_cols: list[Any], x: pd.DataFrame, preprocessing:
Column names present in the contributions.
x : pd.DataFrame
The encoded input data.
preprocessing : Any or None
preprocessing : category_encoders, ColumnTransformer, list or dict or None
The preprocessing applied to the original data, if any.

Returns
Expand Down
38 changes: 34 additions & 4 deletions shapash/backend/lime_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def _with_feature_names(
values : np.ndarray or pd.DataFrame
Input values to pass to the prediction function.
predict_fn : Callable[[Any], Any]
Prediction function to call.
Model batch-prediction function: ``predict_proba`` for classification or
``predict`` for regression. It must accept multiple rows at once and return
one prediction row per input row. DataFrame inputs are passed through;
NumPy inputs are first wrapped in a DataFrame with ``feature_names`` as
columns, then retried as a NumPy array if that call raises ``TypeError`` or
``ValueError``.
feature_names : list[str]
Explicit column names — no implicit closure over outer scope.

Expand Down Expand Up @@ -90,7 +95,7 @@ def __init__(
----------
model : Any
Model to explain.
preprocessing : Any or None, optional
preprocessing : category_encoders, ColumnTransformer, list or dict or None, optional
Preprocessing applied to the model inputs.
data : pd.DataFrame or None, optional
Data used to initialize the LIME explainer. Defaults to the data
Expand Down Expand Up @@ -195,10 +200,26 @@ def _explain_multiclass(
it returns explanations for every class in a single call, so there is
no need to loop over classes in the outer dimension.

Parameters
----------
x : pd.DataFrame
Input observations to explain. Each row is explained once with
:meth:`lime.lime_tabular.LimeTabularExplainer.explain_instance`.
feature_names : list[str]
Ordered list of feature names used to build each per-class
contributions dataframe and preserve the original column order.
predict_fn : Callable
Batch prediction callable passed to LIME. It must accept multiple
perturbed rows at once and return class probabilities, with one output
row per input row (shape ``(n_samples, n_classes)``).
num_classes : int
Number of target classes to explain.

Comment thread
guillaume-vignal marked this conversation as resolved.
Resolved
Returns
-------
tuple[pd.DataFrame, np.ndarray]
- One DataFrame of shape (n_samples, n_features) per class.
tuple[list[pd.DataFrame], np.ndarray]
- One DataFrame of shape (n_samples, n_features) per class,
ordered by class index.
- Local intercepts of shape (n_samples, n_classes).
"""
# One explain_instance call per sample — O(n_samples)
Expand Down Expand Up @@ -232,6 +253,15 @@ def _explain_binary_or_regression(
"""
Compute LIME contributions for binary classification or regression.

Parameters
----------
predict_fn : Callable
Batch prediction callable passed to LIME. It must accept multiple
perturbed rows at once and return one output row per input row. For
classification, it returns class probabilities with shape
``(n_samples, n_classes)``; for regression, it returns one prediction
per row with shape ``(n_samples,)``.

Returns
-------
tuple[pd.DataFrame, np.ndarray]
Expand Down
14 changes: 10 additions & 4 deletions shapash/backend/shap_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Callable
from typing import Any

import numpy as np
Expand All @@ -19,7 +20,7 @@ def __init__(
self,
model: Any,
preprocessing: Any | None = None,
masker: Any | None = None,
masker: shap.maskers.Masker | pd.DataFrame | np.ndarray | Callable[..., Any] | None = None,
explainer_args: dict[str, Any] | None = None,
explainer_compute_args: dict[str, Any] | None = None,
) -> None:
Expand All @@ -29,10 +30,15 @@ def __init__(
----------
model : Any
Model to explain.
preprocessing : Any or None, optional
preprocessing : category_encoders, ColumnTransformer, list or dict or None, optional
Preprocessing applied to the model inputs.
masker : Any or None, optional
Masker passed to the SHAP explainer.
masker : shap.maskers.Masker, pandas.DataFrame, numpy.ndarray, callable, or None, optional
SHAP masking strategy, when supported by the selected explainer. A DataFrame
or array is used as background data. A callable must accept a binary feature
mask via ``mask=...`` and one sample in the model's input format, and return
an iterable of masked samples. If None, the selected explainer applies its
default behavior. Some specialized explainers, such as TreeExplainer, do
not accept a masker.
explainer_args : dict[str, Any] or None, optional
Keyword arguments used to construct the SHAP explainer.
explainer_compute_args : dict[str, Any] or None, optional
Expand Down
2 changes: 1 addition & 1 deletion shapash/decomposition/contributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def inverse_transform_contributions(
----------
contributions : pandas.DataFrame
Contributions values.
preprocessing : Any or None, optional
preprocessing : category_encoders, ColumnTransformer, list or dict or None, optional
The processing apply to the original data.
agg_columns : Literal["sum", "first"], optional
Type of aggregation performed. For Shap we want so sum contributions of one hot encoded variables.
Expand Down
2 changes: 1 addition & 1 deletion shapash/explainer/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def compile(
where each contrib_i is a pandas DataFrame.
x : pd.DataFrame or None, optional
Dataset on which to compute consistency metrics, by default None
preprocessing : Any or None, optional
preprocessing : category_encoders, ColumnTransformer, list or dict or None, optional
--> Differents types of preprocessing are available:

- A single category_encoders (OrdinalEncoder/OnehotEncoder/BaseNEncoder/BinaryEncoder/TargetEncoder)
Expand Down
4 changes: 3 additions & 1 deletion shapash/explainer/smart_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,9 @@ def interactions_plot(
# add break line to X label if necessary
max_len_by_row = max([round(50 / self._explainer.features_desc[feature_values1.columns.values[0]]), 8])
args = (max_len_by_row, 120)
feature_values_str = feature_values1.iloc[:, 0].apply(add_line_break, args=args)
feature_values_str = feature_values1.iloc[:, 0].apply(
lambda value: add_line_break(value, *args) if isinstance(value, str) else value
)
feature_values1 = pd.DataFrame({feature_values1.columns[0]: feature_values_str})

# selecting the best plot : Scatter, Violin?
Expand Down
4 changes: 2 additions & 2 deletions shapash/explainer/smart_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class SmartPredictor:
Dictionary of postprocessing modifications to apply in x_init dataframe.
_case: string
String that informs if the model used is for classification or regression problem.
_classes: list, None
_classes: list[int | float | str] | None
List of labels if the model used is for classification problem, None otherwise.
mask_params: dict (optional)
Dictionary that specify how to summarize the explainability.
Expand Down Expand Up @@ -262,7 +262,7 @@ def __init__(
check_consistency_model_label(self.columns_dict, self.label_dict)
self._drop_option = check_preprocessing_options(columns_dict, features_dict, preprocessing, list_preprocessing)

def check_model(self) -> tuple[Literal["classification", "regression"], list[Any] | None]:
def check_model(self) -> tuple[Literal["classification", "regression"], list[int | float | str] | None]:
"""
Check if model has a predict_proba method is a one column dataframe of integer or float
and if y_pred index matches x_init index
Expand Down
2 changes: 1 addition & 1 deletion shapash/explainer/smart_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def inverse_transform_contributions(
----------
contributions : pandas.DataFrame
Local contributions of a model on a prediction set.
preprocessing : Any or None, optional
preprocessing : category_encoders, ColumnTransformer, list or dict or None, optional
Single step of preprocessing, typically a category encoder.
agg_columns : Literal["sum", "first"], optional
Type of aggregation performed. For SHAP, contributions of one-hot encoded variables are summed.
Expand Down
4 changes: 2 additions & 2 deletions shapash/manipulation/select_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def keep_right_contributions(
y_pred: pd.DataFrame,
contributions: pd.DataFrame | list[pd.DataFrame],
_case: Literal["classification", "regression"],
_classes: list[Any] | None,
_classes: list[int | float | str] | None,
label_dict: dict[Any, Any] | None,
proba_values: pd.DataFrame | None = None,
) -> tuple[pd.DataFrame, pd.DataFrame]:
Expand All @@ -49,7 +49,7 @@ def keep_right_contributions(
and a list of per-class DataFrames for classification.
_case : {"classification", "regression"}
String that informs if the model used is for classification or regression problem.
_classes : list[Any] or None
_classes : list[int or float or str] or None
Class labels for classification, or None for regression.
label_dict : dict[Any, Any] or None
Optional mapping from class labels to domain names.
Expand Down
8 changes: 6 additions & 2 deletions shapash/plots/plot_bar_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,16 @@ def _suffix_duplicate_labels(labels: list[str]) -> list[str]:
feat_groups_values = x_init[features_groups[group_name]].loc[index_value[0]]
hoverlabel = "<br />".join(
[
f"<b>{add_line_break(features_dict.get(f_name, f_name), 40, maxlen=120)} :</b>{add_line_break(f_value, 40, maxlen=160)}"
f"<b>{add_line_break(str(features_dict.get(f_name, f_name)), 40, maxlen=120)} :</b>"
f"{add_line_break(str(f_value), 40, maxlen=160)}"
for f_name, f_value in feat_groups_values.to_dict().items()
]
)
else:
hoverlabel = f"<b>{add_line_break(feat_name, 40, maxlen=120)} :</b><br />{add_line_break(x_val_el, 40, maxlen=160)}"
hoverlabel = (
f"<b>{add_line_break(str(feat_name), 40, maxlen=120)} :</b><br />"
f"{add_line_break(str(x_val_el), 40, maxlen=160)}"
)
if len(contrib) <= yaxis_max_label and (
features_groups is None
# We don't want to display label values for t-sne projected values of groups of features.
Expand Down
8 changes: 6 additions & 2 deletions shapash/plots/plot_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ def plot_scatter(

# add break line to X label if necessary
args = (max_len_by_row, 120)
feature_values_str = feature_values.iloc[:, 0].apply(add_line_break, args=args)
feature_values_str = feature_values.iloc[:, 0].apply(
lambda value: add_line_break(value, *args) if isinstance(value, str) else value
)
feature_values = pd.DataFrame({column_name: feature_values_str})

if pred is not None:
Expand Down Expand Up @@ -670,7 +672,9 @@ def plot_violin(

# add break line to X label if necessary
args = (max_len_by_row, 120)
feature_values_str = feature_values.iloc[:, 0].apply(add_line_break, args=args)
feature_values_str = feature_values.iloc[:, 0].apply(
lambda value: add_line_break(value, *args) if isinstance(value, str) else value
)
feature_values = pd.DataFrame({column_name: feature_values_str})

contributions = contributions.loc[feature_values.index]
Expand Down
6 changes: 3 additions & 3 deletions shapash/plots/plot_line_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ def plot_line_comparison(
features.append("<b>" + str(feat) + "</b>")
pred_x_val = x_val[dict_features[feat]]
x_hover.append(
f"Id: <b>{add_line_break(id_i, 40, 160)}</b>"
+ f"<br /><b>{add_line_break(feat, 40, 160)}</b> <br />"
f"Id: <b>{add_line_break(str(id_i), 40, 160)}</b>"
+ f"<br /><b>{add_line_break(str(feat), 40, 160)}</b> <br />"
+ f"Contribution: {contrib[i]:.4f} <br />Value: "
+ str(add_line_break(pred_x_val, 40, 160))
+ add_line_break(str(pred_x_val), 40, 160)
)

lines.append(
Expand Down
27 changes: 18 additions & 9 deletions shapash/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def check_preprocessing(preprocessing: Any | None = None) -> tuple[bool, bool] |
return None


def check_model(model: Any) -> tuple[Literal["regression", "classification"], list[Any] | None]:
def check_model(model: Any) -> tuple[Literal["regression", "classification"], list[int | float | str] | None]:
"""Determine whether a model supports classification or regression.

Parameters
Expand All @@ -48,10 +48,10 @@ def check_model(model: Any) -> tuple[Literal["regression", "classification"], li

Returns
-------
tuple[str, list[Any] or None]
tuple[str, list[int | float | str] or None]
The model type ('regression' or 'classification') and its classes, if it is a classifier.
"""
_classes = None
_classes: list[int | float | str] | None = None
if hasattr(model, "predict"):
if hasattr(model, "predict_proba") or any(hasattr(model, attrib) for attrib in ["classes_", "_classes"]):
if hasattr(model, "_classes"):
Expand All @@ -60,6 +60,15 @@ def check_model(model: Any) -> tuple[Literal["regression", "classification"], li
_classes = model.classes_
if isinstance(_classes, np.ndarray):
_classes = _classes.tolist()
if _classes is not None:
if not isinstance(_classes, list):
raise ValueError("Model classes must be provided as a list or NumPy array")
_classes = [
class_label.item() if isinstance(class_label, np.generic) else class_label
for class_label in _classes
]
if not all(isinstance(class_label, int | float | str) for class_label in _classes):
raise ValueError("Model class labels must be integers, floats, or strings")
if hasattr(model, "predict_proba") and _classes == []:
_classes = [0, 1] # catboost binary
if hasattr(model, "predict_proba") and _classes is None:
Expand All @@ -75,7 +84,7 @@ def check_model(model: Any) -> tuple[Literal["regression", "classification"], li
def check_label_dict(
label_dict: dict[Any, Any] | None,
case: Literal["regression", "classification"],
classes: list[Any] | None = None,
classes: list[int | float | str] | None = None,
) -> None:
"""
Check if label_dict and model _classes match
Expand All @@ -86,11 +95,11 @@ def check_label_dict(
Dictionary mapping integer labels to domain names (classification - target values).
case: str
String that informs if the model used is for classification or regression problem.
classes: list[Any] or None, optional
classes: list[int or float or str] or None, optional
List of labels if the model used is for classification problem, None otherwise.
"""
if label_dict is not None and case == "classification":
if set(cast(list[Any], classes)) != set(list(label_dict.keys())):
if set(cast(list[int | float | str], classes)) != set(list(label_dict.keys())):
raise ValueError(
"label_dict and don't match: \n"
+ f"label_dict keys: {str(list(label_dict.keys()))}\n"
Expand Down Expand Up @@ -171,7 +180,7 @@ def check_y(

def check_contribution_object(
case: Literal["regression", "classification"],
classes: list[Any] | None,
classes: list[int | float | str] | None,
contributions: np.ndarray | pd.DataFrame | list[Any],
) -> None:
"""Validate the type and number of contribution objects for the model case.
Expand All @@ -180,7 +189,7 @@ def check_contribution_object(
----------
case: str
String that informs if the model used is for classification or regression problem.
classes: list[Any] or None
classes: list[int or float or str] or None
List of labels if the model used is for classification problem, None otherwise.
contributions: pandas.DataFrame, numpy.ndarray or list
Contributions for the model; classification requires one object per class.
Expand All @@ -195,7 +204,7 @@ def check_contribution_object(
)
elif case == "classification":
if isinstance(contributions, list):
if len(contributions) != len(cast(list[Any], classes)):
if len(contributions) != len(cast(list[int | float | str], classes)):
raise ValueError(
"""
Length of list of contributions parameter is not equal
Expand Down
4 changes: 2 additions & 2 deletions shapash/utils/explanation_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _df_to_array(instances: pd.DataFrame | pd.Series | np.ndarray) -> np.ndarray
return instances


def _compute_distance(x1: np.ndarray, x2: np.ndarray, mean_vector: np.ndarray, epsilon: float = 0.0000001) -> float:
def _compute_distance(x1: np.ndarray, x2: np.ndarray, mean_vector: np.ndarray, epsilon: float = 1e-7) -> float:
"""
Compute distances between data points by using L1 on normalized data : sum(abs(x1-x2)/(mean_vector+epsilon))

Expand All @@ -40,7 +40,7 @@ def _compute_distance(x1: np.ndarray, x2: np.ndarray, mean_vector: np.ndarray, e
mean_vector : numpy.ndarray
Each value of this vector is the std.dev for each feature in dataset
epsilon : float, optional
Small value added to the standard deviation to avoid division by zero, by default 0.0000001
Small value added to the standard deviation to avoid division by zero, by default 1e-7

Returns
-------
Expand Down
6 changes: 3 additions & 3 deletions shapash/utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def extract_features_model(model: Any, model_attribute: list[str]) -> Any:
return extract_features_model(getattr(model, model_attribute[0]), model_attribute[1:])


def predict_proba(model: Any, x_encoded: pd.DataFrame, classes: list[Any]) -> pd.DataFrame:
def predict_proba(model: Any, x_encoded: pd.DataFrame, classes: list[int | float | str]) -> pd.DataFrame:
"""
Compute class probabilities for each row in ``x_encoded``.

Expand Down Expand Up @@ -104,7 +104,7 @@ def predict_error(
y_pred: pd.DataFrame | None,
model_type: Literal["regression", "classification"],
proba_values: pd.DataFrame | None = None,
classes: list[Any] | None = None,
classes: list[int | float | str] | None = None,
) -> pd.DataFrame | None:
"""
Compute prediction errors for regression or classification.
Expand Down Expand Up @@ -136,7 +136,7 @@ def predict_error(
proba_values : pandas.DataFrame or None, optional
DataFrame of class probabilities returned by model.predict_proba().
Each column corresponds to a class, in the same order as in `classes`.
classes : list[Any] or None, optional
classes : list[int or float or str] or None, optional
Ordered list of class label codes (`model.classes_`), used to map the
true label to the correct probability column when probabilities are supplied.

Expand Down
Loading
Loading