Skip to main content

Custom Explanations with Plotly

Custom explanations are enabled if a model is assigned a data set 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 a metadata field "explanations.format" with value "plotly"
  • There is a boolean column is_background in the model input (request)
  • There is at least one row with value true in the is_background column
  • There is at least one row with value false in the is_background column

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(context, data)
}

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 as python dictionaries.

The _draw_explanations method return a list of plotly figures as python dictionaries.