0% found this document useful (0 votes)
133 views24 pages

Exercise 4. Getting Started With Watson Machine Learning: Estimated Time

Uploaded by

Maram Gallaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views24 pages

Exercise 4. Getting Started With Watson Machine Learning: Estimated Time

Uploaded by

Maram Gallaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Exercise 4.

Getting started with


Watson Machine Learning
Estimated time
01:00

Overview
This exercise introduces you to the basic tasks that you must perform while you import and deploy
machine learning models by using the Watson Machine Learning service and Watson Studio.

Objectives
After completing this exercise, you should be able to:
• Add a machine learning model saved in Predictive Model Markup Language (PMML) format to
the project.
• Deploy the machine learning model.
• Call the deployed model as a service.
• Test the machine learning model.
• Create a model by using the AutoAI graphical tool in Watson Studio.

Introduction
In this exercise, you follow the end-to-end process to import, deploy, and test a PMML machine
learning model. You also learn how to call the model that is deployed as a web service from your
applications.
PMML is the de facto standard language that is used to represent data mining models. You can
read more about PMML here. PMML stands for Predictive Model Markup Language, it is XML-
based. It allows you to easily share predictive analytic models between different applications.
Therefore, you can train a model in one system, express it in PMML, and move it to another
system where you can use it for predictions.
This exercise uses a PMML formatted model trained by using the Iris flower dataset. A similar
exercise that uses a Jupyter Notebook and is named Use PMML to predict iris species can be
found in the Watson Studio Gallery.
In the second part of this exercise, you follow a tutorial on the web to learn how to use the new
AutoAI tool in Watson Studio. You can use the AutoAI tool to build machine learning models with
your own data. AutoAI automatically detects the best model for your data. Machine learning
models that are produced by AutoAI can also be deployed and tested as any other machine
learning model in Watson Studio.

© Copyright IBM Corp. 2019 1


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Requirements
Complete Exercise 3. Getting Started with Watson Studio before you perform this exercise.

© Copyright IBM Corp. 2019 2


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise instructions
In this exercise, you are guided step-by-step to use Watson Studio to import and use PMML
machine learning models. You are also introduced to AutoAI.
In this exercise, you complete the following tasks:
__ 1. Import, deploy, and test a PMML machine learning model with IBM Watson Studio and IBM
Watson Machine Learning.
__ a. Add a PMML machine learning model to the project.
__ b. Deploy the machine learning model.
__ c. Call the deployed model as a service.
__ d. Test the machine learning model.
__ 2. Create a machine learning model with IBM Watson Studio and the AutoAI tool.

Part 1. Importing, deploying, and testing a PMML model with IBM


Watson Machine Learning
In this part, you complete the following tasks:
__ 1. Add a PMML machine learning model to the project.
__ 2. Deploy the trained machine learning model.
__ 3. Call the deployed model as a service.
__ 4. Test the machine learning model.

Adding a PMML machine learning model to the project


In this section, you use a model in PMML format that was trained by using the Iris flower dataset.
The data set consists of 150 samples, 50 samples from each of the Iris species, which are: Iris
Setosa, Iris Virginica, and Iris Versicolor).
Four features were measured from each sample: the length and width of the sepals, and the
length and width of the petals, in centimeters. The model must detect the species of a particular
flower based on the input features. This problem is a classification problem. The PMML model is a
decision tree model that is built by using the CHAID algorithm on the Iris dataset.
Perform the following steps:
__ 1. Log in to IBM Cloud and open the Dashboard
__ 2. Click the Watson Studio service instance that you created previously.

© Copyright IBM Corp. 2019 3


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Note: Take note of the location in which your IBM Watson Studio service instance is located. In
this example, the location is Dallas.

__ 3. Click Get started to start Watson Studio.

__ 4. Select the project that you previously created in Exercise 3.

__ 5. From your project’s Assets tab, click + Add to project

© Copyright IBM Corp. 2019 4


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 6. Choose Watson Machine Learning Model.

__ 7. Enter the model name, MyPMMLModel, optionally a description and click Associate a
Machine Learning service instance.

© Copyright IBM Corp. 2019 5


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Note: You can associate an existing Machine Learning service instance with your project or create
a new one. Since this is your first Watson Machine Learning service instance, create a new one.

__ 8. In the Machine Learning service page, make sure that the New tab is selected, select the
Lite plan, scroll-down, and click Create.

__ 9. At the Confirm Creation prompt, accept the default values and click Confirm.

Important: You can associate a Watson Machine Learning service instance with your Watson
Studio project only when the Watson Machine Learning service instance and the Watson Studio
instance are located in the same region. Ensure that the region for both Watson Studio and
Watson Machine Learning match. If the location for the Watson Studio service instance is Dallas,
the region must be Dallas; if the location is London, the region must be London.
.

© Copyright IBM Corp. 2019 6


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 10. Click Reload to refresh the page. The Machine Learning service instance that you just
created is displayed.

__ 11. Download the sample PMML file from here.

© Copyright IBM Corp. 2019 7


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Information: You can open the iris_chaid.xml file that you downloaded, which contains the PMML
model. Notice the structure of the file and its elements, for example:
DataDictionary: Identifies and defines the input data fields. These fields can include numerical,
ordinal, and categorical fields. In the current model, there are five fields. Four of the fields have
continuous values that represent the petal and sepal width and length. One categorical field
represents the species, which is used as the target field.
MiningSchema: Defines the strategies for handling missing and outlier values.
ModelDefinition: Defines the structure and the parameters that are used to build the model. PMML
covers various statistical techniques. For a decision tree, it defines all the tree nodes and also
simple and compound predicates.
.

__ 12. Select From file, browse to the PMML file that you downloaded, and click Import.

__ 13. After you import the model, a summary of the model is displayed.

© Copyright IBM Corp. 2019 8


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
In this page you can see information about the model, for example, the date that the model
was trained, the label column (Species) and the Input Schema.

Deploying the trained machine learning model


Now it is time to deploy the trained machine learning model as a web service. The model is
exposed as an API and applications can call the API and use the model to make predictions.
Perform the following steps:
__ 1. Click the Deployments tab.
__ 2. Click Add Deployment +.

© Copyright IBM Corp. 2019 9


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 3. Enter a name for the deployment, for example, MyPMMLdeployment, optionally a
description, and select Web service as deployment type.

__ 4. Click Save.
At this point, the MyPMMLModel prediction model is deployed as a web service in IBM
Cloud.
__ 5. After the model is successfully deployed, the status changes to DEPLOY_SUCCESS as
shown in the following figure. Refresh the page if the status takes too long to change.

© Copyright IBM Corp. 2019 10


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 6. Click the three dots under the Actions section and then click View.

The following page shows the properties of the deployed web service.

© Copyright IBM Corp. 2019 11


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Calling the deployed model as a service
Watson Studio provides code snippets to help you integrate the model that is deployed as a
web service with your applications. With this approach, you can use the deployed model
outside of Watson Studio. Perform the following steps:
__ 1. Click the Implementation tab.

© Copyright IBM Corp. 2019 12


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 2. Notice the Code Snippets section. Click the tab for the language of your choice, for
example Python, and review the code snippet. You can copy the code to your code editor
and include a call to the prediction model web service in your applications. Use cURL in
this exercise.
__ 3. To call the API, you must retrieve credentials from the Watson Machine Learning service
that was created in the section Adding a machine learning model to the project. Perform
the following steps to retrieve the credentials:
__ a. Click Services > right click Watson Services > Open link in new tab.

© Copyright IBM Corp. 2019 13


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ b. Locate the Machine Learning service that you previously created and click Actions >
Manage in IBM Cloud.

© Copyright IBM Corp. 2019 14


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ c. Click Service credentials > View credentials. Scroll-down to display the credentials in
JSON format.

© Copyright IBM Corp. 2019 15


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ d. Click the Copy to clipboard icon and paste the credentials in a text editor of your
choice. Example:
{
"apikey": "MlqNG80Q8Od869VGzQ9k15siFzwLJz9a8hqeWO2ZftaA",
"iam_apikey_description": "Auto-generated for key d3e2cb00-7305-41e4-
bfd2-f489bae6483d",
"iam_apikey_name": "wdp-writer",
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
"iam_serviceid_crn": "crn:v1:bluemix:public:iam-
identity::a/1bfc79d1e439444fa906561ac47d5105::serviceid:ServiceId-082785ba-
4b72-4a83-8bee-a346b0a79510",
"instance_id": "3d24c49a-c471-40b3-a592-d85feb73f13a",
"url": "https://us-south.ml.cloud.ibm.com"
}
The fields of interest are “apikey” and “instance_id”, and their respective values.
__ 4. Now that you have the credentials, you can make calls to your model.
First, retrieve the IAM token, which is an authentication token that is required to access
IBM Cloud services.
You can obtain the token by running the following cURL command. Replace the $API_KEY
placeholder with the “apikey” field value from the credentials of your service.

© Copyright IBM Corp. 2019 16


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
curl -X POST \
'https://iam.cloud.ibm.com/identity/token' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=urn:ibm:params:oauth:grant-type:apikey' \
-d 'apikey=$API_KEY'

For Example:
curl -X POST \
'https://iam.cloud.ibm.com/identity/token' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=urn:ibm:params:oauth:grant-type:apikey' \
-d 'apikey=MlqNG80Q8Od869VGzQ9k15siFzwLJz9a8hqeWO2ZftaA'

Note: For more information about this command, see Create an IAM access token for a user or
service ID at https://cloud.ibm.com/apidocs/iam-identity-token-api#create-an-iam-access-token-for-
a-user-or-service-i

__ 5. Run the previous cURL command in Git bash. The access token is returned along with
other parameters.

© Copyright IBM Corp. 2019 17


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 6. Go through the output and extract only the value for the “access_token” field. Disregard
the other parameters such as “refresh token”, “token_type”, and the others.
__ 7. Copy the token and paste it in a text editor. Example:
{"access_token":"eyJraWQiOiIyMDE5MDUxMyIsImFsZyI6IlJTMjU2In0.eyJpYW1faWQiOi
JpYW0tU2VydmljZUlkLTA4Mjc4NWJhLTRiNzItNGE4My04YmVlLWEzNDZiMGE3OTUxMCIsImlkI
joiaWFtLVNlcnZpY2VJZC0wODI3ODViYS00YjcyLTRhODMtOGJlZS1hMzQ2YjBhNzk1MTAiLCJy
ZWFsbWlkIjoiaWFtIiwiaWRlbnRpZmllciI6IlNlcnZpY2VJZC0wODI3ODViYS00YjcyLTRhODM
tOGJlZS1hMzQ2YjBhNzk1MTAiLCJzdWIiOiJTZXJ2aWNlSWQtMDgyNzg1YmEtNGI3Mi00YTgzLT
hiZWUtYTM0NmIwYTc5NTEwIiwic3ViX3R5cGUiOiJTZXJ2aWNlSWQiLCJhY2NvdW50Ijp7InZhb
GlkIjp0cnVlLCJic3MiOiIxYmZjNzlkMWU0Mzk0NDRmYTkwNjU2MWFjNDdkNTEwNSJ9LCJpYXQi
OjE1NjY5ODI2NjgsImV4cCI6MTU2Njk4NjI2OCwiaXNzIjoiaHR0cHM6Ly9pYW0uYmx1ZW1peC5
uZXQvaWRlbnRpdHkiLCJncmFudF90eXBlIjoidXJuOmlibTpwYXJhbXM6b2F1dGg6Z3JhbnQtdH
lwZTphcGlrZXkiLCJzY29wZSI6ImlibSBvcGVuaWQiLCJjbGllbnRfaWQiOiJkZWZhdWx0IiwiY
WNyIjoxLCJhbXIiOlsicHdkIl69.IxJqDWVx-XFMJC_078Gz1ye67LtD6f6JCHQdDo-
8AIajIaA_bVg-Q5HiB94_5Q35AMy2j9B3-
2O097FwJOVD1v1vXTeLFodAGTXghUF3tK1rEVOP3dx21yy8JlYqAx6RR3xyo-
O3BtawU76YoFUfN66im6IPBAr6V43X4dLGnGGxNexJLbPSehPPZehI4DyvQ5f7K3Eo3e5ltPcMc
fLFtKTjP1Aqfxz-
TcC1QjFLx2LBPbv53TIRTQWnF1v6vBxxxfEiNQyCY_YJ6WkRWEfrvd9rfUfAMI80As2Ufe-
1lJdxG3EiXgv_b0pgWxBpFLx1Kp7O3_wukrhwxoCXprMxvg"}

Note: The auth token expires after a short time. Run the next cURL command shortly after you
retrieve the token.

© Copyright IBM Corp. 2019 18


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 8. Return to the deployment Implementation tab, select the cURL tab under Code Snippet,
copy the cURL command, and run it on Git Bash.
Replace $IAM_TOKEN by the value of the token (“access_token”) that you copied
previously.
Replace $ML_INSTANCE_ID by the Machine learning instance ID, which is the value of
the “instance_id” field that you copied in from the service credentials.
$ARRAY_OF_VALUES_TO_BE_SCORED contains the values that you send to your
model for scoring. Replace these placeholders with this array [[0.2, 3.5, 1.4]]

Note: You can change the input values as you want to test different inputs to your model.

curl -X POST --header 'Content-Type: application/json' --header 'Accept:


application/json' --header "Authorization: Bearer $IAM_TOKEN" --header "ML-
Instance-ID: $ML_INSTANCE_ID" -d '{"fields": ["Petal.Width", "Sepal.Width",
"Petal.Length"],"values": [$ARRAY_OF_VALUES_TO_BE_SCORED,
$ANOTHER_ARRAY_OF_VALUES_TO_BE_SCORED] }' https://us-
south.ml.cloud.ibm.com/v3/wml_instances/3d24c49a-c471-40b3-a592-
d85feb73f13a/deployments/b9d608c6-2b64-445e-a7e8-c0881c63387c/online

For Example:
curl -X POST --header 'Content-Type: application/json' --header 'Accept:
application/json' --header "Authorization: Bearer
eyJraWQiOiIyMDE5MDUxMyIsImFsZyI6IlJTMjU2In0.eyJpYW1faWQiOiJpYW0tU2VydmljZUl
kLTA4Mjc4NWJhLTRiNzItNGE4My04YmVlLWEzNDZiMGE3OTUxMCIsImlkIjoiaWFtLVNlcnZpY2
VJZC0wODI3ODViYS00YjcyLTRhODMtOGJlZS1hMzQ2YjBhNzk1MTAiLCJyZWFsbWlkIjoiaWFtI
iwiaWRlbnRpZmllciI6IlNlcnZpY2VJZC0wODI3ODViYS00YjcyLTRhODMtOGJlZS1hMzQ2YjBh
Nzk1MTAiLCJzdWIiOiJTZXJ2aWNlSWQtMDgyNzg1YmEtNGI3Mi00YTgzLThiZWUtYTM0NmIwYTc
5NTEwIiwic3ViX3R5cGUiOiJTZXJ2aWNlSWQiLCJhY2NvdW50Ijp7InZhbGlkIjp0cnVlLCJic3
MiOiIxYmZjNzlkMWU0Mzk0NDRmYTkwNjU2MWFjNDdkNTEwNSJ9LCJpYXQiOjE1NjY5ODI2NjgsI
mV4cCI6MTU2Njk4NjI2OCwiaXNzIjoiaHR0cHM6Ly9pYW0uYmx1ZW1peC5uZXQvaWRlbnRpdHki
LCJncmFudF90eXBlIjoidXJuOmlibTpwYXJhbXM6b2F1dGg6Z3JhbnQtdHlwZTphcGlrZXkiLCJ
zY29wZSI6ImlibSBvcGVuaWQiLCJjbGllbnRfaWQiOiJkZWZhdWx0IiwiYWNyIjoxLCJhbXIiOl
sicHdkIl19.IxJqDWVx-XFMJC_078Gz1ye69LtD6f6JCHQdDo-8AIajIaA_bVg-
Q5HiB94_5Q35AMy2j9B3-
2O097FwJOVD1v1vXTeLFodAGTXghUF3tK1rEVOP3dx21yy8JlYqAx6RR3xyo-
O3BtawU76YoFUfN66im6IPBAr6V43X4dLGnGGxNexJLbPSehPPZehI4DyvQ5f7K3Eo3e5ltPcMc
fLFtKTjP1Aqfxz-
TcC1QjFLx2LBPbv53TIRTQWnF1v6vBxxxfEiNQyCY_YJ6WkRWEfrvd9rfUfAMI80As2Ufe-
1lJdxG3EiXgv_b0pgWxBpFLx1Kp7O3_wukrhwxoCXprMxvg" --header "ML-Instance-ID:
3d24c49a-c471-40b3-a592-d85feb73f13a" -d '{"fields": ["Petal.Width",
"Sepal.Width", "Petal.Length"],"values": [[0.2, 3.5, 1.4]]}' https://us-
south.ml.cloud.ibm.com/v3/wml_instances/3d24c49a-c471-40b3-a592-
d85feb73f13a/deployments/b9d608c6-2b64-445e-a7e8-c0881c63387c/online
__ 9. The score results for the array of input values are returned:

© Copyright IBM Corp. 2019 19


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
{
"fields": [["$R-Species", "$RC-Species", "$RP-Species", "$RP-setosa", "$RP-
versicolor", "$RP-virginica", "$RI-Species"]],
"values": [["setosa", 1.0, 1.0, 1.0, 0.0, 0.0, "1"]]
}

Information: The score results consist of various fields as follows:


"$R-Species": Represents the predicted class of Iris, which can be “setosa”, “versicolor”, or
“virginica”.
"$RC-Species": Represents the predicted class probability.
"$RP-setosa", "$RP-versicolor", "$RP-virginica": Each field represents each class
probability.
"$RI-Species": The rule identifier from the decision tree that is used for prediction.

__ 10. In the deployment page, click View API Specification to display the API information.

__ 11. Browse through the web page IBM Watson Machine Learning API to the review the API
documentation.

Testing the machine learning model


You can test the model predictions by entering different values to score in the deployment Test
tab.

© Copyright IBM Corp. 2019 20


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
__ 1. Click the Test tab. Enter input data as shown in the following figure and click Predict. The
values might be different in your situation.

The test results show that a flower with these features is more likely to be Iris-Setosa. View the
iris-dataset in the Gallery page by searching for “Iris” and test several values from the dataset.
__ 2. Change the input data to test different predictions and view the results.

Part 2. Creating a machine learning model with IBM Watson Studio and
the AutoAI tool
The AutoAI graphical tool in Watson Studio automatically analyzes your data and generates
candidate model pipelines that are customized for your predictive modeling problem. These model
pipelines are created over time as AutoAI algorithms learn more about your dataset and discover
data transformations, estimator algorithms, and parameter settings that work best for your problem

© Copyright IBM Corp. 2019 21


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
setting. Results are displayed on a leaderboard, showing the automatically generated model
pipelines ranked according to your problem optimization objective.

In this part, follow the tutorial AutoAI tutorial: Build a binary classification model. This tutorial
guides you through training a model to predict whether a customer is likely to buy a tent from an
outdoor equipment store. In this tutorial, you create an AutoAI experiment in IBM Watson Studio to
build a model. This model analyzes your data and selects the best model type and algorithms to
produce, train, and optimize pipelines, which are model candidates. After you review the pipelines,
you save one as a model, deploy it, and then test it to get a prediction.

End of exercise

© Copyright IBM Corp. 2019 22


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise review and wrap-up
In this exercise you learned how to use Watson Studio to import a PMML machine learning model,
deploy, and test the model.
You called the model that was deployed as a web service. You used the machine learning API to
integrate the prediction model in your applications.
You were introduced the new Watson Studio tool AutoAI that you can use to build machine
learning models by using your own data.

© Copyright IBM Corp. 2019 23


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
References
From PMML model to online scoring with api client
What is PMML?
IBM Watson Machine Learning
Create and deploy a machine learning model without coding
Deep learning models using Watson Studio Neural Network Modeler and Experiments
Overview: Watson Machine Learning
Creating an AutoAI experiment from sample data
AutoAI Overview

© Copyright IBM Corp. 2019 24


Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

You might also like