0 / 0
Quick start: Build and deploy a machine learning model in a Jupyter notebook
Quick start: Build and deploy a machine learning model in a Jupyter notebook

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:

  1. Create a project. Projects are where you can collaborate with others to work with data.
  2. Add a notebook to the project. You can create a blank notebook or import a notebook from a file or GitHub repository.
  3. Add code and run the notebook.
  4. Review the model pipelines and save the desired pipeline as a model.
  5. 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 Video 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:

  1. Create a project.
  2. Add a notebook to your project.
  3. Set up the environment.
  4. Run the notebook:
    • Build and train a model.
    • Save a pipeline as a model.
    • Deploy the model.
    • Test the deployed model.
  5. View and test the deployed model in the deployment space.

  6. (Optional) Clean up.

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.

  1. 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.
  2. Select Create an empty project.
  3. On the Create a project screen, add a name and optional description for the project.
  4. Choose an existing object storage service instance or create a new one.
  5. Click Create.
  6. When the project opens, click the Manage tab.
  7. On the Services and integrations page, click Associate service > Watson.
  8. If you don't yet have a Watson Machine Learning service instance provisioned, click New service, select Machine Learning, and click Create.
  9. Select your Watson Machine Learning instance, and click Associate service.
  10. Close the Associate service dialog box.

For more information or to watch a video, see Creating a project.
For more information on associated services, see Adding associated services.

Task 2: Add a notebook to your project

The notebook used in this tutorial is available in the Gallery.

  1. Access the Use sckit-learn to recognize hand-written digits notebook in the Gallery.
  2. Click Add to project.
  3. Select the project from the list, and click Add.
  4. Verify the notebook name and description (optional).
  5. Select a runtime environment for this notebook.
  6. Click Create. Wait for the notebook editor to load.
  7. 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 IBM Cloud credentials and Watson Machine Learning service instance location.

  1. Scroll to the Set up the environment section.
  2. Choose a method to obtain the API key and location.
  3. Paste your API key and location into cell 1.
  4. Run cells 1 and 2.
  5. To install the ibm-watson-machine-learning package, run cell 3.
  6. To import the API client and create the API client instance using your credentials, run cell 4.
  7. To see a list of all existing deployment spaces, run cell 6. If you do not have a deployment space:
    1. Open another tab with your Cloud Pak for Data deployment.
    2. From the navigation menu, click Deployments.
    3. Click New deployment space.
    4. Add a name and optional description for the deployment.
    5. Click Create, then View new space.
    6. Click the Manage tab.
    7. Copy the Space GUID and close the tab, this value will be your space_id.
  8. 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:

  1. Explore the data.
  2. Create the scikit-learn model.
    1. Prepare the data by splitting it into three data sets (train, test, and score).
    2. Create the pipeline.
    3. Train the model.
    4. Evaluate the model using the test data.
  3. Publish the model.
  4. View the model information.
  5. Create the model deployment and view the deployment details.
  6. 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.

  1. From the navigation menu, click Deployments.
  2. Select the appropriate deployment space from the list.
  3. Click the scikit model.
  4. Click the Deployment of scikit model.
  5. Review the Endpoint and Code snippets.
  6. 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]]}]}
    
  7. Click Predict. The resulting prediction indicates that the hand-written digits are 5 and 4.

(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

Now you can use this data set for further analysis. For example, you or other users can do any of these tasks:

Additional resources

Parent topic: Getting started with building, deploying, and trusting models