alex-mon888
alex-mon888

Reputation: 51

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

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:

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.

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 very appreciate any help or direction. Thank you.

Upvotes: 0

Views: 211

Answers (1)

unknown
unknown

Reputation: 7483

This issue was solved by AlphaLu-0572's answer, add it as the answer to close the question:

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.

Upvotes: 1

Related Questions