如何从部署在Azure ML中的Web服务中不仅获得预测值,还获得预测概率



我使用auto-ml训练了一个分类机器学习模型,该模型预测一个人是否违约,我已经将最佳模型部署为web服务。现在我正在尝试使用Web服务,但它给出的结果是0或1的列表。我还需要预测的概率。

我正在使用给定的python代码来使用我部署的Web服务。

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.
# More information can be found here:
# https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
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
# The azureml-model-deployment header will force the request to go to a specific deployment.
# Remove this header to have the request observe the endpoint traffic rules
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 the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(error.info())
print(error.read().decode("utf8", 'ignore'))

我得到了这样的回应。

b"{"结果":[1]}">

我希望它和预测的标签一起显示预测的概率。

分类模型具有predict_proba方法,该方法为您提供类概率。你需要像一样使用它

ypred_probabilities = classifer.predict_proba(Xtest)
ypred = classifer.predict(Xtest)
probability_score = np.max(ypred_probabilities, axis=1)

现在,以类似json的形式返回这个分数作为输出之一:

result = {
"class" : ypred,
"probability_score": probability_score
}

最新更新