可以使用joblib转储许多对象吗?

时间:2019-05-26 10:35:10

标签: python svm pca joblib

我有这个代码。我可以使用joblib.dump将'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')

1 个答案:

答案 0 :(得分:0)

您可以简单地使用:

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')