Search code examples
pythonazure-machine-learning-service

How to get not only predicted value but also predicted probability from a Web Service deployed in Azure ML


I have trained a classification machine learning model using auto ml which is predicting whether a person default or not and I have deployed the best model as a web service. Now I am trying to Consume the Web Service but it is giving the result as a list of 0 or 1. I also need the predicted probability.

I am using the given python code to consume my deployed Web Service.

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'))

I am getting this response.

b'{"Results": [1]}'

I want that along with the predicted label it should show the predicted probability.


Solution

  • The classification model has predict_proba method which gives you the class probabailities. You need to use that like

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

    Now, return this score as one of the output in the form of json like:

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