Skip to content

Commit 958cfba

Browse files
author
Montana Low
committed
keep model in memory to avoid going to disk
1 parent 3c66272 commit 958cfba

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ sudo pip3 install sklearn
108108
### Run the example
109109

110110
```bash
111-
sudo mkdir /app/models
112-
sudo chown postgres:postgres /app/models
113111
psql -f scikit_train_and_predict.sql
114112
```
115113

scikit_train_and_predict.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,27 @@ AS $$
4545
rfc = RandomForestClassifier()
4646
rfc.fit(X, y)
4747

48-
with open("/app/models/postgresml-rfc.pickle", "wb") as f:
49-
pickle.dump(rfc, f)
50-
return "OK"
48+
return pickle.dumps(rfc).hex()
5149

5250
$$ LANGUAGE plpython3u;
5351

54-
SELECT scikit_learn_train_example();
52+
;
5553

56-
CREATE OR REPLACE FUNCTION scikit_learn_predict_example(value INT)
54+
CREATE OR REPLACE FUNCTION scikit_learn_predict_example(model TEXT, value INT)
5755
RETURNS DOUBLE PRECISION
5856
AS $$
5957
import pickle
6058

61-
with open("/app/models/postgresml-rfc.pickle", "rb") as f:
62-
m = pickle.load(f)
59+
m = pickle.loads(bytes.fromhex(model))
6360

6461
r = m.predict([[value,]])
6562
return r[0]
6663
$$ LANGUAGE plpython3u;
6764

65+
WITH model as (
66+
SELECT scikit_learn_train_example() AS pickle
67+
)
6868
SELECT value,
6969
weight,
70-
scikit_learn_predict_example(value::int) AS prediction
70+
scikit_learn_predict_example((SELECT model.pickle FROM model), value::int) AS prediction
7171
FROM scikit_train_view LIMIT 5;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy