Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save classifier to postrgesql database, in scikit-learn

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?

like image 359
zlatko Avatar asked Oct 19 '17 12:10

zlatko


2 Answers

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)
like image 112
SDIdo Avatar answered Oct 23 '22 00:10

SDIdo


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()  
like image 3
Mohammad Sadoughi Avatar answered Oct 23 '22 02:10

Mohammad Sadoughi