Say, I have finished building a regression model in Python, as below:
from sklearn import linear_model
model = linear_model.LogisticRegression().fit(X_train, Y_train)
How do I save the "model" that I have built so I could shut down my computer and work on it the next day without having to rerun the code to get the "model" again?
The reason why I am asking this is because my dataset is quite huge and it will take really long having to rerun to get the model again.
This is a problem of serialisation and a very simple way would be to use the pickle
module. The following snippets show how you can save and load a Python object.
To save:
import pickle
with open("YOUR_FILE_NAME_HERE.pkl", 'wb') as file:
pickle.dump(model, file)
To load:
# Import all your relevant libraries first
from sklearn import linear_model
...
import pickle
with open("YOUR_FILE_NAME_HERE.pkl", 'rb') as file:
model = pickle.load(file)
The idea is to essentially create a file representation of your object ( model
) and save it to file in a way that can be interpreted and loaded on demand. This can be achieved in many different ways, but the simplest method with Python is to use pickle which creates a binary representation of your object and all the associated objects and modules.
For further reading, consult the pickle
documentation here and for a better understanding of serialisation, refer to here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.