ImportError: cannot import name 'joblib' from 'sklearn.externals'

后端 未结 9 1367
暖寄归人
暖寄归人 2020-12-17 07:59

I am trying to load my saved model from s3 using joblib

import pandas as pd 
import numpy as np
import json
import subprocess
import sqlalchemy
from sklearn.         


        
相关标签:
9条回答
  • 2020-12-17 08:23

    Just try checking your installed packages for joblib...Then import it..

    0 讨论(0)
  • 2020-12-17 08:25

    In case the execution / call to joblib is within another .py program instead of your own (in such case even you have installed joblib, it still causes error from within the calling python programme unless you change the code, i thought would be messy), I tried to create a hardlink:

    (windows version)

    Python> import joblib

    then inside your sklearn path >......\Lib\site-packages\sklearn\externals

    mklink /J ./joblib .....\Lib\site-packages\joblib

    (you can work out the above using a ! or %, !mklink....... or %mklink...... inside your Python juptyter notebook , or use python OS command...)

    This effectively create a virtual folder of joblib within the "externals" folder

    Remarks: Of course to be more version resilient, your code has to check for the version of sklearn is >= 0.23 again before hand.

    This would be alternative to changing sklearn vesrion.

    0 讨论(0)
  • 2020-12-17 08:28

    When getting error:

    from sklearn.externals import joblib it deprecated older version.

    For new version follow:

    1. conda install -c anaconda scikit-learn (install using "Anaconda Promt")
    2. import joblib (Jupyter Notebook)
    0 讨论(0)
  • 2020-12-17 08:32

    I have tried to import joblib directly and its work for me like below.

    import joblib

    0 讨论(0)
  • 2020-12-17 08:33

    It looks like your existing pickle save file (model_d2v_version_002) encodes a reference module in a non-standard location – a joblib that's in sklearn.externals.joblib rather than at top-level.

    The current scikit-learn documentation only talks about a top-level joblib – eg in 3.4.1 Persistence example – but I do see a reference in someone else's old issue to a DeprecationWarning in scikit-learn version 0.21 about an older scikit.external.joblib variant going away:

    Python37\lib\site-packages\sklearn\externals\joblib_init_.py:15: DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.

    'Deprecation' means marking something as inadvisable to rely-upon, as it is likely to be discontinued in a future release (often, but not always, with a recommended newer way to do the same thing).

    I suspect your model_d2v_version_002 file was saved from an older version of scikit-learn, and you're now using scikit-learn (aka sklearn) version 0.23+ which has totally removed the sklearn.external.joblib variation. Thus your file can't be directly or easily loaded to your current environment.

    But, per the DeprecationWarning, you can probably temporarily use an older scikit-learn version to load the file the old way once, then re-save it with the now-preferred way. Given the warning info, this would probably require scikit-learn version 0.21.x or 0.22.x, but if you know exactly which version your model_d2v_version_002 file was saved from, I'd try to use that. The steps would roughly be:

    • create a temporary working environment (or roll back your current working environment) with the older sklearn

    • do imports something like:

    import sklearn.external.joblib as extjoblib
    import joblib
    
    • extjoblib.load() your old file as you'd planned, but then immediately re-joblib.dump() the file using the top-level joblib. (You likely want to use a distinct name, to keep the older file around, just in case.)

    • move/update to your real, modern environment, and only import joblib (top level) to use joblib.load() - no longer having any references to `sklearn.external.joblib' in either your code, or your stored pickle files.

    0 讨论(0)
  • 2020-12-17 08:43

    Maybe your code is outdated. For anyone who aims to use fetch_mldata in digit handwritten project, you should fetch_openml instead. (link)

    In old version of sklearn:

    from sklearn.externals import joblib
    mnist = fetch_mldata('MNIST original')
    

    In sklearn 0.23 (stable release):

    import sklearn.externals
    import joblib
        
    dataset = datasets.fetch_openml("mnist_784")
    
    features = np.array(dataset.data, 'int16')
    labels = np.array(dataset.target, 'int')
    

    For more info about deprecating fetch_mldata see scikit-learn doc

    0 讨论(0)
提交回复
热议问题