Model Deployment

To help with deploying trained models in production, the interpretableai package offers a lightweight prediction API that can load saved models from JSON files and use these models to make predictions for new data without requiring a Julia installation.

This functionality is provided by the interpretableai.Predictor object, and is accessible without first needing to import the iai submodule, meaning it can be used without Julia installed on the machine.

Example for Optimal Trees Predictors

We can create a Predictor object by passing the path to a JSON file containing the model. We can then use the methods of this object, e.g. predict, to make predictions on new input data:

from interpretableai import Predictor
predictor = Predictor('lnr.json')
pred = predictor.predict(X)
[22.2, 22.2, 22.2, 22.2, 17.26, 17.26, 13.414286, 22.2, 22.2, 17.26, 17.26, 17.26, 17.26, 17.26, 13.414286, 13.414286, 13.414286, 30.88, 30.88, 30.88, 22.2, 17.26, 17.26, 13.414286, 17.26, 30.88, 22.2, 30.88, 13.414286, 22.2, 13.414286, 22.2]

The new data can be supplied in the following formats:

  • a pandas.DataFrame
  • a list containing a dict for each sample in the data, where the keys are the feature names and the values indicate the corresponding feature values (i.e. the features as they would be represented as JSON)

API Reference

The applicable API depends on the type of learner. For each of the learner type below, we list the supported APIs:

PredictorType
iai.Predictor(filename)

An object that reads saved Interpretable AI models from JSON files and makes predictions.

Parameters

  • filename: (str) path to the JSON file to be read in
applyMethod
predictor.apply(X)

Return the leaf index in the Optimal Trees model into which each point in the features X falls.

Equivalent to apply.

predictMethod
predictor.predict(X)

Return the predictions made by the model for each point in the features X.

Equivalent to predict.

predict_probaMethod
predictor.predict_proba(X)

Return the probabilities of class membership predicted by the model for each point in the features X.

Equivalent to predict_proba.

predict_outcomesMethod

The behavior of this function depends on the type of model.


predictor.predict_outcomes(X)

For Optimal Prescriptive Trees, return the predicted outcome for each point in the features X for each treatment made by the model.

Equivalent to predict_outcomes.


predictor.predict_outcomes(X, rewards)

For Optimal Policy Trees, return the outcome for each point in the features X from rewards under the prescriptions made by the model. The rewards can be a numpy matrix, a pandas dataframe, or a list of dicts as it would be represented as JSON.

Equivalent to predict_outcomes.

predict_treatment_rankMethod
predictor.predict_treatment_rank(X)

Return the treatments in ranked order of effectiveness for each point in the features X as predicted by the Optimal Policy Tree model.

Equivalent to predict_treatment_rank.

predict_hazardMethod
predictor.predict_hazard(X)

Return the fitted hazard coefficient estimate made by the Optimal Survival Tree model for each point in the data X.

Equivalent to predict_hazard.