Deploy a Machine Learning Model from a Jupyter Notebook | by Brandon Walker | Towards Data Science

To put it frankly, if you can’t get your machine learning models out of a notebook, it’s probably not going to get used. This article should help you create a deployment of your model as quickly as possible, so that you can infuse your models with business processes. This is an important skill to have since it means you won’t be relying on a software engineer/developer to assist you.
To do this we will use Watson Machine Learning, and a Jupyter Notebook. I will assume you already have Anaconda or another environment that can run notebooks. Here is an outline that we will follow, feel free to skip steps that you may have already completed:
Create an IBM Cloud account. (~2 minutes)Create a Watson Machine Learning (WML) instance. (~2 minutes)Obtain an API key. (~1 minute)Create a deployment space that can store models. (~1 minute)Create a machine learning model. (~1 minute, unless you are creating your own model)Deploy your model. (~4 minutes)Try sending your deployed model data. (~2 minutes)
Create an IBM Cloud account. (~2 minutes)
Create a Watson Machine Learning (WML) instance. (~2 minutes)
Obtain an API key. (~1 minute)
Create a deployment space that can store models. (~1 minute)
Create a machine learning model. (~1 minute, unless you are creating your own model)
Deploy your model. (~4 minutes)
Try sending your deployed model data. (~2 minutes)
Visit the registration page for IBM Cloud and follow the instructions there.
Once you’ve logged in, this should be the screen you are presented with. You can click the Catalog button in the banner at the top and scroll until you find Machine Learning, or use the search bar to find it.
After selecting Watson Machine Learning, create an free Lite instance by clicking the create button in the bottom right. Be sure to keep track of what region you have selected, for me that is Dallas.
As you see above, Watson Machine Learning comes with 20 free capacity units per month during which models can be trained, evaluated, deployed, and scored. If you are operating with a limited budget, I’d recommend training you model on your local environment, and saving your capacity units for scoring. For smaller models that won’t be called very frequently, this is probably enough.
To get a key visit the API key management page of your IBM Cloud Account. You can then click “Create an IBM Cloud API key” to make a new key, and supply a key name and description. I gave “wml-api-key” for my key’s name.
Once you’ve created your API key, copy it and paste it into your notebook so we can use it.
A Deployment Space is where you can store and manage all your deployed models. I’d recommend having one deployment space for each business purpose. Visit your deployment spaces page to create a new deployment.
From here on out, we will be writing python code in a notebook.
I’m going to keep this very simple and create a KNN model for the iris data set. While you should make something related to your business use case, this model is enough to demonstrate how to use WML. You can copy and paste the cell below if you aren’t making your own model.
import sklearnfrom sklearn.datasets import load_irisiris = load_iris()X = iris.dataY = iris.targetclf = sklearn.neighbors.KNeighborsClassifier()clf.fit(X, Y)
You will need to install the IBM Watson Machine Learning python SDK. You can do that by running pip install ibm-watson-machine-learning in the command line. You can also run this in it’s own cell in your notebook like this:!pip install ibm-watson-machine-learning.
You can start your code by importing the Watson Machine Learning package and supplying your API key and region. If you don’t know what value you should supply for location you should use check the city your instance is in. You take that city and find the region by consulting the locations page. Since my instance is in Dallas, I supplied “us-south” for the location variable below.
from ibm_watson_machine_learning import APIClientapi_key = "<your-key>"location = "<your-region>"
These can be supplied as credentials.
wml_credentials = {    "apikey": api_key,    "url": 'https://' + location + '.ml.cloud.ibm.com'}client = APIClient(wml_credentials)
We need to get the ID of your deployment space. You can do that by running the code below.
client.spaces.list()
From the output of the above you will get an ID for your deployment space. Set your APIClient to use your deployment space with the code below.
client.set.default_space("<ID>")
Before we deploy our model, we need to publish it. First we have to decide what version of python we want to use and set some metadata. If you need to figure out what version of python you are using run import sys then sys.version.
If you are also deploying a model based on sklearn, you will need to determine the version of that. You can run sklearn.__version__ to figure that out.
metadata = {    client.repository.ModelMetaNames.NAME: 'Iris KNN Model',    client.repository.ModelMetaNames.TYPE: 'scikit-learn_0.22',    client.repository.ModelMetaNames.SOFTWARE_SPEC_UID: sofware_spec_uid}published_model = client.repository.store_model(    model=clf,    meta_props=metadata,    training_data=iris.data,    training_target=iris.feature_names)
You will need to get the ID of your published model. You can get that by running the following.
models_details = client.repository.list_models()
To deploy your model use your published model’s ID from the cell above and run the code below.
metadata = {    client.deployments.ConfigurationMetaNames.NAME: "Deployment of Iris KNN Model",    client.deployments.ConfigurationMetaNames.ONLINE: {}}created_deployment = client.deployments.create("<your-published-model-id>", meta_props=metadata)
Your model is now deployed and we can now send data to it and get a response. First we need to get the ID of the deployed model by running the code below.
client.deployments.list()
We can then use that ID and send observations in a list of lists.
scoring_payload = {"input_data": [{"values": [[5.1, 3.5, 1.4, 0.2]]}]}predictions = client.deployments.score("<your-deployment-id>", scoring_payload)
In the predictions variable we now have a stored a prediction of the class and probability of each class.
If your work is going to make a large impact it has to appear somewhere other than a PowerPoint. For many data scientists a notebook is the primary development environment for machine learning models, if you don’t want to burden yourself with packaging the model and its dependencies into something that can be run on both your computer and your production server, then this is the way to go. Besides the complications of all of that, this is the simplest workflow in terms of knowledge you have to acquire to make ML models work in production, which may be the fastest way to adopt AI in your business.