如何将从 Azure 获得的值作为请求响应分开?
How to separate the values that was got from Azure as request response?
我在这里插入所有代码。所以请任何人帮助我将名为“评分标签”的列下的值分开。例如1 或 0。
基本上,我在 Python 中使用 urllib 作为请求传递 X 轴、Y 轴和 Z 轴值。为此,我得到了一堆列表的响应。这里我只需要“Scored Labels”数据,仅供他用。
这是示例请求代码
{
"Inputs": {
"input1": {
"ColumnNames": [
"X axis",
"Y axis",
"Z axis"
],
"Values": [
[
"0",
"0",
"0"
],
[
"0",
"0",
"0"
]
]
}
},
"GlobalParameters": {}
}
Input Parameters
这是示例响应
{
"Results": {
"output1": {
"type": "DataTable",
"value": {
"ColumnNames": [
"X axis",
"Y axis",
"Z axis",
"Scored Labels",
"Scored Probabilities"
],
"ColumnTypes": [
"Numeric",
"Numeric",
"Numeric",
"Numeric",
"Numeric"
],
"Values": [
[
"0",
"0",
"0",
"0",
"0"
],
[
"0",
"0",
"0",
"0",
"0"
]
]
}
}
}
}
Output Parameters
所有示例代码都是这样的(这在请求响应 API 我的机器学习模型 Web 服务的文档中给出)
import urllib2
# If you are using Python 3+, import urllib instead of urllib2
import json
data = {
"Inputs": {
"input1":
{
"ColumnNames": ["X axis", "Y axis", "Z axis"],
"Values": [ [ "0", "0", "0" ], [ "0", "0", "0" ], ]
}, },
"GlobalParameters": {
}
}
body = str.encode(json.dumps(data))
url = 'https://ussouthcentral.services.azureml.net/workspaces/b5eeedd29d564074b47a9c8ce5faa745/services/e6065c9017d0426196db3bbf0f78e9da/execute?api-version=2.0&details=true'
api_key = 'abc123' # Replace this with the API key for the web service
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
req = urllib2.Request(url, body, headers)
try:
response = urllib2.urlopen(req)
# If you are using Python 3+, replace urllib2 with urllib.request in the above code:
# req = urllib.request.Request(url, body, headers)
# response = urllib.request.urlopen(req)
result = response.read()
print(result)
except urllib2.HTTPError, error:
print("The request failed with status code: " + str(error.code))
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(error.info())
print(json.loads(error.read()))
我的输出为
This get b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'
Process finished with exit code 0
对于我的这个输入-->。 (我只需要第 4 个参数为 0 或 1)
"Inputs": {
"input1":
{
"ColumnNames": ["X axis", "Y axis", "Z axis"],
"Values": [["0.202999", "9.345999", "-2.9790000"], ["-0.15099999", "9.378999", "-2.8689999"], ]
# "Values": [["0.202999", "9.345999", "-2.9790000"], ]
}, },
"GlobalParameters": {
}
请帮我解决这个问题。
提前致谢。
您可以试试下面的代码片段/
在下面的代码片段中,用您的输出响应替换 x 或存储 x。
import json
x = b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'
#print(x)
y= json.loads(x)
output = y["Results"]
for value in output["output1"]["value"]["Values"]:
print(value[3])
示例输出:
我在这里插入所有代码。所以请任何人帮助我将名为“评分标签”的列下的值分开。例如1 或 0。 基本上,我在 Python 中使用 urllib 作为请求传递 X 轴、Y 轴和 Z 轴值。为此,我得到了一堆列表的响应。这里我只需要“Scored Labels”数据,仅供他用。
这是示例请求代码
{
"Inputs": {
"input1": {
"ColumnNames": [
"X axis",
"Y axis",
"Z axis"
],
"Values": [
[
"0",
"0",
"0"
],
[
"0",
"0",
"0"
]
]
}
},
"GlobalParameters": {}
}
Input Parameters
这是示例响应
{
"Results": {
"output1": {
"type": "DataTable",
"value": {
"ColumnNames": [
"X axis",
"Y axis",
"Z axis",
"Scored Labels",
"Scored Probabilities"
],
"ColumnTypes": [
"Numeric",
"Numeric",
"Numeric",
"Numeric",
"Numeric"
],
"Values": [
[
"0",
"0",
"0",
"0",
"0"
],
[
"0",
"0",
"0",
"0",
"0"
]
]
}
}
}
}
Output Parameters
所有示例代码都是这样的(这在请求响应 API 我的机器学习模型 Web 服务的文档中给出)
import urllib2
# If you are using Python 3+, import urllib instead of urllib2
import json
data = {
"Inputs": {
"input1":
{
"ColumnNames": ["X axis", "Y axis", "Z axis"],
"Values": [ [ "0", "0", "0" ], [ "0", "0", "0" ], ]
}, },
"GlobalParameters": {
}
}
body = str.encode(json.dumps(data))
url = 'https://ussouthcentral.services.azureml.net/workspaces/b5eeedd29d564074b47a9c8ce5faa745/services/e6065c9017d0426196db3bbf0f78e9da/execute?api-version=2.0&details=true'
api_key = 'abc123' # Replace this with the API key for the web service
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}
req = urllib2.Request(url, body, headers)
try:
response = urllib2.urlopen(req)
# If you are using Python 3+, replace urllib2 with urllib.request in the above code:
# req = urllib.request.Request(url, body, headers)
# response = urllib.request.urlopen(req)
result = response.read()
print(result)
except urllib2.HTTPError, error:
print("The request failed with status code: " + str(error.code))
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(error.info())
print(json.loads(error.read()))
我的输出为
This get b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'
Process finished with exit code 0
对于我的这个输入-->。 (我只需要第 4 个参数为 0 或 1)
"Inputs": {
"input1":
{
"ColumnNames": ["X axis", "Y axis", "Z axis"],
"Values": [["0.202999", "9.345999", "-2.9790000"], ["-0.15099999", "9.378999", "-2.8689999"], ]
# "Values": [["0.202999", "9.345999", "-2.9790000"], ]
}, },
"GlobalParameters": {
}
请帮我解决这个问题。 提前致谢。
您可以试试下面的代码片段/
在下面的代码片段中,用您的输出响应替换 x 或存储 x。
import json
x = b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'
#print(x)
y= json.loads(x)
output = y["Results"]
for value in output["output1"]["value"]["Values"]:
print(value[3])
示例输出: