<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>article Qlik AutoML: How to generate predictions via API realtime-predictions with Python in Official Support Articles</title>
    <link>https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-generate-predictions-via-API-realtime/ta-p/1995683</link>
    <description>&lt;P&gt;In a &lt;A href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-test-API-realtime-predictions-from-Postman/ta-p/1992894" target="_blank" rel="noopener"&gt;previous article&lt;/A&gt;, 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.&lt;/P&gt;
&lt;P&gt;As before, we use &lt;A href="https://gist.github.com/netj/8836201" target="_blank" rel="noopener"&gt;iris.csv dataset&lt;/A&gt;, a common data science example. Variety is the target variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H4&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Steps&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;Upload, train and deploy the model for &lt;FONT face="courier new,courier"&gt;iris.csv&lt;/FONT&gt;.&amp;nbsp; See&lt;/SPAN&gt;&lt;A href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-upload-model-deploy-and-predict-on-Qlik-Cloud/ta-p/1960164" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;this article&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;for steps on how to deploy a model.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;XGBoost Classification was the champion model which I choose to deploy.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A style="font-family: inherit; background-color: #ffffff;" href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-generate-API-Keys/ta-p/1964795" target="_blank" rel="noopener"&gt;Generate an API key&lt;/A&gt;&lt;SPAN&gt;. Copy this information for later steps.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Open the Deployed model and navigate to the Real-time predictions tab&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="deployemodel_realtimepreds.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91925i258879C49CA2F99E/image-size/large?v=v2&amp;amp;px=999" role="button" title="deployemodel_realtimepreds.png" alt="deployemodel_realtimepreds.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;Copy the Realtime-predictions URL to a safe place&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="copyurl_3.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91924iDF1D19C46C0981EB/image-size/large?v=v2&amp;amp;px=999" role="button" title="copyurl_3.png" alt="copyurl_3.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Open a Python instance (in this case a jupyter notebook) and import the required packages. &lt;BR /&gt;&lt;BR /&gt;Set the API key and API URL as string variables. &lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python1.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91926i1444640D600EA664/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python1.png" alt="api_python1.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Read a local data file to a dataframe&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python2.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91927i3E951F622992DA8A/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python2.png" alt="api_python2.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Set the proper header, json_data, and parameter values&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python3.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91928iBEC70E515E524EF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python3.png" alt="api_python3.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Get the response and print the result&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python4.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91929i180882A440FFFDB9/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python4.png" alt="api_python4.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In our example, we print &lt;FONT face="courier new,courier"&gt;response.text&lt;/FONT&gt;, but this could also have been saved as a file.&amp;nbsp;&amp;nbsp;This is a powerful option for generating prediction results locally or in a location python has access to.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code used above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H4&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Environment&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;LI-PRODUCT title="Qlik AutoML" id="qlikAutoML"&gt;&lt;/LI-PRODUCT&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="font-style: italic;"&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Oct 2022 14:46:36 GMT</pubDate>
    <dc:creator>KellyHobson</dc:creator>
    <dc:date>2022-10-24T14:46:36Z</dc:date>
    <item>
      <title>Qlik AutoML: How to generate predictions via API realtime-predictions with Python</title>
      <link>https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-generate-predictions-via-API-realtime/ta-p/1995683</link>
      <description>&lt;P&gt;In a &lt;A href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-test-API-realtime-predictions-from-Postman/ta-p/1992894" target="_blank" rel="noopener"&gt;previous article&lt;/A&gt;, 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.&lt;/P&gt;
&lt;P&gt;As before, we use &lt;A href="https://gist.github.com/netj/8836201" target="_blank" rel="noopener"&gt;iris.csv dataset&lt;/A&gt;, a common data science example. Variety is the target variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H4&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Steps&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;Upload, train and deploy the model for &lt;FONT face="courier new,courier"&gt;iris.csv&lt;/FONT&gt;.&amp;nbsp; See&lt;/SPAN&gt;&lt;A href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-upload-model-deploy-and-predict-on-Qlik-Cloud/ta-p/1960164" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;this article&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;for steps on how to deploy a model.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;XGBoost Classification was the champion model which I choose to deploy.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A style="font-family: inherit; background-color: #ffffff;" href="https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-generate-API-Keys/ta-p/1964795" target="_blank" rel="noopener"&gt;Generate an API key&lt;/A&gt;&lt;SPAN&gt;. Copy this information for later steps.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Open the Deployed model and navigate to the Real-time predictions tab&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="deployemodel_realtimepreds.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91925i258879C49CA2F99E/image-size/large?v=v2&amp;amp;px=999" role="button" title="deployemodel_realtimepreds.png" alt="deployemodel_realtimepreds.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;Copy the Realtime-predictions URL to a safe place&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="copyurl_3.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91924iDF1D19C46C0981EB/image-size/large?v=v2&amp;amp;px=999" role="button" title="copyurl_3.png" alt="copyurl_3.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Open a Python instance (in this case a jupyter notebook) and import the required packages. &lt;BR /&gt;&lt;BR /&gt;Set the API key and API URL as string variables. &lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python1.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91926i1444640D600EA664/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python1.png" alt="api_python1.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Read a local data file to a dataframe&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python2.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91927i3E951F622992DA8A/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python2.png" alt="api_python2.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Set the proper header, json_data, and parameter values&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python3.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91928iBEC70E515E524EF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python3.png" alt="api_python3.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Get the response and print the result&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="api_python4.png" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/91929i180882A440FFFDB9/image-size/large?v=v2&amp;amp;px=999" role="button" title="api_python4.png" alt="api_python4.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In our example, we print &lt;FONT face="courier new,courier"&gt;response.text&lt;/FONT&gt;, but this could also have been saved as a file.&amp;nbsp;&amp;nbsp;This is a powerful option for generating prediction results locally or in a location python has access to.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code used above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H4&gt;&lt;FONT color="#339966"&gt;&lt;STRONG&gt;Environment&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;LI-PRODUCT title="Qlik AutoML" id="qlikAutoML"&gt;&lt;/LI-PRODUCT&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="font-style: italic;"&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2022 14:46:36 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Official-Support-Articles/Qlik-AutoML-How-to-generate-predictions-via-API-realtime/ta-p/1995683</guid>
      <dc:creator>KellyHobson</dc:creator>
      <dc:date>2022-10-24T14:46:36Z</dc:date>
    </item>
  </channel>
</rss>

