如何解决部署为 Web 服务 (Python) 的 Azure ML 时间序列预测模型的数据输入格式调用服务
对于这个冗长的问题,我想尽可能详细地解释一下,预先抱歉。 我使用 Azure AutoML 训练模型并将其部署为 Web 服务。现在我可以通过 REST 端点访问(调用)它。
我有以下属性数据类型:日期(时间戳)、数字、数字、数字、数字、整数。 我使用以下参数训练模型:
- 时间间隔:15 分钟
- Forecast Horizon:4(我需要下一小时每小时的预测)
- 目标滚动窗口大小:96(预测必须基于过去 24 小时的数据)
据我所知,基于上述内容,我必须向模型提供最后 4 个条目才能进行正确预测。否则,它会考虑一个时间间隔。我对吗?在这种情况下,我如何一次输入 4 个实例进行单个预测?以下示例是错误的,因为它要求对每个实例进行 4 次预测:
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
],[
2020-10-04 19:45:00,1.29294,[
2020-10-04 21:00:00,1.29217,1.29163,34
],[
2020-10-04 21:15:00,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)
以上代码基于提供的 Microsoft 示例 https://docs.microsoft.com/en-us/azure/machine-learning/how-to-consume-web-service?tabs=python#call-the-service-python。
我无法用我的数据复制提供的示例。我有一个错误“语法错误:不允许在十进制整数文字中使用前导零;八进制整数使用 0o 前缀”指向日期。我假设,我需要指定数据类型,但找不到方法。
我非常感谢任何帮助或指导。谢谢。
解决方法
此问题已由 AlphaLu-0572 的 answer 解决,将其添加为结束问题的答案:
该服务以反序列化的熊猫数据框的形式获取数据。在下面的例子中,它看起来像:
import json
X_test = pd.DataFrame([
['2020-10-04 19:30:00',1.29281,1.29334,1],['2020-10-04 19:45:00',1.29294,['2020-10-04 21:00:00',1.29217,1.29163,34],['2020-10-04 21:15:00',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
这将导致 JSON 字符串为:
{"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,{"date": "2020-10-04 21:00:00","number_1": 1.29294,"number_2": 1.29217,"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}]}
请将列重命名为训练数据集中的相应列。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。