You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Azure ML部署的Web Service获取预测值与预测概率

问题

我用Auto ML训练了一个预测用户是否违约的二分类模型,部署成Web服务后,调用仅返回0或1的预测标签,现在需要同时获取预测概率。当前调用的Python代码如下:

import urllib.request
import json
import os
import ssl

def allowSelfSignedHttps(allowed):
    # bypass the server certificate verification on client side
    if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None):
        ssl._create_default_https_context = ssl._create_unverified_context

allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service.

# Request data goes here
# The example below assumes JSON formatting which may be updated
# depending on the format your endpoint expects.
data =  {
  "Inputs": {
    "data": [
      {
        "EXT_SOURCE_1": 0.0,
        "EXT_SOURCE_2": 0.0,
        "EXT_SOURCE_3": 0.0,
        "client_installments_AMT_PAYMENT_min_sum": 0.0,
        "NAME_EDUCATION_TYPE_Higher education": 0,
        "DAYS_BIRTH": 0,
        "bureau_DAYS_CREDIT_ENDDATE_max": 0.0,
        "CODE_GENDER_F": 0,
        "AMT_ANNUITY": 0.0,
        "previous_loans_NAME_CONTRACT_STATUS_Refused_count_norm": 0.0,
        "DAYS_EMPLOYED": 0,
        "previous_loans_CNT_PAYMENT_max": 0.0,
        "FLAG_DOCUMENT_3": 0,
        "previous_loans_NAME_YIELD_GROUP_high_count": 0.0,
        "previous_loans_NAME_CONTRACT_STATUS_Approved_count_norm": 0.0,
        "client_installments_AMT_INSTALMENT_min_min": 0.0,
        "bureau_DAYS_CREDIT_max": 0.0,
        "OWN_CAR_AGE": 0.0,
        "client_cash_SK_DPD_DEF_sum_max": 0.0,
        "NAME_FAMILY_STATUS_Married": 0,
        "FLAG_PHONE": 0,
        "DAYS_LAST_PHONE_CHANGE": 0.0,
        "previous_loans_CNT_PAYMENT_mean": 0.0,
        "previous_loans_HOUR_APPR_PROCESS_START_mean": 0.0,
        "bureau_CREDIT_ACTIVE_Active_count": 0.0,
        "client_cash_CNT_INSTALMENT_max_max": 0.0,
        "previous_loans_RATE_DOWN_PAYMENT_sum": 0.0,
        "NAME_INCOME_TYPE_Working": 0,
        "REGION_RATING_CLIENT": 0,
        "bureau_CREDIT_ACTIVE_Active_count_norm": 0.0,
        "SK_ID_CURR": 0
      }
    ]
  },
  "GlobalParameters": {
    "method": "predict"
  }
}

body = str.encode(json.dumps(data))

url = ''
api_key = '' # Replace this with the API key for the web service

headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib.request.Request(url, body, headers)

try:
    response = urllib.request.urlopen(req)
    result = response.read()
    print(result)
except urllib.error.HTTPError as error:
    print("The request failed with status code: " + str(error.code))
    print(error.info())
    print(error.read().decode("utf8", 'ignore'))

当前响应为 b'{"Results": [1]}',希望同时返回预测标签和对应概率。

解决方案

要同时获取预测标签和概率,需要修改Web服务的评分脚本(score.py),让它在返回结果时同时包含两者,具体步骤如下:

1. 修改评分脚本

AutoML部署的默认评分脚本只会返回预测标签,你需要替换成能输出标签和概率的版本。示例脚本如下:

import json
import joblib
import pandas as pd

def init():
    global model
    # 加载训练好的模型,路径需和部署时的模型文件匹配
    model = joblib.load('model.pkl')

def run(raw_data):
    try:
        # 解析请求数据
        input_data = json.loads(raw_data)['Inputs']['data']
        df = pd.DataFrame(input_data)
        
        # 获取预测标签和概率
        predictions = model.predict(df)
        # 取类别1的概率(对应违约的概率),如果需要所有类别概率可直接用predict_proba的结果
        probabilities = model.predict_proba(df)[:, 1]
        
        # 组装结果,每个样本对应标签和概率
        output = [{"label": int(label), "probability": float(prob)} for label, prob in zip(predictions, probabilities)]
        return json.dumps({"Results": output})
    except Exception as e:
        return json.dumps({"error": str(e)})

2. 更新Web服务部署

将修改后的score.py重新部署到你的Web服务,或者更新现有部署的评分脚本。部署完成后,服务会返回包含标签和概率的结果。

3. 调用服务后的响应示例

修改后调用服务,会得到类似如下的响应:

b'{"Results": [{"label": 1, "probability": 0.87}]}'

内容的提问来源于stack exchange,提问作者Shubham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:45:35