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

后端 未结 9 714
失恋的感觉
失恋的感觉 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: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()
    

提交回复
热议问题