Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 2.03 KB

02-pickle.md

File metadata and controls

51 lines (37 loc) · 2.03 KB

5.2 Saving and loading the model

Slides

Notes

In this session we'll cover the idea "How to use the model in future without training and evaluating the code"

  • To save the model we made before there is an option using the pickle library:
    • First install the library with the command pip install pickle-mixin if you don't have it.
    • After training the model and being the model ready for prediction process use this code to save the model for later.
    • import pickle
      
      with open('model.bin', 'wb') as f_out: # 'wb' means write-binary
          pickle.dump((dict_vectorizer, model), f_out)
    • In the code above we'll making a binary file named model.bin and writing the dict_vectorizer for one hot encoding and model as array in it. (We will save it as binary in case it wouldn't be readable by humans)
    • To be able to use the model in future without running the code, We need to open the binary file we saved before.
    • import pickle
      
      with open('mode.bin', 'rb') as f_in: # very important to use 'rb' here, it means read-binary 
          dict_vectorizer, model = pickle.load(f_in)
      ## Note: never open a binary file you do not trust the source!
    • With unpacking the model and the dict_vectorizer, We're able to again predict for new input values without training a new model by re-running the code.

Add notes from the video (PRs are welcome)

⚠️ The notes are written by the community.
If you see an error here, please create a PR with a fix.

Navigation