Data keeps on growing and extracting meaningful information out of that data is very important. Using machine learning models from the existing data helps a company to not only extract meaningful insights but also predict future results.
This code pattern will demonstrate a data scientist's journey in creating a machine learning model using IBM Watson Studio and IBM Db2 On Cloud. The pattern uses Jupyter notebook to connect to the Db2 database and uses a machine learning algorithm to create a model which is then deployed to IBM Watson machine learning service. This deployed model can now be used by exposing an API and use the input data to the API to predict the home value.
- Create a Watson Studio Project on IBM Cloud.
- IBM DB2 on Cloud database stores information that will be used for machine learning and predictions.
- Watson Machine Learning helps to create ML models so that new predictions can be run against the model.
- Jupyter notebook uses IBM Db2 on Cloud and Watson Machine Learning to create the machine learning model.
- The model is exposed through an API
- Angular UI uses the API to send new data for predictions
- Clone the repo
- Create an IBM Cloud account
- Load data into IBM Db2 on Cloud
- Setup Watson studio project
- Creating and deploying a machine learning model
- Testing using UI
Before we start anything, we need to clone the repo. The repo has our dataset and python notebook which we will use when creating our model.
git clone https://github.com/IBM/predict-home-value-using-watson-machine-learning-and-db2.git
Create a free IBM on Cloud Account if you don't already have one using the following link:
Creating this account will give us access to Db2 on Cloud
and Watson Studio
services.
Now that we have created our IBM Cloud account. We need to create a Db2 on Cloud service. Once we have create that, we will then we able to load our data into our database.
Go to the dashboard of your IBM Cloud account and follow the steps to create your Db2 On Cloud service.
- In the search bar at the top of your dashboard, search
Db2
. - Although there are different database options to choose from, for the purposes of this tutorial we will be using the
Db2
option. ClickDb2
when that option appears in the search bar.
- For the service name, enter in
Data-Science-Track
. - Make sure you pick the region that is closest to where you currently reside.
- Scroll down to the
Pricing Plan
section and choose theLite
plan. - Click
Create
NOTE: You will be only able to create one instance per account.
Once you have created your database instance, we can go back to the dashboard and click on the View Resources
link under the Resource Summary
section. You should then be able to see and verify that your Db2 instance has been created under the Cloud Foundry Services
tab.
Go to the dashboard of your IBM Cloud account and follow the steps to load your data onto Db2 On Cloud service.
- In the search bar, search
Data-Science-Track
and click on your Db2 on Cloud service
- Click on
Open Console
which will direct you to the Db2 on Cloud Console. - Click on
Load
under the Hamburger menu. - Click on
browse files
and selecthome-sales-training-data.csv
from your computer. - Click
Next
.
- The next step is to decide where our data will be stored. Click on the first schema that shows up, then select
New Table
. - Enter
HOME_SALES
as our table name and selectCreate
and finallyNext
. - Make sure the column names and datatypes are correct, and click
Next
. - Click
Begin Load
.
Once the job has been completed, our data has finally been loaded into our database.
Setting up our project enivorment can be broken down in the follow steps.
- Get Db2 on Cloud credentials
- Creating Watson Studio service
- Creating a Watson Studio project
- Connect Db2 on Cloud with Watson Studio
Before we create a Watson Studio service, we need to first create credentials for our database so that Watson Studio can connect to it.
- In the search bar, search
Data-Science-Track
and click on your Db2 on Cloud service - Click on
Service Credentials
on the left hand side. - Click on
New Credentials
and thenAdd
. Make sure to save the credentials for later use.
The key information that is important for us is:
HOSTNAME
UI
PWD
DATABASE
- Go to Catalog and search for
Watson Studio
and click on that option.
- Fill out all the fields and choose 'Lite' plan.
- Click
Create
and thenGet Started
.
This will redirect you to the Watson Studio homepage.
Let's now create and setup our project.
- Select
Create a Project
and then selectStandard
. - If you haven't created object storage earlier, go to the bottom of the page and click the link
Cloud Object Storage
. Choose theLite
plan and clickCreate
. - Go back to the project page and make sure to choose the Cloud Object Storage that you have created earlier.
- Fill out the project details and click
Create
.
This will take you to your project dashboard/homepage.
- On the top of the project homepage, select
Add to project
and then clickConnection
. - Select
Db2
connection opton.
- This will take you to a connection configuration page. Here, we will enter the Db2 credentials that we got from Step 4a. Make sure to use
50000
for thePort
option. - Click
Create
once you have entered all the required information.
This will redirect you to the asset page for this project, and you should see your new Db2 connection as one of the assets.
Now that we have our database connected to our project, we need to also connect our data that is stored in our database to the project as well.
- On the top of the project homepage, select
Add to project
and then clickConnected data
. - Select
Select Source
.
- Select our database, scheme and finally our table
HOME_SALES
. - Click
Select
. - Let's name this connected data as
HOME_SALES
and then clickSelect
.
We have finally created our Watson Studio service. Within that, created a project where our database and data are connected. We can now finally start coding and building our model!
- On the top of the project homepage, select
Add to project
and then clickNotebook
.
- Fill out the notebook details
- Select
From URL
option from the tab and paste the following link to Notebook URL field:https://raw.githubusercontent.com/IBM/predict-home-value-using-watson-machine-learning-and-db2/master/notebooks/WML%20DB2.ipynb
Before we run the notebook, we need to create Watson Machine Learning
instance so that we can deploy the model to Watson Machine Learning on IBM Cloud. Here are the steps:
- Go to IBM cloud dashboard and click
Create Resource
- Search for
machine learning
and selectMachine Learning
service - Fill out the details, select
Lite
plan and clickCreate
.
- And finally Create
Service Credentials
as shown below
In the notebook, after the import
cell, add cell to create connection as shown below.
Add Watson Machine Learning service credentials that you have saved from above, at step 5 of the notebook as shown below.
Then, run all the cells. At the end of the run the model will be deployed using Watson Machine Learning on IBM Cloud so that you could use the same model to predict home value using an API exposed through Watson Machine Learning service.
- Run the code from the github URL:
https://github.com/IBM/crud-using-nodejs-and-db2
-
Replace the value of
GO_DB2_API: 'https://<url>',
insrc/enviornments/enviornment.ts
with the API URL of your go server whcih would behttp://localhost:8080/predict
-
Click
Predict
from the navigation bar on top right. -
Fill out the details, Choose
Predict using Watson Machine Learning
in Predict Options and clickSubmit
. -
Analyze the prediction result.
- Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns
This code pattern is licensed under the Apache 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 and the Apache License, Version 2.