SHAP Load

Loads a saved SHAP explainer object from a binary file using Joblib.

SHAP Load

Processing

This brick loads a previously saved SHAP explainer object from a specific file path on your system. It utilizes the Joblib library to "deserialize" (open and reconstruction) binary data, allowing you to reuse trained explainers for interpreting machine learning models without needing to recalculate or retrain them.

Inputs

file path
The full system path to the binary file containing the saved SHAP explainer. This file should have been previously generated and saved using Joblib.

Inputs Types

Input Types
file path str, FilePath

You can check the list of supported types here: Available Type Hints.

Outputs

explainer
The loaded SHAP explainer object. This complex object contains the internal structure and data necessary to interpret model predictions and can be passed to subsequent analysis or visualization bricks.

Outputs Types

Output Types
explainer Any

You can check the list of supported types here: Available Type Hints.

Options

The SHAP Load brick contains some changeable options:

Verbose
Controls the amount of information logged during the loading process.
import logging
import pathlib
import joblib
from coded_flows.types import Union, Str, Any, FilePath
from coded_flows.utils import CodedFlowsLogger

logger = CodedFlowsLogger(name="SHAP Load", level=logging.INFO)


def load_explainer(file_path: Union[Str, FilePath], options=None) -> Any:
    options = options or {}
    verbose = options.get("verbose", True)
    explainer = None
    try:
        verbose and logger.info(f"Attempting to load SHAP explainer from: {file_path}")
        path_obj = pathlib.Path(file_path)
        if not path_obj.exists():
            verbose and logger.error(f"File not found at path: {path_obj}")
            raise FileNotFoundError(f"The specified file does not exist: {path_obj}")
        if not path_obj.is_file():
            verbose and logger.error(f"Path is not a file: {path_obj}")
            raise ValueError(f"The specified path is not a valid file: {path_obj}")
        verbose and logger.info("File validated. Initiating load process.")
        explainer = joblib.load(path_obj)
        verbose and logger.info(
            f"Object loaded successfully. Type: {type(explainer).__name__}"
        )
    except Exception as e:
        verbose and logger.error(f"Failed to load object: {e}")
        raise e
    return explainer

Brick Info

version v0.1.4
python 3.11, 3.12, 3.13
requirements
  • shap>=0.47.0
  • joblib
  • numba>=0.56.0
  • shap