File tree Expand file tree Collapse file tree 2 files changed +8
-10
lines changed Expand file tree Collapse file tree 2 files changed +8
-10
lines changed Original file line number Diff line number Diff line change @@ -108,8 +108,6 @@ sudo pip3 install sklearn
108
108
### Run the example
109
109
110
110
``` bash
111
- sudo mkdir /app/models
112
- sudo chown postgres:postgres /app/models
113
111
psql -f scikit_train_and_predict.sql
114
112
```
115
113
Original file line number Diff line number Diff line change @@ -45,27 +45,27 @@ AS $$
45
45
rfc = RandomForestClassifier()
46
46
rfc .fit (X, y)
47
47
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()
51
49
52
50
$$ LANGUAGE plpython3u;
53
51
54
- SELECT scikit_learn_train_example() ;
52
+ ;
55
53
56
- CREATE OR REPLACE FUNCTION scikit_learn_predict_example (value INT )
54
+ CREATE OR REPLACE FUNCTION scikit_learn_predict_example (model TEXT , value INT )
57
55
RETURNS DOUBLE PRECISION
58
56
AS $$
59
57
import pickle
60
58
61
- with open(" /app/models/postgresml-rfc.pickle" , " rb" ) as f:
62
- m = pickle .load (f)
59
+ m = pickle .loads (bytes .fromhex (model))
63
60
64
61
r = m .predict ([[value,]])
65
62
return r[0 ]
66
63
$$ LANGUAGE plpython3u;
67
64
65
+ WITH model as (
66
+ SELECT scikit_learn_train_example() AS pickle
67
+ )
68
68
SELECT value,
69
69
weight,
70
- scikit_learn_predict_example(value::int ) AS prediction
70
+ scikit_learn_predict_example(( SELECT model . pickle FROM model), value::int ) AS prediction
71
71
FROM scikit_train_view LIMIT 5 ;
You can’t perform that action at this time.
0 commit comments