AWS AI Services Programming Series - Part6 (Forecast)

   Go back to the Task List

  « 7: Setup Development Environment    9. Clean up »

8: Create Client

In this task, you write python code to call Amazon Forecast APIs to get forecast for specific item (zip code).

  1. Before you write python code, you need to make note of the ARN of the forecast. In Amazon Forecast console, open dojo_dataset_group details. Select Forecasts in the left menu and then click on the dojo_forecast to open the details. Make note of the Forecast ARN field.

    Forecast

  2. Next, goto the AWS Cloud9 console and click on the New File option under the File menu.

    Forecast

  3. A new Untitled1 file gets created. Write the following code in the Untitled1 file.

    Forecast

    import boto3
    
    client = boto3.client('forecastquery')
    
    response = client.query_forecast(ForecastArn='{FORECAST-ARN}',  StartDate="2019-07-28T00:00:00", EndDate="2019-08-26T00:00:00", Filters={"item_id" : "2606"})
    
    print (response["Forecast"]["Predictions"])
    

    `

  4. In the code above, replace {FORECAST-ARN} with the Forecast ARN you noted in the first step.

  5. The code is simple. You first create client to forecastquery. Then you call query_forecast method to get forecast for the item 2606 for the date range. Finally, you prints all the predictions from the response.

  6. Click on the Save option under the File menu. On the Save As popup, type in dojoforecastclient.py as the file name and click on the Save button. The code is saved in the file.

    Forecast

  7. In the console window, execute python dojoforecastclient.py command to run the dojoforecastclient.py code. The code execution finishes in no time and will print the predictions in the JSON format.

    Forecast

  8. This was an example of how you can build client to call Amazon Forecast APIs. People generally use python pandas to plot the forecast to get visualization. We leave that to your experimentation. The workshop finishes here. Goto the next task to clean-up the resources so that you don’t incur any cost post the workshop.