I know that scikit-learn models can be persisted in files, by using joblib (as described here: http://scikit-learn.org/stable/modules/model_persistence.html). However, since i have machine learning procedure inside postgresql plpythonu function, I would rather persist the model inside the Postgresql database.
What is recommended, the most convinient way to store scikit-learn model inside a Postgresql database?
If you are using Django you can binarize a sci-kit learn model
using pickle and then save it to a table that has a BinaryField member.
A simple example:
views.py (saving)
from sklearn import svm
import pickle
from ml.models import MlModels
from rest_framework.response import Response
@api_view(['GET'])
def save(request):
if request.method == 'GET':
X = [[0.12, 22, 33, 100], [0.19, 19, 99, 33], [0.5, 50, 150, 0]]
y = [1, 0, 1]
model = svm()
model.fit(X=X, y=y)
data = pickle.dumps(model)
MlModels.objects.create(model=data)
return Response(status=status.HTTP_200_OK)
models.py
from django.db import models
class MlModels(models.Model):
model = models.BinaryField()
views.py (using)
import pickle
from ml.models import MlModels
from rest_framework.response import Response
@api_view(['GET'])
def predict(request):
if request.method == "GET":
X = [[0.12, 22, 33, 100]]
raw_model = MlModel.objects.all()[0]
model = pickle.loads(raw_model.model)
print(model.predict(X))
return Response(status=status.HTTP_200_OK)
Here is sample code in python for sending the trained model to a Postgres table. Note that you first need to create a table that has a column with the "bytea" type to store the pickled sklearn model in bineary format.
from sklearn import svm
import psycopg2
import pickle
#### # Connect to postgres
connection = psycopg2.connect(user, password, host, port, database)
cur = connection.cursor()
model = svm.OneClassSVM()
model.fit(features) # features are some training data
data = pickle.dumps(model) # first we should pickle the model
#### # Assuming you have a postgres table with columns epoch and file
sql = "INSERT INTO sampletable (epoch, file) VALUES(%s)"
cur.execute(sql, (epochpsycopg2.Binary(data)) )
connection.commit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With