Deploying our first Machine Learning model using Titan

Titan Tutorial #2: Getting started with ML

In our previous post, we talked about how to deploy a very simple “Hello world” service, using Titan for transforming a piece of Python code into an API endpoint ready to be consumed.

  • Linear Regression from sklearn (A complete toolbox for data analysis)
import pandas as pd
from sklearn.linear_model import LinearRegression
import json
# Reading the dataset from a Gitlab repo
url = "https://gitlab.com/jfuentesibanez/datasets/raw/master/regression_tutorial/advertising.csv"
df = pd.read_csv(url)
# Data exploration
df.head()
+ — — — -+ — — — -+ — — — — — -+ — — — -+
| TV | Radio | Newspaper | Sales |
+ — — — -+ — — — -+ — — — — — -+ — — — -+
| 230.1 | 37.8 | 69.2 | 22.1 |
| 44.5 | 39.3 | 45.1 | 10.4 |
| 17.2 | 45.9 | 69.3 | 12.0 |
| 151.5 | 41.3 | 58.5 | 16.5 |
| 180.8 | 10.8 | 58.4 | 17.9 |
# In order to build he LR model we will use the first three columns as predictors (TV, Radio & Newspaper)predictors = ['TV', 'Radio', 'Newspaper']X = df[predictors]y = df['Sales']
# Model fitting and initializationlm = LinearRegression()
model = lm.fit(X, y)
# Now we can see the coefficients of our model 
print(f'alpha = {model.intercept_}')
print(f'betas = {model.coef_}')
# And make predictions based on arbitrary data 
input_params = [[200, 100, 100]]
print(model.predict(input_params))
  • 100 monetary units in Radio advertising
  • 100 monetary units in Newspaper advertising
# POST /predictionbody = json.loads(REQUEST)['body']# predict the output for a new sample. Function to be exposed through Titaninput_params = body['data']
print(model.predict(input_params))
  • Return the result of the last print statement to the caller.
# Mock request object for local API testing
headers = {
'content-type': 'application/json'
}
body = {
'data': [[200,100,100]]
}
REQUEST = json.dumps({ 'headers': headers, 'body': body })
$ titan deploy
3,2,1… Deploy!
Testing the service

Wrap-up

In this second post of our series of tutorials we have seen how to create and deploy a simple ML model directly from a Jupyter Notebook using our product Titan.

Next Tutorial

In our next tutorial, we show how to manage all the deployed services. Don’t miss it!

Foreword

Titan can help you to radically reduce and simplify the effort required to put AI/ML models into production, enabling Data Science teams to be agile, more productive and closer to the business impact of their developments.

Akoios: Frictionless solutions for modern data science.

Akoios