Watson Machine Learning Python client samples and examples

Review and use sample Jupyter Notebooks that use Watson Machine Learning Python library to demonstrate machine learning features and techniques. Each notebook lists learning goals so you can find the one that best meets your goals.

Training and deploying models from notebooks

If you choose to build a machine learning model in a notebook, you must be comfortable with coding in a Jupyter Notebook. A Jupyter Notebook is a web-based environment for interactive computing. 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.

Learn from sample notebooks

Many ways exist to build and train models and then deploy them. Therefore, the best way to learn is to look at annotated samples that step you through the process by using different frameworks. Review representative samples that demonstrate key features.

The samples are built by using the V4 version of the watsonx.ai Python client library.

Watch this video to learn how to train, deploy, and test a machine learning model in a Jupyter Notebook. This video mirrors the Use scikit-learn to recognize hand-written digits found in the Deployment samples table.

This video provides a visual method to learn the concepts and tasks in this documentation.

Helpful variables

Use the pre-defined PROJECT_ID environment variable to call the watsonx.ai Python client APIs. PROJECT_ID is the guide of the project where your environment is running.

Deployment samples

View or run these Jupyter Notebooks to see how techniques are implemented by using various frameworks. Some of the samples rely on trained models, which are also available for you to download from the public repository.

Sample name Framework Techniques demonstrated
Use scikit-learn and custom library to predict temperature Scikit-learn Train a model with custom defined transformer
Persist the custom-defined transformer and the model in Watson Machine Learning repository
Deploy the model by using Watson Machine Learning Service
Perform predictions that use the deployed model
Use scikit-learn and AI lifecycle capabilities to predict California house prices with ibm-watsonx-ai Scikit-learn Download an externally trained scikit-learn model with dataset
Persist an external model in Watson Machine Learning repository
Deploy model for online scoring using client library
Score sample records using client library
Update previously persisted model
Redeploy model in-place
Scale deployment
Use PMML to predict iris species PMML Deploy and score a PMML model
Use Python function to recognize hand-written digits Python Use a function to store a sample model, then deploy the sample model.
Use scikit-learn to recognize hand-written digits Scikit-learn Train sklearn model
Persist trained model in Watson Machine Learning repository
Deploy model for online scoring by using client library
Score sample records by using client library
Use Spark and batch deployment to predict customer churn Spark Load a CSV file into an Apache Spark DataFrame
Explore data
Prepare data for training and evaluation
Create an Apache Spark machine learning pipeline
Train and evaluate a model
Persist a pipeline and model in Watson Machine Learning repository
Explore and visualize prediction result by using the plotly package
Deploy a model for batch scoring by using Watson Machine Learning API
Use Spark and Python to predict Credit Risk Spark Load a CSV file into an Apache® Spark DataFrame
Explore data
Prepare data for training and evaluation
Persist a pipeline and model in Watson Machine Learning repository from tar.gz files
Deploy a model for online scoring by using Watson Machine Learning API
Score sample data by using the Watson Machine Learning API
Explore and visualize prediction results by using the plotly package.
Use SPSS to predict customer churn SPSS Work with the instance
Perform an online deployment of the SPSS model
Score data by using deployed model
Use XGBoost to classify tumors XGBoost Load a CSV file into numpy array
Explore data
Prepare data for training and evaluation
Create an XGBoost machine learning model
Train and evaluate a model
Use cross-validation to optimize the model's hyperparameters
Persist a model in Watson Machine Learning repository
Deploy a model for online scoring
Score sample data
Use Keras to recognize hand-written digits.ipynb Keras (Tensorflow) Download an externally trained Keras model with dataset.
Persist an external model in the Watson Machine Learning repository.
Deploy a model for online scoring by using client library.
Score sample records by using client library.
Machine Learning artifact management Core Export and import artifacts
Load, deploy, and score externally created models
Use custom image, software specification and runtime definition to deploy a python function with ibm-watsonx-ai Python Creating a Custom Image
Creating custom software specification and runtime definition
Creating an online deployment for Python function
Scoring data using deployed function
Use R Shiny app to create SIR model R Persist an R Shiny app in Watson Machine Learning asset repository.
Deploy an application for online scoring by using client library.
Score sample records by using client library.
Use watsonx, and meta-llama/llama-3-1-8b-instruct to run as an AI service Python Setup
Create AI service
Testing AI service's function locally
Deploy AI service
Example of Executing an AI service
Summary
Use watsonx, and meta-llama/Meta-Llama-3-8B to Fine Tune with online banking queries annotated Python Setup
Data loading
Initialize experiment
Run Fine Tuning
Fine Tuning details
List historical Fine Tuning experiments
Deploy Tuned Model
Preparing data for testing process
Foundation Models Inference on watsonx.ai
Deploy Base Model
Comparison of prediction results
Clean up
Summary and next steps

AutoAI samples

View or run these Jupyter Notebooks to see how AutoAI model techniques are implemented.

Sample name Framework Techniques demonstrated
Use AutoAI and Lale to predict credit risk Hybrid (AutoAI) with Lale Work with Watson Machine Learning experiments to train AutoAI models.
Compare trained models quality and select the best one for further refinement
Refine the best model and test new variations
Deploy and score the trained model
Use AutoAI RAG and Chroma to create a pattern and get information from ibm-watsonx-ai SDK documentation Setup
RAG Optimizer definition
RAG Experiment run
RAG Patterns comparison and testing
Historical runs
Clean up
Summary and next steps
Use AutoAI RAG and Milvus database to work with ibm-watsonx-ai SDK documentation Setup
RAG Optimizer definition
RAG Experiment run
RAG Patterns comparison and testing
Historical runs
Clean up
Summary and next steps

More samples

View or download sample notebooks from the GitHub repository to use as a model for your tasks. Most of the notebooks are annotated to describe key actions and features.

Next steps

Parent topic: Managing predictive deployments