Deploying our first Machine Learning model using Titan

Titan Tutorial #2: Getting started with ML

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))
# 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))
# 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

Next Tutorial

Foreword

Akoios: Frictionless solutions for modern data science.

Akoios