-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.qmd
109 lines (84 loc) · 2.07 KB
/
model.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: "Model"
format:
html:
code-fold: true
jupyter:
kernelspec:
name: "conda-env-do4ds-env-py"
language: "python"
display_name: "do4ds-env"
---
```{python}
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
```
## Get Data
```{python}
con = duckdb.connect("my-db.duckdb")
df = con.execute("SELECT * FROM penguins").fetchdf().dropna()
con.close()
```
```{python}
#| echo: false
#| eval: false
# df = penguins.load_penguins().dropna()
# df.head(3)
```
## Define Model and Fit
```{python}
X = get_dummies(df[['bill_length_mm', 'species', 'sex']], drop_first = False)
y = df['body_mass_g']
model = LinearRegression().fit(X, y)
```
## Get some information
```{python}
print(f"R^2 {model.score(X,y)}")
print(f"Intercept {model.intercept_}")
print(f"Columns {X.columns}")
print(f"Coefficients {model.coef_}")
```
```{python}
#| echo: false
#| eval: false
# save the model
# v = VetiverModel(model, model_name='penguin_model', prototype_data=X)
# # create a board with pins
# model_board = board_folder(path="data/model", versioned=True, allow_pickle_read=True)
# vetiver_pin_write(model_board, v)
```
## Create API
```{python}
#| eval: false
# 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
```
## Query API
```{python}
#| eval: false
# 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.")
```