Deploy scikit-learn models on IBM Cloud
Tags: watsonstudio, scikitlearn, ibm, machinelearning
Published: 2019-06-03 15:42:17

---
title: Deploy scikit-learn models on IBM Cloud
published: true
tags: watson-studio,scikit-learn,ibm,machine-learning
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*OkkUVLS2F18wOvQbJil0Tg.png)

I [wrote last week](https://dev.to/ibmdeveloper/data-science-without-much-coding-that-s-the-future-like-it-or-not-578c) on how the data science industry is moving towards a code-free landscape where **some of the mundane and metrics driven tasks are being automated and intermediate decisions are being made for the data scientists**. Don’t get me wrong. As a practicing data scientist, you still have to understand the different models, hyper parameters, and have an intuition about the whole process. You still will spend majority of your time cleaning up data and maintaining your models. Finally, until these tools get to a point where they are sophisticated enough to gain trust of data scientists, we will need a way to deploy hand written and opensource models that have been vetted by the community. A good example is the [IBM Model Asset Exchange](https://developer.ibm.com/exchanges/models?cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-ibmdev-OInfluencer-Medium-USL-ibm-max&cm_mmca1=000037FD&cm_mmca2=10010797).

So let’s look at the same model that we automatically created in the last post using Watson Studio Model Builder feature. We will instead create a linear regression model using scikit-learn and then save, deploy and manage it on IBM Cloud. To recap, I proposed the following lifecycle for a data science project …

![](https://cdn-images-1.medium.com/max/1024/1*acMwkK4ibmtbz6SIDq91gg.jpeg)

As before, we are looking at circles labeled **Model Deployment and Model Management**. I am purposely not doing any preprocessing or cleaning and skipping feature selection and model selection/scoring on the dataset for the sake of brevity. We will simply generate the simplest scikit-learn **Linear Regression** model as follows. You can grab the code from this [github repository](https://github.com/lidderupk/watson-studio-ml/blob/master/assets/housing-regression.ipynb) if you want to copy it in your project.

![](https://cdn-images-1.medium.com/max/1024/1*SGptN_fdYiNcWc85dhLE5g.png)<figcaption>Create Linear Regression Model</figcaption>

It’s not the greatest model as you can see from the metrics below. But it will do for now. The point is to be able to deploy your model.

```
Coefficients: 
 [ -1.56053402e-01 5.13699772e-02 5.82438860e-02 2.01467441e+00
 -1.80634645e+01 3.87160744e+00 -5.82508110e-03 -1.52533524e+00
 2.88260126e-01 -1.14467685e-02 -9.77473854e-01 9.87278339e-03
 -5.31623043e-01]
Mean squared error: 33.03
Variance score: 0.69
```

The next step is to save the model on IBM Cloud. The following code does exactly that …

![](https://cdn-images-1.medium.com/max/1024/1*wlSQz4VSpIrY8XUZ9KRVrQ.png)

Again, You can grab the code from this [github repository](https://github.com/lidderupk/watson-studio-ml/blob/master/assets/housing-regression.ipynb) if you want to copy it in your project. You can grab your service credentials from the Watson Machine Learning service under your account as shown here …

![](https://cdn-images-1.medium.com/max/1024/1*JqbNgOaC5OpkRawY1Eo_Ag.gif)

Finally, we will deploy the model as follows …

![](https://cdn-images-1.medium.com/max/1024/1*wtMbrInaOmPahvtONGQaFA.png)

The cell should print out **INITIALIZING** and then **DEPLOY\_SUCCESS**. It should also print out the scoring URL. I have removed it from the screenshot below.

![](https://cdn-images-1.medium.com/max/1024/1*nqe8G_YGx7OCt-wTcrtn7w.png)

That’s it ! You made it. You just deployed a scikit-learn model on IBM Cloud. It wasn’t the greatest model ever, but hey, it’s yours and your friends can use it now !

![](https://cdn-images-1.medium.com/max/600/1*XZiKlkrcEGAABkm9BsN-sw.gif)

You can now predict/score against the model as follows …

![](https://cdn-images-1.medium.com/max/1024/1*nfx8kBdjEyPekl1j-PCxpA.png)

Finally, just to prove that I am not lying, you should see the deployed model in Watson Studio.

![](https://cdn-images-1.medium.com/max/986/1*x93sJlexIUGZH4gif_yxUQ.png)

You can test the model by using the test tab inside the deployment as follows …

![](https://cdn-images-1.medium.com/max/856/1*oB4SqacuL245FHns0fKN_A.png)<figcaption>Testing the scikit-learn Linear Regression Model</figcaption>

I got the JSON input from the boston test data as follows …

```
import json
testjson = {}
testjson['fields'] = list(boston.feature\_names)
testjson['values'] = [list(X\_test[0]), list(X\_test[1])]
json.dumps(testjson)
```

Results in …

```
{"values": [[0.97617, 0.0, 21.89, 0.0, 0.624, 5.757, 98.4, 2.346, 4.0, 437.0, 21.2, 262.76, 17.31], [0.21977, 0.0, 6.91, 0.0, 0.448, 5.602, 62.0, 6.0877, 3.0, 233.0, 17.9, 396.9, 16.2]], "fields": ["CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "LSTAT"]}
```

I also used postman to test the same model. The first step was to get the token from IBM Cloud

![](https://cdn-images-1.medium.com/max/1024/1*dU0xwjzY8R-sleLmdzzQCw.png)<figcaption>Get token with POSTMAN</figcaption>

The second step is to use the scoring URL to make predictions using the token above in the Bearer Authorization.

![](https://cdn-images-1.medium.com/max/1024/1*NuU2ib9rfESabJQpT85rTw.png)<figcaption>Score model with POSTMAN</figcaption>

Stay tuned for the next article in the series. We will look at some of the opensource deep learning models at IBM and use Node-RED to test them out. They are a lot of fun !

Next Steps

1. Sign up for IBM Cloud and try it out for yourself ! -- http://bit.ly/waston-ml-sign
2. You can also look at this in depth code pattern on [Data Science Pipeline with Employee Attrition](https://developer.ibm.com/patterns/data-science-life-cycle-in-action-to-solve-employee-attrition-problem?cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-ibmdev-OInfluencer-Medium-USL-employee-attrition-model-ensemble-learning&cm_mmca1=000037FD&cm_mmca2=10010797).

Thanks to [Adam Massachi](https://medium.com/u/fe353759d0c) for hanging out at PyData LA and introducing me to WML ! Thanks to [Max Katz](https://medium.com/u/bf01a11701fe) for hosting our online meetup.