可以用Joblib倾倒多个对象吗?

2022-04-07 00:00:00 python pca joblib svm

问题描述

我有这个代码。我可以使用joblib.ump将‘pca’和‘svm_clf’保存到一个文件吗?如果没有,还有其他方法吗?

from sklearn.svm import SVC
from sklearn.externals import joblib
from sklearn import decomposition
from sklearn import svm

X = [[1,3,4,5,6,7,8,7,0],[1,3,4,5,6,7,9,5,1]]
Y = [1,0]
#pca
pca = decomposition.PCA(0.98)
X_new = pca.fit_transform(X)
svm_clf = svm.LinearSVC()
#svm model
svm_clf = SVC(C=1, kernel='rbf')
svm_clf.fit(X_new, Y)
#save model
joblib.dump(pca, 'model.sav')
joblib.dump(svm_clf, 'model.sav')

解决方案

您只需使用:

joblib.dump([pca, svm_clf], 'model.sav', compress=1)

然后使用如下模型:

pca, svm_clf = joblib.load('model.sav')

如果您想要同时使用这两个模型并转储管道,可能更好的方法是定义管道:

from sklearn.pipeline import make_pipeline
pipeline = make_pipeline(pca, svm_clf)
joblib.dump(pipeline, 'model.sav')

相关文章