在scikit-learn中将分类器保存到postrgesql数据库中

问题描述 投票:4回答:1

我知道scikit学习模型可以通过使用joblib保留在文件中(如此处所述:http://scikit-learn.org/stable/modules/model_persistence.html)。但是,由于我在postgresql plpythonu函数中有机器学习过程,因此我宁愿将模型保留在Postgresql数据库中。

推荐什么,在Postgresql数据库中存储scikit-learn模型的最便捷方法?

postgresql scikit-learn persistence
1个回答
1
投票

[这是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()  
© www.soinside.com 2019 - 2024. All rights reserved.