arthurai.explainability.arthur_explainer.ArthurExplainer#

class arthurai.explainability.arthur_explainer.ArthurExplainer(model_type, model_input_type, predicted_attributes, predict_func, data, label_mapping, categorical_features=None, enable_shap=True, shap_training_samples=20, enable_lime=True, text_delimiter='\\\\W+', load_image_func=None, predictions_df=None)#

Bases: object

Class for explaining inferences with a variety of libs

Methods

build_image_explainers

rtype

None

build_nlp_explainers

rtype

None

build_tabular_explainers

rtype

None

decode_labels

Decodes into strings

encode_labels

Encodes labels into integers

explain_image

rtype

List[Dict[str, Any]]

explain_image_lime

rtype

List[Dict[str, Any]]

explain_nlp

rtype

List[List[List[List[float]]]]

explain_nlp_lime

rtype

List[List[List[List[float]]]]

explain_tabular

rtype

List[List[List[float]]]

explain_tabular_lime

Calculate lime values.

explain_tabular_shap

Calculate SHAP values.

format_lime_image_results

Format LIME results for images

format_lime_results

LIME's .as_map() method returns scores looking like the following:

format_lime_results_for_one_class

This method unpacks the results for a single classes's explanation:

format_lime_text_results

Format LIME results for Text models

predict_tabular

Takes in customer provided data and returns the prediction values as numpy array (both regression and classification)

predict_wrapper

Wraps user supplied predict function.

shap_expected_values

Returns expected values from the shap explainer for this model input type

Attributes

LIME

SHAP

num_predicted_attributes

rtype

int

decode_labels(feature_vector)#

Decodes into strings

Return type

Iterable

encode_labels(feature_vector)#

Encodes labels into integers

Return type

Iterable

explain_tabular_lime(raw_feature_vectors, **kwargs)#

Calculate lime values. For one or more inferences.

Parameters
  • raw_feature_vectors (List) – An iterable of one or more feature vectors to explain.

  • kwargs – Additional args to pass to LIME.

Return type

List[List[List[float]]]

Returns

A List of explanations for each feature vector. A single explanation is a list of lists that contain the explanation for each model output by input feature. In this example, the model has 4 features and 3 output classes:

[
    [
        [-0.011, 0.009, -0.128, -0.161],
        [-0.009, 0.003, -0.091, -0.065],
        [0.02, -0.013, 0.220, 0.226]
    ]
]

explain_tabular_shap(raw_feature_vectors, **kwargs)#

Calculate SHAP values. For one or more inferences.

Parameters
  • raw_feature_vectors (List) – An iterable of one or more feature vectors to explain.

  • kwargs – Additional args to pass to SHAP.

Return type

List[List[List[float]]]

Returns

A List of explanations for each feature vector. A single explanation is a list of lists that contain the explanation for each model output by input feature. In this example, the model has 4 features and 3 output classes.

[
    [-0.011, 0.009, -0.128, -0.161],
    [-0.009, 0.003, -0.091, -0.065],
    [0.02, -0.013, 0.220, 0.226]
]

static format_lime_image_results(results)#

Format LIME results for images

Return type

List[Dict[str, Any]]

Returns

formatted output in the following format:

{
    "lime_segment_mask": [
        [1, 1, 1, 3, 3, 1, 0],
        [1, 1, 1, 3, 3, 1, 0],
        [1, 1, 1, 3, 3, 1, 0],
        [2, 2, 2, 3, 3, 1, 0]
    ],
    "lime_region_mapping": {
        "0": [[0, 0.23], [1, 0.3], [2, 0.001], [3, -0.5]]
    }
}

format_lime_results(results)#

LIME’s .as_map() method returns scores looking like the following:

{
   0:[
      (3, -0.1614709564346036),
      (2, -0.12871035938256958),
      (0, -0.011225126011010974),
      (1, 0.009948039509525414)
   ],
   1:[
      (2, -0.0914379318506283),
      (3, -0.06513690080871648),
      (0, -0.009164069675758498),
      (1, 0.003622751782692439)
   ],
   2:[
      (3, 0.2266078572433201),
      (2, 0.22014829123319787),
      (0, 0.02038919568676947),
      (1, -0.013570791292217853)
   ]
}

Where the keys are the class labels, and the values are lists of feature index, score tuples for that class.

This method unpacks the result to a list of lists for EACH explanation:

[
    [-0.011, 0.009, -0.128, -0.161],
    [-0.009, 0.003, -0.091, -0.065],
    [0.02, -0.013, 0.220, 0.226]
]

For regression, this will return a list of lists for one class per inference:

[
    [-0.011, 0.009, -0.128, -0.161]
]
Return type

List[List[List[float]]]

static format_lime_results_for_one_class(feature_index_score_pairs)#

This method unpacks the results for a single classes’s explanation:

[
    (3, -0.1614709564346036),
    (2, -0.12871035938256958),
    (0, -0.011225126011010974),
    (1, 0.009948039509525414)
]
Return type

List[float]

Returns

unpacked lime results for a single class

[-0.011, 0.009, -0.128, -0.161]

format_lime_text_results(results)#

Format LIME results for Text models

LIME’s .as_map() method returns scores looking like the following:

{0: [('I', -0.1614709564346036), ('a', -0.12871035938256958), ('am', -0.011225126011010974), ('developer', 0.009948039509525414)],
 1: [('am', -0.0914379318506283), ('a', -0.06513690080871648), ('I', -0.009164069675758498), ('developer', 0.003622751782692439)],
 2: [('developer', 0.2266078572433201), ('am', 0.22014829123319787), ('a', 0.02038919568676947), ('I', -0.013570791292217853)]}

Where the keys are the class labels, and the values are lists of word, score tuples for that class.

This method unpacks the result to a list of lists for EACH explanation:

[
    [['I', -0.011], ['am', 0.009], ['a', -0.128], ['developer', -0.161]],
    [['I', -0.02], ['am', 0.007], ['a', -0.8], ['developer', 0.11]],
    [['I', -0.03], ['am', 0.89], ['a', -0.28], ['developer', -0.16]]
]

For regression, this will return a list of lists for one class per inference:

[
    [['I', -0.011], ['am', 0.009], ['a', -0.128], ['developer', -0.161]]
]
Return type

List[List[List[List[float]]]]

Returns

formatted results in the following format

[
    [['I', -0.011], ['am', 0.009], ['a', -0.128], ['developer', -0.161]],
    [['I', -0.02], ['am', 0.007], ['a', -0.8], ['developer', 0.11]],
    [['I', -0.03], ['am', 0.89], ['a', -0.28], ['developer', -0.16]]
]

predict_tabular(input_feature_vectors)#

Takes in customer provided data and returns the prediction values as numpy array (both regression and classification)

Return type

ndarray

predict_wrapper(encoded_feature_vectors)#

Wraps user supplied predict function. Handles decoding back into original format. For binary classifiers, checks the output size and reshapes to two dimensions if necessary

Return type

ndarray

shap_expected_values()#

Returns expected values from the shap explainer for this model input type

Return type

List[float]