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 adict
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:
OptimalTreeClassifier
OptimalTreeRegressor
OptimalTreePrescriptionLearner
OptimalTreePolicyLearner
OptimalTreeSurvivalLearner
OptimalFeatureSelectionClassifier
OptimalFeatureSelectionRegressor
Predictor
— Typeiai.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
apply
— Methodpredictor.apply(X)
Return the leaf index in the Optimal Trees model into which each point in the features X
falls.
Equivalent to apply
.
predict
— Methodpredictor.predict(X)
Return the predictions made by the model for each point in the features X
.
Equivalent to predict
.
predict_proba
— Methodpredictor.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_outcomes
— MethodThe 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 dict
s as it would be represented as JSON.
Equivalent to predict_outcomes
.
predict_treatment_outcome
— Methodpredictor.predict_treatment_outcome(X)
Return the estimated quality of each treatment in the trained Optimal Policy Tree model for each point in the features X
.
Equivalent to predict_treatment_outcome
.
predict_treatment_rank
— Methodpredictor.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_hazard
— Methodpredictor.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
.
predict_expected_survival_time
— Methodpredictor.predict_expected_survival_time(X)
Return the expected survival time estimate made by the Optimal Survival Tree model for each point in the data X
.
Equivalent to predict_expected_survival_time
.