2

I trained my knn classifier over multiple images and saved the model. I am getting some new images to train. I don't want to retrain the already existing model.

How to add the newly trained model to the existing saved model ?

Could someone guide if this is possible or any articles describing the same ?

Thank you,

Sociopath
  • 13,068
  • 19
  • 47
  • 75
user1
  • 391
  • 3
  • 27
  • You can refer to Scikit-learn's [documentation on model persistence](https://scikit-learn.org/stable/modules/model_persistence.html). It describes how to use `pickle`, as @AkshayNevrekar has below. – jkr Feb 07 '18 at 13:40
  • 3
    Scikit-learns implementation of knn does not support updating of knn models. Only models that have a partial_fit method can be updated at a later stage. Here is an [overview](http://scikit-learn.org/stable/modules/scaling_strategies.html#incremental-learning) of models supporting a partial_fit. Knn is not listed there. But even this updating at a later stage is rather limited as explained in the docs. – error Feb 07 '18 at 13:46
  • Hi @error, Actually, i want to predict the new image from the trained set of images. So, i used knn to get the closest match of images. Can this be achieved with any of models under classification category from http://scikit-learn.org/stable/modules/scaling_strategies.html#incremental-learning ?Thank you, – user1 Feb 07 '18 at 14:21
  • Hi @error, i tried the sgd classifier for incremental learning. However, i got this https://stackoverflow.com/questions/48956252/scitkit-sgdclassifier-partial-fit-doesnot-learn-incrementally-returns-classes. Any ideas ? – user1 Feb 23 '18 at 21:33

2 Answers2

0

In Scikit-learn some algorithms can do partial_fil but KNN implementation cannot.

It's good to take a look at the Milvus vector engine database for incremental KNN and also ANN (Approximate Nearest Neighbor) method for a faster search

Masoud
  • 1,343
  • 8
  • 25
-1

You can use pickle to do so.

import pickle

# save the model to disk
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))


# load the model from disk
loaded_model = pickle.load(open(filename, 'rb'))

for more details you can refer this blogpost

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 1
    Hi Akshay, ur code is saving the model and loading it back. i am asking in the question about retraining the loaded model with newly added observartions. Is ur code doing this ? I read the blogpost too. It also doesnot say about this. – user1 Feb 07 '18 at 14:17