Model explainer
Explainer
Parent class for the explainers
Source code in template_num/monitoring/model_explainer.py
__init__(*args, **kwargs)
explain_instance(content, **kwargs)
Explains a prediction
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
DataFrame
|
Single entry to be explained |
required |
Returns: (?): An explanation object
Source code in template_num/monitoring/model_explainer.py
explain_instance_as_html(content, **kwargs)
Explains a prediction - returns an HTML object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
DataFrame
|
Single entry to be explained |
required |
Returns: str: An HTML code with the explanation
Source code in template_num/monitoring/model_explainer.py
explain_instance_as_json(content, **kwargs)
Explains a prediction - returns an JSON serializable object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Returns: Union[dict, list]: A JSON serializable object containing the explanation
Source code in template_num/monitoring/model_explainer.py
ShapExplainer
Bases: Explainer
Shap Explainer wrapper class
Source code in template_num/monitoring/model_explainer.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
__init__(model, anchor_data, anchor_preprocessed=False)
Initialization
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[ModelClass]
|
A model instance with predict (regressors) or predict_proba (classifiers) functions |
required |
anchor_data |
DataFrame
|
data anchor needed by shap (usually 100 data points) |
required |
Kwargs:
anchor_preprocessed (bool): If the anchor data has already been preprocessed
Raises:
TypeError: If the provided model is a regressor and does not implement a predict
function
TypeError: If the provided model is a classifier and does not implement a predict_proba
function
Source code in template_num/monitoring/model_explainer.py
classifier_fn(content_prep)
Function to get probabilities from a dataset (already preprocessed) - classifiers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content_prep |
DataFrame
|
dataset (already preprocessed) to be considered |
required |
Returns: np.array: probabilities
Source code in template_num/monitoring/model_explainer.py
explain_instance(content, class_or_label_index=None, **kwargs)
Explains predictions by returning a shap.Explanation object
This function calls the Shap module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
DataFrame
|
Entries to be explained |
required |
Kwargs: class_or_label_index (int): for classification only. Class or label index to be considered. Returns: shap.Explanation: Shap Explanation object
Source code in template_num/monitoring/model_explainer.py
explain_instance_as_html(content, class_or_label_index=None, **kwargs)
Explains a prediction - returns an HTML object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
DataFrame
|
Single entry to be explained |
required |
Kwargs: class_or_label_index (int): for classification only. Class or label index to be considered. Returns: str: An HTML code with the explanation
Source code in template_num/monitoring/model_explainer.py
explain_instance_as_json(content, class_or_label_index=None, **kwargs)
Explains predictions by returning a JSON serializable object
This function calls the Shap module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
DataFrame
|
entries to be explained |
required |
Kwargs: class_or_label_index (int): for classification only. Class or label index to be considered. Returns: (Union[dict, list]): Shap values
Source code in template_num/monitoring/model_explainer.py
regressor_fn(content_prep)
Function to get predictions from a dataset (already preprocessed) - regressors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content_prep |
DataFrame
|
dataset (already preprocessed) to be considered |
required |
Returns: np.array: predictions