So I just watched a tutorial that the author didn't need to import sklearn when using predict function of pickled model in anaconda environment (sklearn installed).
I have tried to reproduce the minimal version of it in Google Colab. If you have a pickled-sklearn-model, the code below works in Colab (sklearn installed):
import pickle
model = pickle.load(open("model.pkl", "rb"), encoding="bytes")
out = model.predict([[20, 0, 1, 1, 0]])
print(out)
I realized that I still need the sklearn package installed. If I uninstall the sklearn, the predict function now is not working:
!pip uninstall scikit-learn
import pickle
model = pickle.load(open("model.pkl", "rb"), encoding="bytes")
out = model.predict([[20, 0, 1, 1, 0]])
print(out)
the error:
WARNING: Skipping scikit-learn as it is not installed.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-dec96951ae29> in <module>()
1 get_ipython().system('pip uninstall scikit-learn')
2 import pickle
----> 3 model = pickle.load(open("model.pkl", "rb"), encoding="bytes")
4 out = model.predict([[20, 0, 1, 1, 0]])
5 print(out)
ModuleNotFoundError: No module named 'sklearn'
So, how does it work? as far as I understand pickle doesn't depend on scikit-learn. Does the serialized model do import sklearn? Why can I use predict function without import scikit learn in the first code?
from Why is this code able to use the sklearn function without import sklearn?
No comments:
Post a Comment