DISCLAIMER: This application is used for demonstrative and illustrative purposes only and does not constitute an offering that has gone through regulatory review.
Create and deploy a scoring model to predict heart failure on IBM Cloud with the Watson Data Platform
In this Code Pattern, we will use a Jupyter Notebook on IBM Watson Studio to build a predictive model that demonstrates a potential health care use case. This a customized version of the Node.js sample app that is available with the Watson Machine Learning Service on IBM Cloud See the original app for a walkthrough of the source code.
When the reader has completed this Code Pattern, they will understand how to:
- Build a predictive model within a Jupyter Notebook
- Deploy the model to IBM Watson Machine Learning service
- Access the Machine Learning model via either APIs or a Nodejs app
- The developer creates an IBM Watson Studio Workspace.
- IBM Watson Studio depends on an Apache Spark service.
- IBM Watson Studio uses Cloud Object storage to manage your data.
- This lab is built around a Jupyter Notebook, this is where the developer will import data, train, and evaluate their model.
- Import data on heart failure.
- Trained models are deployed into production using IBM's Watson Machine Learning Service.
- A Node.js web app is deployed on IBM Cloud calling the predictive model hosted in the Watson Machine Learning Service.
- A user visits the web app, enters their information, and the predictive model returns a response.
- IBM Watson Studio: Analyze data using RStudio, Jupyter, and Python in a configured, collaborative environment that includes IBM value-adds, such as managed Spark.
- Jupyter Notebook: An open source web application that allows you to create and share documents that contain live code, equations, visualizations, and explanatory text.
- PixieDust: Provides a Python helper library for IPython Notebook.
- Artificial Intelligence: Artificial intelligence can be applied to disparate solution spaces to deliver disruptive technologies.
- Data Science: Systems and scientific methods to analyze structured and unstructured data in order to extract knowledge and insights.
- Node.js: An open-source JavaScript run-time environment for executing server-side JavaScript code.
- Deploy the testing application
- Create an instance of the Watson Machine Learning Service
- Create a project in IBM Watson Studio and bind it to your Watson Machine Learning service instance
- Save the credentials for your Watson Machine Learning Service
- Create a notebook in IBM Watson Studio
- Run the notebook in IBM Watson Studio
- Deploy the saved predictive model as a scoring service using the web ui
- Deploy the saved predictive model using APIs
-
An account on IBM Watson Studio.
-
A space in IBM Cloud Dallas, London, Frankfurt, or Tokyo regions.
As of 12/14/2018, the Machine Learning service on IBM Cloud is only available in the Dallas, London, Frankfurt, or Tokyo regions.
Use Ctrl-click on the Deploy to IBM Cloud
button below to open the deployment process in a separate tab.
Note: Make sure to deploy the application to the same region and space as where the Apache Spark and Cloud Object Storage services were created when you signed up for IBM Watson Studio. Please take note of this space as later in this lab the Watson Machine Learning service needs to be deployed into the same space.
-
Click on
Deploy
to deploy the application. -
A Toolchain and Delivery Pipeline will be created for you to pull the app out of Github and deploy it in to IBM Cloud. Click on the Delivery Pipeline tile to see the status of the deployment. Wait for the Deploy Stage to complete successfully.
-
In your browser go to the IBM Cloud Dashboard and click
Catalog
. -
Search for
Machine Learning
, Verify this service is being created in the same space as the app in Step 1, and clickCreate
. -
On the Watson ML Dashboard select
Connections
on left menu panel, andCreate Connection
. Select the application that you deployed earlier in Step 1 of this lab connecting this Watson ML service to the Cloud Foundry application deployed. -
Click
Connect and restage app
when you’re prompted to restage your application. The app will take a couple of minutes to be back in therunning
state.
3. Create a project in IBM Watson Studio and bind it to your Watson Machine Learning service instance
- Sign up for IBM's Watson Studio.
- Create a new project by clicking
+ New project
and choosingData Science
:
Note: By creating a project in Watson Studio a free tier
Object Storage
service will be created in your IBM Cloud account. Take note of your service names as you will need to select them in the following steps.
Note: When creating your Object Storage service, select the
Free
storage type in order to avoid having to pay an upgrade fee.
Note: Services created must be in the same region, and space, as your Watson Studio service.
-
Enter a name for the project name and click
Create
. -
From within the new project
Overview
panel, clickAdd to project
on the top right, selectingData asset
.A panel on the right of the screen appears, select
load
and click onBrowse
to upload the data file you'll use to create a predictive model. -
On your machine, browse to the location of the file patientdataV6.csv in this repository in the data/ directory. Select the file and click on Open (or the equivalent action for your operating system).
-
Once successfully uploaded, the file should appear in the
Data Assets
section. -
Click on the
Services
pull-down at the top of the page, click onWatson services
and click `+ Add service': -
Choose your existing Machine Learning instance and click on
Select
. -
The Watson Machine Learning service is now listed as one of your
Associated Services
. -
Leave the browser tab open for later.
-
In a different browser tab go to https://cloud.ibm.com and log in to the Dashboard.
-
Click on your Watson Machine Learning instance under
Services
, click onService credentials
and then onView credentials
to see the credentials. -
Save the username, password and instance_id to a text file on your machine. You’ll need this information later in your Jupyter notebook.
-
In Watson Studio using the project you've created, click on
+ Add to project
->Notebook
OR in theAssets
tab underNotebooks
choose+ New notebook
to create a notebook. -
Select the
From URL
tab. -
Enter a name for the notebook.
-
Optionally, enter a description for the notebook.
-
Under
Notebook URL
provide the following url: https://github.com/dzwietering/watson-dojo-pm-tester/blob/master/notebooks/ML%20Predictive%20Model.ipynb -
Select the Spark runtime with Python 3.5 .
-
Click the
Create
button.
-
Place your cursor in the first code block in the notebook.
-
Click on the
Find and Add
data icon -- see step 1 in diagram below -- and then selectInsert to code
under the file patientdataV6.csv. This is step 2 in diagram below. Finally selectInsert SparkSession Data Frame
-- which is step 3 in diagram below.
Note: Make sure to rename the variable to
df_data
and add.option('inferSchema','True')\
.
-
Goto the cell that says
Stop here !!!!
during Step 5, insert the username, password, and instance_id that you saved from your Watson Machine Learning instance into the code before running it. Do the same after Step 6Save model to WML Service
. -
Click on the
Run
icon to run the code in the cell. -
Move your cursor to each code cell and run the code in it. Read the comments for each cell to understand what the code is doing. Important when the code in a cell is still running, the label to the left changes to In [*]:. Do not continue to the next cell until the code is finished running.
-
In Watson Studio go to you project, under
Assets
->Models
and click on the model you've created:Heart Failure Prediction Model
. -
Go to the
Deployments
tab and+ Add Deployment
. -
Give your Deployment a name, click
Create
, and it should show up withSTATUS
ofDEPLOY_SUCCESS
. -
Restart the Node.js Web App. For this, return to your IBM Cloud Dashboard, choose your application, and select restart from the
More action
three vertical dots
- To deploy the model using the APIs instead of using the Web UI, at Step 6.1, add the
instance_id
from yout Watson Machine Learning Service credentials. During Step 6.2, after running the second cell, get themodel_id
and put it in the cell that follows. Put thedeployment_id
in the cell underMontitor the status of deployment
. For Step 6.3, add thescoring_url
to the cell.
- In the dashboard, Click on the application name, then choose
Visit App URL
from theOverview
page to open the application in a separate tab.
-
When the application appears click on
Score now
to test the scoring model with the default values. -
Verify that the model predicts that there is a risk of heart failure for the patient with these medical characteristics.
Click Close
. Run the app again with the following parameters.
- Verify that the model predicts that there is not a risk of heart failure for the patient with these medical characteristics.
- Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
- Data Analytics Code Patterns: Enjoyed this Code Pattern? Check out our other Data Analytics Code Patterns
- AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
- With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.
- Watson Studio: Master the art of data science with IBM's Watson Studio
- Spark on IBM Cloud: Need a Spark cluster? Create up to 30 Spark executors on IBM Cloud with our Spark service
This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.