Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.

Qlik AutoML: How to generate predictions via API realtime-predictions with Python

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
KellyHobson
Former Employee
Former Employee

Qlik AutoML: How to generate predictions via API realtime-predictions with Python

Last Update:

Oct 24, 2022 10:46:36 AM

Updated By:

Sonja_Bauernfeind

Created date:

Oct 21, 2022 2:14:45 PM

In a previous article, I outlined the steps to POST an API call to realtime-predictions with Postman. In this article, we will look at how to do this in Python and send a file with records to predict against.

As before, we use iris.csv dataset, a common data science example. Variety is the target variable.

 

Steps

  1. Upload, train and deploy the model for iris.csv.  See this article for steps on how to deploy a model. 

    XGBoost Classification was the champion model which I choose to deploy.

  2. Generate an API key. Copy this information for later steps. 

  3. Open the Deployed model and navigate to the Real-time predictions tab

    deployemodel_realtimepreds.png

  4. Copy the Realtime-predictions URL to a safe place

    copyurl_3.png

  5. Open a Python instance (in this case a jupyter notebook) and import the required packages.

    Set the API key and API URL as string variables.

    api_python1.png

  6. Read a local data file to a dataframe

    api_python2.png

  7. Set the proper header, json_data, and parameter values

    api_python3.png

  8. Get the response and print the result

    api_python4.png

 

In our example, we print response.text, but this could also have been saved as a file.  This is a powerful option for generating prediction results locally or in a location python has access to. 

Additionally, with pandas, we can send a larger amount of records (from CSV to dataframe) in one POST call rather than having manual JSON entries with Postman. 

 

Here is the code used above:

 

 

api_url = 'https://earlyaccess.us.qlik-stage.com/api/v1/automl-deployments/xxx/realtime-predictions'
api_key = 'xxxx'

df = pd.read_csv('C:\\tmp\\iris_pred.csv')
rows = df.to_dict('tight')['data']

headers = {
    'Authorization': 'Bearer ' + api_key,
    'Content-type': 'application/json',
    'Accept': 'text/csv',
}
json_data = {
    "rows": rows, 
    "schema": [
          {"name": "sepal.length"},
          {"name": "sepal.width"},
          {"name": "petal.length"},
		  {"name": "petal.width"} 
    ],
}
params = {
    'includeNotPredictedReason': 'false',
    'includeShap': 'true',
    'includeSource': 'false'
}

response = requests.post(api_url, headers=headers, json=json_data, params=params)
print(response.text)

 

 

 

 

Environment

 

The information in this article is provided as-is and to be used at own discretion. Depending on tool(s) used, customization(s), and/or other factors ongoing support on the solution below may not be provided by Qlik Support.

 

Labels (1)
Version history
Last update:
‎2022-10-24 10:46 AM
Updated by: