Skip to content

Instantly share code, notes, and snippets.

@sroecker
Created May 31, 2021 08:21
Show Gist options
  • Save sroecker/3ffabf589c9de9a528b388254af69979 to your computer and use it in GitHub Desktop.
Save sroecker/3ffabf589c9de9a528b388254af69979 to your computer and use it in GitHub Desktop.
DataRobot Python API Get Prediction Explanations
def upload_dataset_from_catalog(self, dataset):
import json
import time
# client = dr.Client()
res = client.post(client.endpoint + "/projects/{}/predictionDatasets/datasetUploads/".format(self.id), json={"datasetId": dataset.id})
dataset_id = json.loads(res.text)['datasetId']
while dataset_id not in [ds.id for ds in self.get_datasets()]:
time.sleep(1)
return dataset_id
def get_prediction_explanations(self, dataset_id):
project = dr.Project.get(self.project_id)
if project.advanced_options['shap_only_mode'] == False:
try:
dr.PredictionExplanationsInitialization.get(project.id, self.id)
except dr.errors.ClientError as e:
if e.json['message'] == 'No prediction explanations initialization found for model':
print('Initialising Explanations...')
while True:
try:
pei_job = dr.PredictionExplanationsInitialization.create(project.id, self.id)
pei_job.wait_for_completion()
except dr.errors.ClientError as e:
if e.json['message'] == 'A prerequisite was not satisfied: Feature impact has not been computed for the model. Run feature impact for this model first.':
print('Calculating Feature Impact...')
self.get_or_request_feature_impact()
else:
raise(e)
else:
print('Explanations Initialised')
break
else:
raise(e)
print('Calculation Explanations...')
while True:
try:
pe_job = dr.PredictionExplanations.create(project.id, self.id, dataset_id)
pe = pe_job.get_result_when_complete()
except dr.errors.ClientError as e:
if e.json['message'] == 'Predictions must first be generated for this dataset':
print('Calculating Predictions...')
p_job = self.request_predictions(dataset_id)
p_job.get_result_when_complete()
else:
raise(e)
else:
print('Explanations Done')
break
return pe.get_all_as_dataframe()
else:
shap_matrix_job = dr.models.ShapMatrix.create(project.id, self.id, dataset_id)
shap_matrix = shap_matrix_job.get_result_when_complete()
return shap_matrix.get_as_dataframe()
dr.Project.upload_dataset_from_catalog = upload_dataset_from_catalog
dr.Model.get_prediction_explanations = get_prediction_explanations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment