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. We will showcase how to these products work together seamlessly to create a more enjoyable expierence.
- Clone The Repo
- Create an IBM Cloud Account
- Load Data into IBM Db2 on Cloud
- Setup Watson Studio Project
- Make Machine Learning Model
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/rohithravin/MachineLearningWithWatsonAndDB2.gitGo to the link below and create a free IBM on Cloud Account.
Creating this account will give us access to create a Db2 on Cloud and Watson Studio service.
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.
Head to the dashboard of your IBM Cloud account and the 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 the
Db2option. ClickDb2when 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 Plansection and choose theLiteplan. - Click
Create
IMPORT NOTE: In some cases you may not be able to create your db2 instance. A warning error will pop-up telling you to create a Cloud Foundary Service. Follow the directed steps to create that service and then try to create a Db2 on Cloud Service again.
Once you created your database instance, we can head 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 Foundary Services tab.
Head to the dashboard of your IBM Cloud account and the follow the steps to load your data onto Db2 On Cloud service.
- In the search bar, search
Data-Science-Trackand click on your Db2 on Cloud service
- Click on
Open Consolewhich will direct you to the Db2 on Cloud Console. - Click on
Loadunder the Hamburger menu. - Click on
browse filesand selecthome-sales-training-data.csvfrom 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. - Let's enter in
HOME_SALESas our table name and selectCreateand finallyNext. - Now we need to finailze the table, and make sure the column names are correct. Everything should we correct, so let's go ahead and click
Next. - Finally, we need to set the number of errors the load function should handle before the operation fails. For now the default number is 1000, so let's go head and 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.
- Acquiring Db2 on Cloud Credentials
- Creating Watson Studio Service
- Creating a 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-Trackand click on your Db2 on Cloud service - Click on
Service Credentialson the left hand side. - Click on
New Credentialsand thenAdd. This will create new credentials for us.
The key information that is important for us is:
HOSTNAMEUIPWDDATABASE
- Now that we have this information, we can go ahead and create our Watson service. In the search bar, search
Watson Studioand click on that option.
- Make sure all the information is correct that you have selected the
Liteplan. - Then click
Createand thenGet Started.
This will redict you to the Watson Studio homepage.
Let's now create and setup our project.
- Select
Create a Projectand then selectStandard. - Before we can create this project, we will need to create a storage service. Scroll down and click
Addunder theDefine Storagesection. Follow the instructions and create the service. Make sure your choose theLiteplane. Then come back to projct creation page and refresh it. You should see your storage service now. - Let's name the project
Data Science Track. - Then click
Create.
This will take you to your project dashboard/homepage.
Now that we have created out project, it's time to connect our project with our database we can use the data stored there for our machine learning project.
- On the top of the project homepage, select
Add to projectand then clickConnection. - This will then show us all the connections that we can have when using Watson Studio. For this project we want to use the
Db2connection opton. So lets go and click that.
- This will take you to a connection configuration page. Here we will enter in our Db2 credentials that we arquire from Step 4a. Make sure for the
Portoption, you use50000. - Click
Createonce 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 projectand 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_Salesand 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!
It's time to finally create our machine leanring model!
- On the top of the project homepage, select
Add to projectand then clickNotebook.
- You can name the notebook whatever you want :)
- You can either create a new notebook and copy the code from the notebook that is in github repo. Or you can import the notebook in this github repo into your project by using the
From Fileoption and follow along.
If you are not able to view the python notebook through github, please use this link.
IMPORTANT NOTE: In the second code snippet of the notebook, make sure you change the configuration string to import your data into your python notebook.
- 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.









