Quick start: Build and deploy a machine learning model in a Jupyter notebook
You can create, train, and deploy machine learning models with Watson Machine Learning in a Jupyter notebook. Read about the Jupyter notebooks, then watch a video and take a tutorial that’s suitable for intermediate users and requires coding.
Required services Watson Studio Watson Machine Learning
Your basic workflow includes these tasks:
- Create a project. Projects are where you can collaborate with others to work with data.
- Add a notebook to the project. You can create a blank notebook or import a notebook from a file or GitHub repository.
- Add code and run the notebook.
- Review the model pipelines and save the desired pipeline as a model.
- Deploy and test your model.
Read about Jupyter notebooks
A Jupyter notebook is a web-based environment for interactive computing. If you choose to build a machine learning model in a notebook, you should be comfortable with coding in a Jupyter notebook. You can run small pieces of code that process your data, and then immediately view the results of your computation. Using this tool, you can assemble, test, and run all of the building blocks you need to work with data, save the data to Watson Machine Learning, and deploy the model.
Read more about training models in notebooks
Learn about other ways to build models
Watch a video about creating a model in a Jupyter notebook
Watch this video to see how to train, deploy, and test a machine learning model in a Jupyter notebook.
This video provides a visual method as an alternative to following the written steps in this documentation.
Try a tutorial to create a model in a Jupyter notebook
In this tutorial, you will complete these tasks:
- Create a project.
- Add a notebook to your project.
- Set up the environment.
- Run the notebook:
- Build and train a model.
- Save a pipeline as a model.
- Deploy the model.
- Test the deployed model.
- View and test the deployed model in the deployment space.
This tutorial will take approximately 30 minutes to complete.
Sample data
The sample data used in this tutorial is from data that is part of scikit-learn and will be used to train a model to recognize images of hand-written digits, from 0-9.
Task 1: Create a project
You need a project to store the notebook.
- If you have an existing project, open it. If you don't have an existing project, click Create a project on the home page or click New project on your Projects page.
- Select Analytics project as the project type.
- Select Create an empty project.
- On the Create a project screen, add a name and optional description for the project.
- Click Create.
For more information or to watch a video, see Creating a project.
Task 2: Add a notebook to your project
- Click New asset > Notebook.
- Select the From URL tab.
- Paste this link (https://raw.githubusercontent.com/IBM/watson-machine-learning-samples/master/cpd4.0/notebooks/python_sdk/deployments/scikit-learn/Use%20scikit-learn%20to%20recognize%20hand-written%20digits.ipynb) in the Notebook URL field.
- Add the notebook name and description (optional).
- Select a runtime environment for this notebook.
- Click Create. Wait for the notebook editor to load.
- From the menu, click Kernel > Restart & Clear Output, then confirm by clicking Restart and Clear All Outputs.
Task 3: Set up the environment
The first section in the notebook sets up the environment by specifying your Cloud Pak for Data credentials and Watson Machine Learning service instance location.
- Scroll to the Set up the environment section.
- Obtain your API key. If you do not have an API key follow these steps:
- Open another tab with your Cloud Pak for Data deployment.
- Click on your profile icon.
- Click Profile and Settings.
- Click API Key.
- Click Generate new key.
- Copy your API key and close the tab.
- Obtain you platform url from you Red Hat OpenShift console (Red Hat OpenShift Container Platform -> Projects -> cpd-instance -> Routes).
- Paste your
username
,api_key
and platformurl
into cell 1. - Run cells 1 and 2.
- To install the
ibm-watson-machine-learning
package, run cell 3. - To import the API client and create the API client instance using your credentials, run cell 4.
- To see a list of all existing deployment spaces, run cell 6. If you do not have a deployment space:
- Open another tab with your Cloud Pak for Data deployment.
- From the navigation menu, click Deployments.
- Click New deployment space.
- Add a name and optional description for the deployment.
- Click Create, then View new space.
- Click the Manage tab.
- Copy the Space GUID and close the tab, this value will be your
space_id
.
- To set the default space, copy and paste the appropriate deployment space ID into cell 5, then run cell 5 and cell 7.
Task 4: Run the notebook
Read through the comments and run the cells to:
- Explore the data.
- Create the scikit-learn model.
- Prepare the data by splitting it into three data sets (train, test, and score).
- Create the pipeline.
- Train the model.
- Evaluate the model using the test data.
- Publish the model.
- View the model information.
- Create the model deployment and view the deployment details.
- Score the model which sends a scoring request to the deployed model and shows the prediction.
Task 5: View and test the deployed model in the deployment space
You can also view the model deployment directly from the deployment space.
- From the navigation menu, click Deployments.
- Select the appropriate deployment space from the list.
- Click the scikit model.
- Click the Deployment of scikit model.
- Review the Endpoint and Code snippets.
-
Click the Test tab. You can test the deployed model by pasting the following JSON code:
{"input_data": [{"values": [[0.0, 0.0, 5.0, 16.0, 16.0, 3.0, 0.0, 0.0, 0.0, 0.0, 9.0, 16.0, 7.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.0, 15.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 15.0, 16.0, 15.0, 4.0, 0.0, 0.0, 0.0, 0.0, 9.0, 13.0, 16.0, 9.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 14.0, 12.0, 0.0, 0.0, 0.0, 0.0, 5.0, 12.0, 16.0, 8.0, 0.0, 0.0, 0.0, 0.0, 3.0, 15.0, 15.0, 1.0, 0.0, 0.0], [0.0, 0.0, 6.0, 16.0, 12.0, 1.0, 0.0, 0.0, 0.0, 0.0, 5.0, 16.0, 13.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 5.0, 15.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.0, 15.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 13.0, 13.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.0, 16.0, 9.0, 4.0, 1.0, 0.0, 0.0, 3.0, 16.0, 16.0, 16.0, 16.0, 10.0, 0.0, 0.0, 5.0, 16.0, 11.0, 9.0, 6.0, 2.0]]}]}
-
Click Predict. The resulting prediction indicates that the hand-written digits are 5 and 4.
Next steps
Now you can use this data set for further analysis. For example, you or other users can do any of these tasks:
Additional resources
- Try these other methods to build models:
- View more videos for machine learning
- Find more Python client samples and examples.
Parent topic: Getting started with building, deploying, and trusting models