Now, the model can be used to make predictions. To do so, you need to provide the inputs (features) which include the manufacturer
, description
and price
of the product. The model will then predict three levels of categories.
Use the command type nul > inference.py
to create a new Python file.
Use the command touch inference.py
to create a new Python file.
Now open the newly created file in your preferred IDE or code editor, such as Visual Studio Code or Atom.
Copy the below code into the file and save it:
import json
import logging
import pprint
from sap.aibus.dar.client.inference_client import InferenceClient
# Show some output while the script is working
logging.basicConfig(level=logging.INFO)
MODEL_NAME = "bestbuy-category-prediction"
# Read file with service key
with open('dar_service_key.json', 'r') as sk_file:
sk_data = sk_file.read()
# Load from file
json_data = json.loads(sk_data)
inference_client = InferenceClient.construct_from_credentials(
dar_url=json_data['url'],
clientid=json_data['uaa']['clientid'],
clientsecret=json_data['uaa']['clientsecret'],
uaa_url=json_data['uaa']['url'],
)
# The code passes two objects to be classified. Each object
# must have all features described in the DatasetSchema used
# during training.
objects_to_be_classified = [
{
"objectId": "optional-identifier-1",
"features": [
{"name": "manufacturer", "value": "Energizer"},
{"name": "description", "value": "Alkaline batteries; 1.5V"},
{"name": "price", "value": "5.99"},
],
},
{
"objectId": "optional-identifier-2",
"features": [
{"name": "manufacturer", "value": "Eidos"},
{"name": "description", "value": "Unravel a grim conspiracy at the brink of Revolution"},
{"name": "price", "value": "19.99"},
],
},
]
inference_response = inference_client.create_inference_request(
model_name=MODEL_NAME,
objects=objects_to_be_classified
)
pprint.pprint(inference_response)
The above code first reads your service key and then creates a new instance of the InferenceClient
. Next, two objects are created which will be classified by the model. Feel free to modify the values or to add more objects. Please keep in mind that an object must include all inputs (features) that were defined in the dataset schema. Last but not least, the objects are sent to the model to be classified.
Now go back into the command prompt and run python inference.py
to run the code.
The results of the classification are printed in to the console:
For each object a prediction is made for all the outputs (labels) which are defined in the dataset schema. Each prediction includes the label name
and the prediction result which is comprised of the probability
and the predicted value
. The probability represents how certain the model is about its prediction whereas 1 represents 100%.
For the first object, the model is very certain about its predictions as it even predicts the third level of category with a probability of 0.99.
For the second object, the model is very certain that the first level is Video Games
but it is not so sure whether it is actually about PlayStation 3
.
Please note that a high probability does not necessarily mean that the predicted value is correct. Whether the predicted values are correct or not largely depends on the quality of the training data.
Now you have successfully run through the whole process of uploading data, training a machine learning model and using the model to make predictions.