# Update the saved model after training

Will saving a trained model this way give me a model trained on every chunk of data or just the last chunk?

df = pd.read_csv(, chunksize=10000)
for chunk in df:
text = chunk['body']
label = chunk['user_id']
print(text.shape, label.shape)

X_train, X_test, y_train, y_test = train_test_split(text, label, test_size=0.3)
text_clf.fit(X_train, y_train)
filename = 'finalized_model.sav'
joblib.dump(text_clf, filename)

# load the model from disk


For example, if the first chunk had labels 1 and 2, and the second chunk 3 and 4, will the final model be able to predict just 3 and 4? Or 1 and 2 as well, given the testing data has all the labels. Any help?

UPDATE The chunk is used to get text from the csv. I have updated my code.

• It is difficult to provide you with advise because your code is not clear. For example, chunk is not used inside the for loop - in fact, nothing is changing inside the loop. Apr 19 '20 at 18:06
• @nwaldo: Apologies. I have updated my code to make clear how chunk is being used inside the loop. Any answer would be helpful. Apr 20 '20 at 8:30

Short answer : a model will not be able to predict attribute it has not seen during the training process. From what I understand from your code, each model will erase the previous one.

• Thank you for your answer. So how can I iteratively train the model (because the whole data set doesn't fit in the memory at once) and save the model trained on every chunk? Any answer would be helpful. I have updated my question for better clarity. Apr 20 '20 at 8:32
• With multiple different models it will be difficult to know which one use to predict if you dont know if the model was trained with data similar as the one you want to predict Apr 20 '20 at 12:59
• You could consider splitting the space into 4 or more subsets that groups similar data and then train a model for each of them. With the new observation to predict, you will have to determine in which subset it falls, so you will be able to ask the right model. If this answer helps, please consider voting up my answer :) Apr 20 '20 at 13:01
• Thank you. I am not sure if you are quite understanding my question. Your solution will not scale as the data scales. In fact, it will take up more memory. So basically, there is no way to save a model trained on the whole data? Apr 20 '20 at 13:19

I'm assuming you do not have access to a larger machine (e.g. leveraging cloud compute) and you have to run this locally in some way. If that's the case, one suggestion I would make is to try a combination of Vaex and sklearn's SGDRegressor functionality.

You can think of Vaex as providing easy manipulation of dataframes which are larger than your memory (i.e. out-of-core Dataframes). With this, you can do your feature engineering and EDA in a more straightforward way than loading chunks.

When you are ready to model, you can leverage some of the models in sklearn which allow for training on batches of data at a time (e.g. SGDRegressor or SGDClassifier). These options provide a partial_fit method for training you model sequentially on different batches from your dataset.

Some links if you are interested in going down this path:

https://github.com/vaexio/vaex

https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html

https://towardsdatascience.com/ml-impossible-train-a-1-billion-sample-model-in-20-minutes-with-vaex-and-scikit-learn-on-your-9e2968e6f385