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

Can use joblib dump many object?

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

您可以简单地使用:

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