我知道scikit-learn模型可以通过使用joblib(如下所述:http://scikit-learn.org/stable/modules/model_persistence.html)持久化在文件中。但是,由于我在postgresql plpythonu函数中具有机器学习过程,因此我宁愿将模型保留在Postgresql数据库中。
推荐什么,在Postgresql数据库中存储scikit-learn模型的最方便的方法是什么?
如果你使用的是 Django,你可以将 sci-kit 学习模型二值化
使用泡菜,然后将其保存到具有二进制字段成员的表。
一个简单的例子:
views.py(保存)
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(使用)
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)
这是python中的示例代码,用于将训练好的模型发送到Postgres表。请注意,您首先需要创建一个表,该表具有"bytea"类型的列,以二进制格式存储腌制的 sklearn 模型。
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()