0

I have been looking for this information but I couldn't find it anywhere, so here's my shot.

I am a beginner in Python 2.7 and I learned a model, I saved it thanks to cPickle, but now I would like to know if it possible to load it from another device (which has not the sklearn library) and then use model.predict(X).

Thank you for your answers

PS: of course, i sent the model in pkl format to the new device

  • 2
    No. When unpickling, you need to have the library installed (and mostly the exact same version which was used when pickling). – Vivek Kumar Jun 21 '17 at 09:22
  • Ok thank you for your help ! – Benjamin Gornadha Jun 21 '17 at 09:43
  • You may want to look [here](https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled) and [here](https://stackoverflow.com/questions/3614379/attributeerror-when-unpickling-an-object) for more information. – Vivek Kumar Jun 21 '17 at 09:44

2 Answers2

2

As others said, unpickling won't work without sklearn installed; this is how Python pickle works. At the low level when you pickle something you're not saving actual source code for all objects/classes used - pickle just saves class/module names of objects. So you need the same environment to unpickle the data.

https://github.com/nok/sklearn-porter allows to export a limited subset of scikit-learn models to other languages. It does so by exporting parameters of sklearn estimators, and then providing an implementation of 'predict' function in a target language. Implementation of prediction routine is much simpler than training for most models, so it is feasible. You can do the same manually if you absolutely need it, and sklearn-porter doesn't support your models.

But usually it is easier to just ensure environments are compatible, i.e. sklearn is installed and versions of packages match.

Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
  • 1
    I'm the main developer of [sklearn-porter](https://github.com/nok/sklearn-porter/). You can transpile models in pickle format on the command line interactively, e.g.: `python -m sklearn_porter -i estimator.pkl --js --pipe > estimator.js`. If the porter doesn't support the used algorithm, you will see an error on the command line as a result. Otherwise it will generate the transpiled version and save it in the piped file. – Darius Jan 14 '18 at 18:47
0

You can load it without sklearn, see here(with other languages support of cPickle), but you can't use it for predict, because sklearn has no other language interfaces.

superK
  • 3,932
  • 6
  • 30
  • 54