This workflow uses churn prediction use case as an example to build an app using prediction output. Summary of usecase can be found here.
Overview of steps:
Fetch predictions and prediction explanation from a datarobot deployment (Notebook)
Save the output of the prediciton as a csv which can then be used as a backend for the Streamlit app (Please note that an alternate approach would be to directly request predicitons to DR prediction API and generate predictions on the fly in the app)
Streamlit_app.py file contains code to generate the frontend of the app which helps user access the churn prediction score and provides information of top churn reason for the population selected by the user
To use this workflow for your own use case, you should have created and deployed a model using Datarobot. A short tour is available here at Build and deploy AI models
Have the deployment id for your deployment
Have the scoring dataset ready for making predcitions/ fetch prediction explanations
To learn and test how the app would look like, just follow this notebook and you can use the example prediction dataset here for churn prediction
Instructions to use this workflow for your use case:
Enter your Datarobot API token and end point in the “Connect to Datarobot section”
Enter your deployment id in “Fetch information about your deployment” section
Load the dataset on which you want to make predictions
Export the predictions obtained in step “Request predictions”
Modify the relevant sections in the streamlit_app.py file
Import libraries
In [1]:
import json
import datarobot as dr
import numpy as np
import pandas as pd
import requests
import streamlit as st
In DataRobot, navigate to Developer Tools by clicking on the user icon in the top-right corner. From here you can generate a API Key that you will use to authenticate to DataRobot. You can find more details on creating an API key in the DataRobot documentation
Determine your DataRobot API Endpoint: the API endpoint will be the same as your DataRobot UI root. Replace {datarobot.example.com} with your deployment endpoint.
In [ ]:
DATAROBOT_API_TOKEN = "[ENTER YOUR API KEY]"
DATAROBOT_ENDPOINT = "[https://{datarobot.example.com}/api/v2]"
dr.Client(token=DATAROBOT_API_TOKEN, endpoint=DATAROBOT_ENDPOINT)
Fetch information about your deployment to make predictions
To generate predictions on new data using the Prediction API, you need:
In [4]:
# Get this information from prediction>real time tab of your deployment
DEPLOYMENT_ID = "ENTER YOUR DEPLOYMENT_ID"
Load the scoring data
This workflow assumes that you have the data to be scored as a csv file saved on your computer
In [4]:
# import the scoring file
scoring_data = pd.read_csv("prediction_data_SHAP.csv")
In [5]:
# display rows
scoring_data.head()
0
1
2
3
4
Customer_ID
8779-QRDMV
7495-OOKFY
1658-BYGOY
4598-XLKNJ
4846-WHAFZ
Dependents
No
Yes
Yes
Yes
Yes
Number_of_Referrals
0
1
0
1
1
Tenure_in_Months
1
8
18
25
37
Internet_Type
DSL
Fiber Optic
Fiber Optic
Fiber Optic
Fiber Optic
Internet_Service
Yes
Yes
Yes
Yes
Yes
Contract
Month-to-Month
Month-to-Month
Month-to-Month
Month-to-Month
Month-to-Month
Paperless_Billing
Yes
Yes
Yes
Yes
Yes
Payment_Method
Bank Withdrawal
Credit Card
Bank Withdrawal
Bank Withdrawal
Bank Withdrawal
Monthly_Charge
39.65
80.65
95.45
98.5
76.5
Zip_Code
90022
90063
90065
90303
90602
Lat_Long
34.02381, -118.156582
34.044271, -118.185237
34.108833, -118.229715
33.936291, -118.332639
33.972119, -118.020188
Latitude
34.02381
34.044271
34.108833
33.936291
33.972119
Longitude
-118.156582
-118.185237
-118.229715
-118.332639
-118.020188
Request predictions
In [6]:
# Create a batch prediction job to get predicitons and explanations
job, df = dr.BatchPredictionJob.score_pandas(
DEPLOYMENT_ID, scoring_data, max_explanations=5, passthrough_columns=["Customer_ID"]
)
Streaming DataFrame as CSV data to DataRobot
Created Batch Prediction job ID 63f44acb42a1b29724edc262
Waiting for DataRobot to start processing
Job has started processing at DataRobot. Streaming results.
Export the predicted file as a csv
In [8]:
df.to_csv("prediction_output_SHAP.csv", index=False)
Use the prediction output obtained from Datarobot (‘prediction_output_SHAP.csv’) as backend data for your streamlit app
At this point you will navigate to the streamlit_app.py file to make modifications based on your prediction dataset and then run the streamlit code below to test your app.
In [ ]:
!streamlit run streamlit_app.py --theme.base 'dark'
Get Started with Free Trial
Experience new features and capabilities previously only available in our full AI Platform product.