can't use scikit-learn - “AttributeError: 'module' object has no attribute …”

后端 未结 9 629
失恋的感觉
失恋的感觉 2020-12-31 02:57

I\'m trying to follow this tutorial of scikit-learn (linear regression).

I\'ve installed scikit through pip install -U scikit-learn, I use python 2.7 an

相关标签:
9条回答
  • 2020-12-31 03:28

    This worked for me:

    from sklearn.datasets import make_moons
    
    0 讨论(0)
  • 2020-12-31 03:28

    I ran into a similar issue and this post with:

    "*** AttributeError: 'GaussianProcessRegressor' object has no attribute '_y_train_mean"

    when I updated scikit-learn and loaded a pickled model and attempted to predict using the model. I simply needed to retrain the model and it solved my issue.

    0 讨论(0)
  • 2020-12-31 03:35

    I solved this issue by inserting the following lines of code:

    import sklearn 
    from sklearn.linear_model import LinearRegression
    
    0 讨论(0)
  • 2020-12-31 03:38

    Another cause of this problem (not the problem with the OP's code) - but the one that got me - is that python does not automatically import subpackages or modules unless that is explicitly done by the package developer. And sklearn does not automatically import its subpackages, so if you have

    import sklearn 
    diabetes = sklearn.datasets.load_diabetes()
    

    then you will get

    AttributeError: module 'sklearn' has no attribute 'datasets'
    

    This is a highly misleading error message, because sklearn does have a subpackage called datasets - you just need to import it explicitly

    import sklearn.datasets 
    diabetes = sklearn.datasets.load_diabetes()
    
    0 讨论(0)
  • 2020-12-31 03:41

    OK.. Found it finally.. Posting it here in case someone will get into the same problem.

    I had another version of sklearn (probably because of apt-get install) in a different directory. It was partially installed somehow but it was the one that got loaded.

    Make sure to look at your pip script's output to see where does it install the package, and when you load it from python, check sklearn.__path__ to see where is it taking it from.

    0 讨论(0)
  • 2020-12-31 03:42

    Try this:

    from sklearn.datasets import load_diabetes
    diabetes = load_diabetes()
    
    0 讨论(0)
提交回复
热议问题