I'm trying to save multiple trained models for binary classification using pickle. I'm saving them in a pickle file as a tuple with the name of the model (name, model)
. However, when I load them and try to use the models all the predictions are zero. This is not the case if I train and predict all at once. Here is the part of the code where I'm saving the models:
else:
# Simple pipeline
if dimension_reduction is None:
model = Pipeline(steps=[('ss', scaler), ('clf', clf)])
else:
model = Pipeline(steps=[('ss', scaler), ('dr', dimension_reduction), ('clf', clf)])
# Training the model
model.fit(X_train, y_train)
# Saving into a list
models.append((name,model))
# Saving into a pickle file
with open("sklearn_models_{}.pckl".format(models_name), "wb") as f:
for model in models:
pickle.dump(model, f)
print('Models saved')
I load them using
models = []
with open("sklearn_models_{}.pckl".format(NAME_MODELS), "rb") as f:
while True:
try:
models.append(pickle.load(f))
except EOFError:
break
The complete code is in this link. The training bit I put here is lines 166 to 183 and the loading bit lines 361 to 368.
It's there a better way to do this? What am I doing wrong?