Custom Explanations with Plotly
Custom explanations are enabled if a model is assigned a dataset and the MLmodel YAML file specifies that explanations are provided by the model using Plotly:
metadata:
explanations: plotly
In this case, the prediction endpoint will receive background data taken from the data assigned to the model. The background data can be used for generating prediction explanations via, e.g., SHAP or other model interpretation methods, which can be visualized using Plotly. To display such visualizations in the navio model try-out view, it is sufficient to output the Plotly figure data as a JSON object under the field explanation in the model’s prediction method.
Explanations are generated when the following conditions are fulfilled:
- The model has the metadata field
explanationsset toplotly(as shown above) - There is a boolean column
is_backgroundin the model input (request) - There is at least one row with value
truein theis_backgroundcolumn - There is at least one row with value
falsein theis_backgroundcolumn
Example:
import mlflow
import pandas as pd
class ExplainableModel(mlflow.pyfunc.PythonModel):
BG_COLUMN = 'is_background'
def has_background(self, data: pd.DataFrame) -> bool:
if self.BG_COLUMN not in data.columns:
return False
if not data[self.BG_COLUMN].isin({True, False}).all():
return False
# must have at least one of each True and False
if data[self.BG_COLUMN].nunique() != 2:
return False
return True
def select_data(self, data: pd.DataFrame, background: bool) -> pd.DataFrame:
return data.loc[data[self.BG_COLUMN] == background] \
.drop(self.BG_COLUMN, axis=1)
def _predict(self, data) -> list:
... # to be implemented
def _draw_explanations(self, data, background) -> list:
... # to be implemented
def predict(self, context, model_input):
if not self.has_background(model_input):
return {
'prediction': self._predict(model_input)
}
background = self.select_data(model_input, True)
data = self.select_data(model_input, False)
return {
'prediction': self._predict(data),
'explanation': self._draw_explanations(data, background)
}
The _predict method returns a list of predictions (see predict() for the expected format).
The _draw_explanations method returns a list of Plotly figures as Python dictionaries.