Save and Load Machine Learning Models in Python with scikit-learn

Finding an accurate machine learning model is not the end of the project.

In this post you will discover how to save and load your machine learning model in Python using scikit-learn.

This allows you to save your model to file and load it later in order to make predictions.

Let’s get started.

  • Update Jan/2017: Updated to reflect changes to the scikit-learn API in version 0.18.
Save and Load Machine Learning Models in Python with scikit-learn

Save and Load Machine Learning Models in Python with scikit-learn
Photo by Christine, some rights reserved.

Need help with Machine Learning in Python?

Take my free 2-week email course and discover data prep, algorithms and more (with sample code).

Click to sign-up now and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

Finalize Your Model with pickle

Pickle is the standard way of serializing objects in Python.

You can use the pickle operation to serialize your machine learning algorithms and save the serialized format to a file.

Later you can load this file to deserialize your model and use it to make new predictions.

The example below demonstrates how you can train a logistic regression model on the Pima Indians onset of diabetes dataset, save the model to file and load it to make predictions on the unseen test set.

Running the example saves the model to finalized_model.sav in your local working directory. Load the saved model and evaluating it provides an estimate of accuracy of the model on unseen data.

Finalize Your Model with joblib

Joblib is part of the SciPy ecosystem and provides utilities for pipelining Python jobs.

It provides utilities for saving and loading Python objects that make use of NumPy data structures, efficiently.

This can be useful for some machine learning algorithms that require a lot of parameters or store the entire dataset (like K-Nearest Neighbors).

The example below demonstrates how you can train a logistic regression model on the Pima Indians onset of diabetes dataset, saves the model to file using joblib and load it to make predictions on the unseen test set.

Running the example saves the model to file as finalized_model.sav and also creates one file for each NumPy array in the model (four additional files). After the model is loaded an estimate of the accuracy of the model on unseen data is reported.

Tips for Finalizing Your Model

This section lists some important considerations when finalizing your machine learning models.

  • Python Version. Take note of the python version. You almost certainly require the same major (and maybe minor) version of Python used to serialize the model when you later load it and deserialize it.
  • Library Versions. The version of all major libraries used in your machine learning project almost certainly need to be the same when deserializing a saved model. This is not limited to the version of NumPy and the version of scikit-learn.
  • Manual Serialization. You might like to manually output the parameters of your learned model so that you can use them directly in scikit-learn or another platform in the future. Often the algorithms used by machine learning algorithms to make predictions are a lot simpler than those used to learn the parameters can may be easy to implement in custom code that you have control over.

Take note of the version so that you can re-create the environment if for some reason you cannot reload your model on another machine or another platform at a later time.

Summary

In this post you discovered how to persist your machine learning algorithms in Python with scikit-learn.

You learned two techniques that you can use:

  • The pickle API for serializing standard Python objects.
  • The joblib API for efficiently serializing Python objects with NumPy arrays.

Do you have any questions about saving and loading your machine learning algorithms or about this post? Ask your questions in the comments and I will do my best to answer them.


Frustrated With Python Machine Learning?

Master Machine Learning With Python

Develop Your Own Models in Minutes

…with just a few lines of scikit-learn code

Discover how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.


56 Responses to Save and Load Machine Learning Models in Python with scikit-learn

  1. Kayode October 18, 2016 at 6:15 pm #

    Thank you so much for this educative post.

  2. TonyD November 13, 2016 at 3:52 pm #

    Hi Jason,

    I have two of your books and they are awesome. I took several machine learning courses before, however as you mentioned they are more geared towards theory than practicing. I devoured your Machine Learnign with Python book and 20x my skills compared to the courses I took.

    I found this page by Googling a code snippet in chapter 17 in your book. The line:

    loaded_model = pickle.load(open(filename, ‘rb’))

    throws the error:

    runfile(‘C:/Users/Tony/Documents/MassData_Regression_Pickle.py’, wdir=’C:/Users/Tony/Documents’)
    File “C:/Users/Tony/Documents/MassData_Regression_Pickle.py”, line 55
    loaded_model = pickle.load(open(filename, ‘rb’))
    ^
    SyntaxError: invalid syntax

    • Jason Brownlee November 14, 2016 at 7:36 am #

      Thanks TonyD.

      I wonder if there is a copy-paste error, like an extra space or something?

      Does the code example (.py file) provided with the book for that chapter work for you?

  3. Konstantin November 19, 2016 at 6:01 am #

    Hello, Jason

    Where we can get X_test, Y_test “sometime later”? It is “garbag collected”!
    X_test, Y_test not pickled In your example you pickle classifier only but you keep refer to x and y. Real applications is not single flow I found work around and get Y from clf.classes_ object.

    What is correct solution? Should we pickle decorator class with X and Y or use pickled classifier to pull Ys values? I didn’t find legal information from documentation on KNeighborclassifier(my example) as well; how to pull Y values from classifier.

    Can you advise?

    • Jason Brownlee November 19, 2016 at 8:51 am #

      Hi Konstantin,

      I would not suggest saving the data. The idea is to show how to load the model and use it on new data – I use existing data just for demonstration purposes.

      You can load new data from file in the future when you load your model and use that new data to make a prediction.

      If you have the expected values also (y), you can compare the predictions to the expected values and see how well the model performed.

      • Guangping Zhang November 21, 2016 at 6:01 am #

        I’m newer Pythoner, your code works perfect! But where is the saved file? I used windows 10.

        • Jason Brownlee November 22, 2016 at 6:56 am #

          Thanks Guangping.

          The save file is in your current working directory, when running from the commandline.

          If you’re using a notebook or IDE, I don’t know where the file is placed.

  4. Mohammed Alnemari December 13, 2016 at 2:45 pm #

    Hi Jason ,
    I am just wondering if can we use Yaml or Json with sklearn library . I tried to do it many times but I could not reach to an answer . I tried to do it as your lesson of Kares , but for some reason is not working . hopefully you can help me if it is possible

    • Jason Brownlee December 14, 2016 at 8:24 am #

      Hi Mohammed, I believe the serialization of models to yaml and json is specific to the Keras library.

      sklearn serialization is focused on binary files like pickle.

  5. Normando Zubia December 29, 2016 at 9:55 am #

    Hi, my name is Normando Zubia and I have been reading a lot of your material for my school lessons.

    I’m currently working on a model to predict user behavoir in a production environment. Due to several situations I can not save the model in a pickle file. Do you know any way to save the model in a json file?

    I have been playing a little with sklearn classes and I noticed that if I save some parameters for example: n_values_, feature_indices_ and active_features_ in a OneHotEncoding model I can reproduce the results. Could this be done with a pipeline? Or do you think I need to save each model’s parameters to load each model?

    PS: Sorry for my bad english and thanks for your attention.

    • Jason Brownlee December 30, 2016 at 5:49 am #

      Hi Normando,

      If you are using a simple model, you could save the coefficients directly to file. You can then try and put them back in a new model later or implement the prediction part of the algorithm yourself (very easy for most methods).

      Let me know how you go.

  6. Samuel February 6, 2017 at 3:14 pm #

    Hello Jason,

    I am new to machine learning. I am your big fan and read a lot of your blog and books. Thank you very much for teaching us machine learning.

    I tried to pickle my model but fail. My model is using VGG16 and replace the top layer for my classification solution. I further narrowed down the problem and find that it is the VGG16 model failed to pickle. Please find my simplified code below and error log below:

    It will be highly appreciated if you can give me some direction on how to fix this error.

    Thank you very much
    ———————————————————-
    # Save Model Using Pickle
    from keras.applications.vgg16 import VGG16
    import pickle

    model = VGG16(weights=’imagenet’, include_top=False)

    filename = ‘finalized_model.sav’
    pickle.dump(model, open(filename, ‘wb’))

    —————————————————-
    /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/samueltin/Projects/bitbucket/share-card-ml/pickle_test.py
    Using TensorFlow backend.
    Traceback (most recent call last):
    File “/Users/samueltin/Projects/bitbucket/share-card-ml/pickle_test.py”, line 8, in
    pickle.dump(model, open(filename, ‘wb’))
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 1376, in dump
    Pickler(file, protocol).dump(obj)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 224, in dump
    self.save(obj)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 606, in save_list
    self._batch_appends(iter(obj))
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 621, in _batch_appends
    save(x)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 606, in save_list
    self._batch_appends(iter(obj))
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 621, in _batch_appends
    save(x)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 606, in save_list
    self._batch_appends(iter(obj))
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 621, in _batch_appends
    save(x)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 331, in save
    self.save_reduce(obj=obj, *rv)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 425, in save_reduce
    save(state)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 606, in save_list
    self._batch_appends(iter(obj))
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 621, in _batch_appends
    save(x)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 568, in save_tuple
    save(element)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 286, in save
    f(self, obj) # Call unbound method with explicit self
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 655, in save_dict
    self._batch_setitems(obj.iteritems())
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 669, in _batch_setitems
    save(v)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py”, line 306, in save
    rv = reduce(self.proto)
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py”, line 70, in _reduce_ex
    raise TypeError, “can’t pickle %s objects” % base.__name__
    TypeError: can’t pickle module objects

    Process finished with exit code 1

    • Jason Brownlee February 7, 2017 at 10:11 am #

      Sorry Samuel, I have not tried to save a pre-trained model before. I don’t have good advice for you.

      Let me know how you go.

  7. Amy March 8, 2017 at 7:03 am #

    I have trained a model using liblinearutils. The model could not be saved using pickle as it gives error that ctype module with pointers cannot be pickled. How can I save my model?

    • Jason Brownlee March 8, 2017 at 9:47 am #

      Sorry Amy, I don’t have any specific examples to help.

      Perhaps you can save the coefficients of your model to file?

  8. SHUBHAM BHARDWAJ April 3, 2017 at 10:42 pm #

    Thanks a lot, very useful

  9. Benju April 11, 2017 at 1:35 am #

    My saved modells are 500MB+ Big….is that normal?

    • Jason Brownlee April 11, 2017 at 9:34 am #

      Ouch, that does sound big.

      If your model is large (lots of layers and neurons) then this may make sense.

  10. Anupam April 13, 2017 at 2:32 am #

    How to use model file (“finalized_model.sav”) to test unknown data. Like, if the model is for tagger , how this model will tag the text file data? Is there any example?

  11. Oss Mps April 21, 2017 at 3:09 pm #

    Dear Sir, please advice on how to extract weights from pickle dump? Thank you

    • Jason Brownlee April 22, 2017 at 9:23 am #

      I would suggest extracting coefficients from your model directly and saving them in your preferred format.

  12. Suhas May 24, 2017 at 4:44 am #

    Hi I love your website; it’s very useful!

    Are there any examples showing how to save out the training of a model after say 100 epochs/iterations? It’s not immediately clear from looking at joblib or scikit learn.

    This is esp. useful when dealing with large datasets and/or computers or clusters which may be unreliable (e.g., subject to system reboots, etc.)

    • Jason Brownlee May 24, 2017 at 4:59 am #

      I’m not sure how to do this with sklearn. You may need to write something custom. Consider posting to stackoverflow.

  13. Viktor May 30, 2017 at 8:52 am #

    Hey!
    Is it possible to open my saved model and make a prediction on cloud server where is no sklearn installed?

    • Jason Brownlee June 2, 2017 at 12:31 pm #

      no.

      You could save the coefficients from within the model instead and write your own custom prediction code.

  14. Clemence June 8, 2017 at 6:55 pm #

    Hello Jason and thank you very much, it’s been very helpful.

    Do you know if it’s possible to load features transformation with the ML model?
    I’m mostly thinking of categorical variables that we need to encode into numerical ones.

    I’m using sklearn to do that, but I don’t know if we can (as for Spark), integrate this transformation with the ML model into the serialized file (Pickle or Joblib).

    #Encode categorical variable into numerical ones
    from sklearn.preprocessing import LabelEncoder
    list_var = [‘country’, ‘city’]

    encoder = LabelEncoder()
    for i in list_var:
    df[i] = encoder.fit_transform(df[i])

    Then I fit the model on the training dataset…

    And I need to save this transformation with the model. Do you know if that’s possible ?
    Thank you!

    • Jason Brownlee June 9, 2017 at 6:23 am #

      I’m not sure I follow sorry.

      You can transform your data for your model, and you can apply this same transform in the future when you load your model.

      You can save the transform objects using pickle. Is that what you mean?

  15. Bhavani Shanker June 22, 2017 at 1:24 am #

    Hi Jason,
    Kindly accept my encomiums for the illustrative lecture that you have delivered on Machine Learning using Python.

    **********************************************
    # save the model to disk
    filename = ‘finalized_model.sav’
    joblib.dump(model, filename)

    # sometime later…

    # load the model from disk
    loaded_model = joblib.load(filename)
    result = loaded_model.score(X_test, Y_test)
    print(result)
    *******************************************************

    After saving the model ‘finalized_model.sav’ , How can recall the saved model in the new session at later date?

    I would appreciate if you can advice on this

    • Jason Brownlee June 22, 2017 at 6:11 am #

      The code after “sometime later” would be in a new session.

  16. jinsh June 28, 2017 at 8:57 pm #

    Hello sir,

    The above code saves the model and later we can check the accuracy also
    but what i have to do for making predicting the class of unknown data?
    I mean which function have to be called ?

    eg: 2,132,40,35,168,43.1,2.288,33

    can you suggest how to get the class of above data through prediction ?

    thank you

    • Jason Brownlee June 29, 2017 at 6:35 am #

      Pass in input data to the predict function and use the result.

  17. Ukesh Chawal July 24, 2017 at 11:09 pm #

    Can we use “pickling” to save an LSTM model and to load or used a hard-coded pre-fit model to generate forecasts based on data passed in to initialize the model?

    When I tried to use it, it gave me following error:

    PicklingError: Can’t pickle : attribute lookup module on builtins failed

  18. akatsuki August 9, 2017 at 1:21 pm #

    tbh this is best of the sites on web. Great!
    I love the email subscriptions of yours as a beginner they are quite helpful to me .

  19. vikash August 10, 2017 at 9:32 pm #

    Hi @Jason Brownlee thanks for such informative blog. Can you please guide me for a problem where i would like to retrain the .pkl model only with new dataset with new class keeping the previous learning intact. I had thought that model.fit(dataset,label) will do that but it forgets the previous learning. Please suggest me some techniques for it.
    Thanks

    • Jason Brownlee August 11, 2017 at 6:42 am #

      Sorry, I don’t follow. Can you please restate your question?

      • sassashi August 28, 2017 at 4:41 am #

        Hi Jason, I believe @vikash is looking for a way to continuously train the model with new examples after the initial training stage. This is something I am searching for as well. I know it is possible to retrain a model in tensorflow with new examples but I am not sure if it’s possible with sklearn.

        to expand the question some more: 1-you train a model with sklearn 2-save it with pickle or joblib
        3-then you get your hands on some new examples that were not available at the time of initial training “step 1” 4-you load the previous model 5-and now you try to train the model again using the new data without losing the previous knowledge… is step 5 possible with sklearn?

  20. Navdeep Singh August 22, 2017 at 8:30 pm #

    Hi Json,

    I need your guidance on Updation of saved pickle files with new data coming in for training

    I recall 3 methods, Online Learning which is train one every new observation coming in and in this case model would always be biased towards new features ,which i dont wana do

    Second is, Whenever some set of n observations comes, embedd it with previous data and do retraining again from scratch, that i dont want to do as in live environment it will take lot of time

    Third is Mini batch learning, i know some of algorithm like SGD and other use partial fit method and do same but I have other algorithms as week like random forest , decision tress, logistic regression. I wana ask can i update the previously trained pickle with new training ?

    I am doing it in text classification, I read that possibly doing this, model update pickle will not take new features of new data ( made using tfidf or countvectorizer) and it would be of less help.

    Also as domain is same, and If client(Project we are working for) is different , inspite of sharing old data with new client (new project), could i use old client trained model pickle and update it with training in new client data. Basically I am transferring learning

    • Jason Brownlee August 23, 2017 at 6:48 am #

      Great question.

      This is a challenging problem to solve. Really, the solution must be specific to your project requirements.

      A flexible approach may be to build-in capacity into your encodings to allow for new words in the future.

      The simplest approach is to ignore new words.

      These, and other strategies are testable. See how performance degrades under both schemes with out-of-band test data.

  21. Merari September 11, 2017 at 7:59 am #

    Gracias por compartir,
    Existe alguna forma en la que pueda realizar predicciones con nuevos datos solo con el modelo guardado? llamando este modelo desde un archivo nuevo? lo he intentado con la instruccion final:

    # load the model from disk
    loaded_model = pickle.load(open(filename, ‘rb’))
    result = loaded_model.score(X_test, Y_test)
    print(result)

    pero no lo he logrado

    373/5000
    Thanks for sharing,
    Is there any way I can make predictions with new data only with the saved model? calling this model from a new file? I have tried with the final instruction:

    # load the model from disk
    loaded_model = pickle.load (open (filename, ‘rb’))
    result = loaded_model.score (X_test, Y_test)
    print (result)

    but I have not achieved it

    • Jason Brownlee September 11, 2017 at 12:11 pm #

      That is exactly what we do in this tutorial.

      What is the problem exactly?

  22. AP September 29, 2017 at 6:36 am #

    Hi Jason, I learn a lot reading your python books and blogs. Thank you for everything.

    I’m having an issue when I work on text data with loaded model in a different session. I fit and transform training data with countvectorizer and tfidf. Then I only transform the test data with the fitted instances as usual. But, when work on loaded pretrained model in a different session, I am having problem in feature extraction. I can’t just transform the test data as it asks for fitted instance which is not present in the current session. If I fit and transform on test data only, model prediction performance drastically decreases. I believe that is wrong way of doing machine learning. So, how can I do the feature extraction using countvectorizer, tfidf or other cases while working with previously trained model?

    I’m using spark ML but I think it would be the same for scikit-learn as well.

    • Jason Brownlee September 30, 2017 at 7:31 am #

      Perhaps you can pickle your data transform objects as well, and re-use them in the second session?

  23. Bhavya Chugh October 29, 2017 at 5:57 am #

    Hi Jason,

    I trained a random forest model and saved the same as a pickle file in my local desktop. I then copied that pickle file to my remote and tested the model with the same file and it is giving incorrect predictions. I am using python 3.6 in my local and python 3.4 in my remote, however the version of scikit-learn are same. Any ideas why this may be happening?

    • Jason Brownlee October 29, 2017 at 6:00 am #

      No idea, perhaps see if the experiment can be replicated on the same machine? or different machines with the same version of Python?

  24. Berkin Albert Antony November 10, 2017 at 5:45 pm #

    Hi Jason Brownlee,

    I have a LogisticRegression model for binary classification. I wish to find a similar data points in a trained model for a given test data points. So that I can show these are similar data points predicted with these same class.

    Could you please suggest your thoughts for the same. I am using scikit learn logistic regression

    Thanks

    • Jason Brownlee November 11, 2017 at 9:18 am #

      Perhaps you could find data points with a low Euclidean distance from each other?

  25. James November 16, 2017 at 8:47 am #

    Hi Jason –

    If you pickle a model trained on a subset of features, is it possible to view these features after loading the pickled model in a different file? For example: original df has features a,b,c,d,e,f. You train the model on a,c,e. Is it possible to load the pickled model in a separate script and see the model was trained on a,c,e?

    Thanks,
    James

    • Jason Brownlee November 16, 2017 at 10:33 am #

      Yes, you can save your model, load your model, then use it to make predictions on new data.

Leave a Reply