Model

Code
from palmerpenguins import penguins
from pandas import get_dummies
# import numpy as np
from sklearn.linear_model import LinearRegression
# from sklearn import preprocessing
from vetiver import VetiverModel, vetiver_pin_write, VetiverAPI
from pins import board_folder
import duckdb
import requests

1 Get Data

Code
con = duckdb.connect("my-db.duckdb")
df = con.execute("SELECT * FROM penguins").fetchdf().dropna()
con.close()

2 Define Model and Fit

Code
X = get_dummies(df[['bill_length_mm', 'species', 'sex']], drop_first = False)
y = df['body_mass_g']

model = LinearRegression().fit(X, y)

3 Get some information

Code
print(f"R^2 {model.score(X,y)}")
print(f"Intercept {model.intercept_}")
print(f"Columns {X.columns}")
print(f"Coefficients {model.coef_}")
R^2 0.8555368759537614
Intercept 2708.3204686392337
Columns Index(['bill_length_mm', 'species_Adelie', 'species_Chinstrap',
       'species_Gentoo', 'sex_female', 'sex_male'],
      dtype='object')
Coefficients [  32.53688677 -265.36728566 -564.13282013  829.50010579 -273.68346204
  273.68346204]

4 Create API

Code
# run this stage in CLI
b = board_folder("data/model", allow_pickle_read=True)
v = VetiverModel.from_pin(b, "penguin_model")
app = VetiverAPI(v, check_prototype=True)

app.run(port=8080)
# may need to kill the server process eg kill 34246 when done

5 Query API

Code
# once app is running, execute cell
pred_X = 20
req_data = [{
  "bill_length_mm": pred_X,
  "species_Chinstrap": False,
  "species_Gentoo": False,
  "sex_male": False
}]
req = requests.post(
  "http://127.0.0.1:8080/predict", json=req_data,
  headers={"Content-Type": "application/json"},
)
res = req.json().get("predict")[0]
print(f"For bill length {pred_X}, body mass {res} is predicted.")