Test the model using the Watson Machine Learning API Client¶

Prerequisites¶

This notebook assumes a deployed binary classification model that predicts the possibility for a tent purchase based on age, sex, marital status and job profession for an individual. To create the model, follow these steps:

  1. Access the GoSales data set in the Resource Hub.
  2. Create an AutoAI experiment using the GoSales data set as the training data.
  3. Save the best performing pipeline in the AutoAI experiment as a model.
  4. Deploy the saved model.

Task 1. Retrieve your credentials, space ID, and deployment ID.¶

To run the code samples in this notebook, you must supply information, such as the apikey, and deployment ID for the scoring endpoint. The details on how to do this are described in the following steps:

  1. Log in to your IBM Cloud account and, from the menu bar, select Manage->Access (IAM)->API Keys.
  2. Create a new API key and copy the key, and paste that in first cell.
  3. To find the scoring endpoint, log in to Cloud Pak for Data as a Service.
  4. On the Deployments tab, open the model deployment.
  5. Copy the Deployment ID from the Information side bar, and paste that in the second cell

After updating the first two code cells with your information, run those cells.

In [ ]:
# Add API credentials to access your Watson Machine Learning service instance 
wml_credentials={
  "url": "https://us-south.ml.cloud.ibm.com",
  "apikey": "insert_your_apikey_here",
}
In [ ]:
# Add the deployment ID from your model deployment from the scoring endpoint
scoring_endpoint = 'insert_your_deployment_ID_here'

Task 2. Import the appropriate libraries and create the WML API client¶

  1. Import the WML API client from the IBM Watson Machine Learning package.
  2. Create the API client with your credentials.
  3. In Cloud Pak for Data as a Service, click Deployment spaces and from the list, select the deployment space containing the deployment model.
  4. Click the Settings tab, copy the Space ID, and paste in the appropriate cell below.
In [ ]:
# Import the installed WML API client from the ibm_watson_machine_learning python package
from ibm_watson_machine_learning import APIClient
In [ ]:
# Create API client
client = APIClient(wml_credentials)
In [ ]:
# Set the deployment space
client.set.default_space('insert_your_deployment_space_ID_here')

Task 3: Call the scoring endpoint to test the model¶

  1. Assign a variable with payload data to use for testing.
  2. Test the model using the payload data.

The result should show that the prediction is TRUE for a Male, 20-year-old, Single, Professional, who previously purchased camping equipment in the amount of $144.78 to buy a tent, and the probability of that event is very high.

In [ ]:
# Call the scoring endpoint with some initial sample data and output the prediction and probability
payload_scoring = {"input_data":[{"fields":["GENDER","AGE","MARITAL_STATUS","PROFESSION","PRODUCT_LINE","PURCHASE_AMOUNT"],"values":[["M",20,"Single","Professional","Camping Equipment",144.78]]}]}
client.deployments.score( scoring_endpoint, payload_scoring )

© Copyright 2024 IBM Corporation.