Data input format (call the service) for Azure ML time series forecast model deployed as a web service (Python)

Alexey Pisakov 36 Reputation points
2021-01-04T04:08:30.997+00:00

Sorry in advance for the lengthy question as I wanted to explain it as detailed as possible. I used the Azure AutoML to train a model and deployed it as a web service. Now I can access (call) it over the REST endpoint.

I have the following data types for attributes: date (timestamp), number, number, number, number, integer. I trained the model with the following parametres:

Timestaps interval: 15 min
Forecast Horizon: 4 (I need the forecast every hour for the next hour)
Target rolling window size: 96 (the forecast must ba based on the last 24 hours of data)

I have two questions.

  1. As I understand, based on the above, I have to provide last 4 entries to the model for a correct prediction. Otherwise, it will consider a time gap. Am I right? In this case, how I could input 4 instances at a time for a single prediction? The following example is wrong as it asks for 4 predictions for each instance: import requests
    import json

    URL for the web service

    scoring_uri = 'http://xxxxx-xxxxxxx-xxxxxx-xxxxxxx.xxxxx.azurecontainer.io/score' data = {"data":
    [
    [
    2020-10-04 19:30:00,1.29281,1.29334,1.29334,1.29334,1
    ],
    [
    2020-10-04 19:45:00,1.29334,1.29294,1.29294,1.29294,1
    ],
    [
    2020-10-04 21:00:00,1.29294,1.29217,1.29334,1.29163,34
    ],
    [
    2020-10-04 21:15:00,1.29217,1.29257,1.29301,1.29115,195]
    ]
    }

    Convert to JSON string

    input_data = json.dumps(data)

    Set the content type

    headers = {'Content-Type': 'application/json'}

    Make the request and display the response

    resp = requests.post(scoring_uri, input_data, headers=headers)
    print(resp.text)

The above code is based on the provided Microsoft example https://learn.microsoft.com/en-us/azure/machine-learning/how-to-consume-web-service?tabs=python#call-the-service-python.

  1. I am unable to replicate the provided example with my data. I have an error "SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers" pointing to the date. I assume, I need to specify the data type but could not find how.
    I tried to load a line from a csv file but I have an error (SyntaxError: invalid syntax) pointing to "with" with the following: data = {"data":
    [with open('file', "r") as f:
    for line in f: pass
    print(line)]
    }

I tested getting the last line from a csv file intependetly and it works but not inside the full script.

I very appreciate any help or direction. Thank you.

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,333 questions
{count} vote

Accepted answer
  1. Alpha Lu 81 Reputation points
    2021-01-04T21:41:48.693+00:00

    @Alexey Pisakov
    Please try the solution mentioned below.

    The service takes data in form of deserialized pandas data frame. In the example below, it will look like:
    import json

    X_test = pd.DataFrame([

    ['2020-10-04 19:30:00', 1.29281, 1.29334, 1.29334, 1.29334, 1],  
    
    ['2020-10-04 19:45:00', 1.29334, 1.29294, 1.29294, 1.29294, 1],  
    
    ['2020-10-04 21:00:00', 1.29294, 1.29217, 1.29334, 1.29163, 34],  
    
    ['2020-10-04 21:15:00', 1.29217, 1.29257, 1.29301, 1.29115, 195]],  
    
    columns=['date', 'number_1', 'number_2', 'number_3', 'number_4', 'integer']  
    

    )

    test_sample = json.dumps({'data': X_test.to_dict(orient='records')})

    test_sample

    Which will result in JSON string as:

    {"data": [{"date": "2020-10-04 19:30:00", "number_1": 1.29281, "number_2": 1.29334, "number_3": 1.29334, "number_4": 1.29334, "integer": 1}, {"date": "2020-10-04 19:45:00", "number_1": 1.29334, "number_2": 1.29294, "number_3": 1.29294, "number_4": 1.29294, "integer": 1}, {"date": "2020-10-04 21:00:00", "number_1": 1.29294, "number_2": 1.29217, "number_3": 1.29334, "number_4": 1.29163, "integer": 34}, {"date": "2020-10-04 21:15:00", "number_1": 1.29217, "number_2": 1.29257, "number_3": 1.29301, "number_4": 1.29115, "integer": 195}]}

    Please rename the columns to the corresponding columns from the training data set.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.