Quick start: Build and deploy a machine learning model in a Jupyter notebook
Last updated: Feb 21, 2025
Quick start: Build and deploy a machine learning model in a Jupyter notebook
You can create, train, and deploy machine learning models with watsonx.ai Runtime 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
watsonx.ai Studio
watsonx.ai Runtime
Your basic workflow includes these tasks:
Open your sandbox 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
Copy link to section
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 watsonx.ai Runtime, and deploy the model.
This tutorial will take approximately 30 minutes to complete.
Sample data
Copy link to section
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.
Expand all sections
Tips for completing this tutorial Here are some tips for successfully completing this tutorial.
Use the video picture-in-picture
Copy link to section
Tip: Start the video, then as you scroll through the tutorial, the video moves to picture-in-picture mode. Close the video table of contents for the best experience with picture-in-picture. You can use picture-in-picture
mode so you can follow the video as you complete the tasks in this tutorial. Click the timestamps for each task to follow along.
The following animated image shows how to use the video picture-in-picture and table of contents features:
For the optimal experience completing this tutorial, open Cloud Pak for Data in one browser window, and keep this tutorial page open in another browser window to switch easily between the two applications. Consider arranging the two browser
windows side-by-side to make it easier to follow along.
Tip: If you encounter a guided tour while completing this tutorial in the user interface, click Maybe later.
Task 1: Open a project
You need a project to store the data and the AutoAI experiment. You can use your sandbox project or create a project.
From the Navigation Menu, choose Projects > View all projects
Open your sandbox project. If you want to use a new project:
Click New project.
Select Create an empty project.
Enter a name and optional description for the project.
Verify 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 to clear the output from the last saved run.
Check your progress
Copy link to section
The following image shows the new notebook.
Task 3: Set up the environment
To preview this task, watch the video beginning at 00:44.
The first section in the notebook sets up the environment by specifying your IBM Cloud credentials and watsonx.ai Runtime service instance location. Follow these steps to set up the environment in your notebook:
Scroll to the Set up the environment section.
Choose a method to obtain the API key and location.
Run the IBM Cloud CLI commands in the notebook from a command prompt.
Access your IBM Cloud resource list, view your watsonx.ai Runtime service instance, and note the Location.
See the watsonx.ai Runtime API Docs for the correct endpoint URL. For example, Dallas is in us-south.
Paste your API key and location into cell 1.
Click the Run icon to run your code in cells 1 and 2.
Run cell 3 to install the ibm-watson-machine-learning package.
Run cell 4 to import the API client and create the API client instance using your credentials.
Run the cell with the code client.spaces.list(limit=10) to see a list of all existing deployment spaces. If you do not have a deployment space, then follow these steps:
Open another tab with your watsonx 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.
Copy and paste the appropriate deployment space ID into the cell with the code space_id = 'PASTE YOUR SPACE ID HERE', then run that cell and the cell with the code client.set.default_space(space_id) to set
the default space.
Check your progress
Copy link to section
The following image shows the notebook with all of the environment variables set up.
Task 4: Run the notebook
To preview this task, watch the video beginning at 02:14.
Now that all of the environment variables are set up, you can run the rest of the cells in the notebook. Follow these steps to read through the comments, run the cells, and review the output:
Run the cells in the Explore data section.
Run the cells in the Create a scikit-learn model section to.
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.
Run the cells in the Persist locally created scikit-learn model section to publish the model, get model details, and get all models.
Note:
If you are using Runtime 24.1 on Python 3.11, then you will need to change the software_spec_uid to runtime-24.1-py3.11 and the scikit-learn version to scikit-learn-1.3.
Run the cells in the Deploy and score section to create the online deployment, get deployment details, and send a scoring request to the deployed model to see the prediction.
Click File > Save.
Check your progress
Copy link to section
The following image shows the notebook with the prediction.
Task 5: View and test the deployed model in the deployment space
To preview this task, watch the video beginning at 04:07.
You can also view the model deployment directly from the deployment space. Follow these steps to test the deployed model in the space.
From the Navigation Menu, click Deployments.
Click the Spaces tab.
Select the appropriate deployment space from the list.
Click Scikit model.
Click 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:
Click Predict. The resulting prediction indicates that the hand-written digits are 5 and 4.
Check your progress
Copy link to section
The following image shows the Test tab with the prediction.
(Optional) Task 6: Clean up
If you'd like to remove all of the assets created by the notebook, create a new notebook based on the Machine Learning artifacts management notebook. A link to this notebook is also available in the Clean up section of the Use scikit-learn to recognize hand-written digits notebook used in this tutorial.
Next steps
Copy link to section
Now you can use this data set for further analysis. For example, you or other users can do any of these tasks:
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.