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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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



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 and Ubuntu 13.04


pip install -U scikit-learn



When I try to run the first lines of code there I get an error and it happens every time I'm trying to run anything with scikit-learn.


import pylab as pl
import numpy as np
from sklearn import datasets, linear_model

# Load the diabetes dataset
diabetes = datasets.load_diabetes()



I get the following:


AttributeError: 'module' object has no attribute 'load_diabetes'



When I try:


regr = linear_model.LinearRegression()



I get :


AttributeError: 'module' object has no attribute 'LinearRegression'



It seems to me that it's either I'm using the package wrong (but I've copied from their tutorial), or I've installed something wrong (but the package is loaded successfully).



Can anyone help?





Does from sklearn.linear_model import LinearRegression work? That's the recommended way of importing.
– Fred Foo
May 25 '13 at 15:44


from sklearn.linear_model import LinearRegression





thanks larsmans, tried that too. after a lot of digging I found another (bad) version of sklearn installed (in addition to what pip installed) and that was what caused the problem. I deleted it, reinstalled with pip just in case and now everything works fine.
– Zach Moshe
May 26 '13 at 6:51




7 Answers
7



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.


pip


sklearn.__path__





Why is the load_diabetes() dataset not labelled? Isn't it a cardinal sin in data science to have unlabeled axes/unlabeled data? I am using the diabetes data set for a mandatory school project and I can't find a single source that would help me understand what all these floating point numbers represent, and it is quite frustrating! It seems to me that given how many different professors through the years explained how unlabeled axes are a cardinal sin in mathematics, it doesn't seem very far-removed that a data set without a keys() fnct that indicates what the columns represent is just as bad..
– Arthur Collé
Sep 22 '14 at 22:09



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


sklearn


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


sklearn


datasets


import sklearn.datasets
diabetes = sklearn.datasets.load_diabetes()





that is exactly what the problem is , i installed and uninstalled it many times but it did'nt worked , but importing the subpackage directly worked , :(
– Vipin Chaudhary
Jul 15 '17 at 5:49



This worked for me:


from sklearn.datasets import make_moons



I faced same issue but then i realized that my program name was sklearn.py . In case if any one sees this type of error also check that you program name is not same as the package name , or else you will get module object has no attribute error, as in the question .


sklearn.py


module object has no attribute error



It seems that the package loaded from sklearn were the one from the distribution library and not the one installed from pip. Solution for me (debian) was to reinstall the pip package. This can be checked with:


import sklearn
sklearn.__path__



If this shows /usr/lib/python/, then is it using the distribution.


/usr/lib/python/



The problem got solved by uninstalling and reinstalling sklearn.


$ pip uninstall scikit-learn
$ pip install scikit-learn



Try this:


from sklearn.datasets import load_diabetes
diabetes = load_diabetes()



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions