Model explainer
Explainer
Parent class for the explainers
Source code in template_nlp/monitoring/model_explainer.py
__init__(*args, **kwargs)
explain_instance(content, **kwargs)
Explains a prediction
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Returns: (?): An explanation object
Source code in template_nlp/monitoring/model_explainer.py
explain_instance_as_html(content, **kwargs)
Explains a prediction - returns an HTML object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Returns: str: An HTML code with the explanation
Source code in template_nlp/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: str: A JSON serializable object with the explanation
Source code in template_nlp/monitoring/model_explainer.py
explain_instance_as_list(content, **kwargs)
Explains a prediction - returns a list object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Returns: list: List of tuples with words and corresponding weights
Source code in template_nlp/monitoring/model_explainer.py
LimeExplainer
Bases: Explainer
Lime Explainer wrapper class
Source code in template_nlp/monitoring/model_explainer.py
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 |
|
__init__(model, model_conf)
Initialization
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[ModelClass]
|
A model instance with predict & predict_proba functions, and list_classes attribute |
required |
model_conf |
dict
|
The model's configuration |
required |
Raises:
TypeError: If the provided model does not implement a predict_proba
function
TypeError: If the provided model does not have a list_classes
attribute
Source code in template_nlp/monitoring/model_explainer.py
classifier_fn(content_list)
Function to get probabilities from a list of (not preprocessed) texts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content_list |
list
|
texts to be considered |
required |
Returns: np.array: probabilities
Source code in template_nlp/monitoring/model_explainer.py
explain_instance(content, class_or_label_index=None, max_features=15, **kwargs)
Explains a prediction
This function calls the Lime module. It creates a linear model around the input text to evaluate the weight of each word in the final prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Kwargs: class_or_label_index (Union[None, int, Iterable]): for classification only. Class or label index to be considered. max_features (int): Maximum number of features (cf. Lime documentation) Returns: (?): An explanation object
Source code in template_nlp/monitoring/model_explainer.py
explain_instance_as_html(content, class_or_label_index=None, max_features=15, **kwargs)
Explains a prediction - returns an HTML object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Kwargs: class_or_label_index (int): for classification only. Class or label index to be considered. max_features (int): Maximum number of features (cf. Lime documentation) Returns: str: An HTML code with the explanation
Source code in template_nlp/monitoring/model_explainer.py
explain_instance_as_json(content, class_or_label_index=None, max_features=15, **kwargs)
Explains a prediction - returns a JSON serializable object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Kwargs: class_or_label_index (Union[None, int, Iterable]): for classification only. Class or label index to be considered. max_features (int): Maximum number of features (cf. Lime documentation) Returns: Union[list, dict]: JSON serializable object containing a list of tuples with words and corresponding weights
Source code in template_nlp/monitoring/model_explainer.py
explain_instance_as_list(content, class_or_label_index=None, max_features=15, **kwargs)
Explains a prediction - returns a list object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
str
|
Text to be explained |
required |
Kwargs: class_or_label_index (int): for classification only. Class or label index to be considered. max_features (int): Maximum number of features (cf. Lime documentation) Returns: list: List of tuples with words and corresponding weights