2
$\begingroup$

Isn't scikit-learn version 1.0.2 supposed to have an attribute datasets? If so, why am I getting an error?

Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn as skl
>>> skl.__version__
'1.0.2'
>>> data = skl.datasets.load_diabetes()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'sklearn' has no attribute 'datasets'
$\endgroup$

1 Answer 1

1
$\begingroup$

Works fine for me:

import sklearn
from sklearn.datasets import load_diabetes
X, y = load_diabetes(return_X_y=True)

print('The scikit-learn version is {}.'.format(sklearn.__version__))
The scikit-learn version is 0.20.3.

See docs and example.

$\endgroup$
4
  • $\begingroup$ You're importing slightly differently from me, though. Could that be the reason? $\endgroup$
    – Tfovid
    Commented Feb 16, 2022 at 10:21
  • $\begingroup$ Yes, try it. I have a old version, but guess it is the same import $\endgroup$
    – Peter
    Commented Feb 16, 2022 at 10:27
  • $\begingroup$ I did and it works, thanks. But if you try the way I import it, then it doesn't work, and I'm wondering why. $\endgroup$
    – Tfovid
    Commented Feb 16, 2022 at 10:29
  • 1
    $\begingroup$ The reason is that the modules inside scikit-learn like datasets are not loaded automatically when loading the top level "sklearn" module. This is to avoid having to load all the modules you do not actually use. Many smaller packages will import their submodules into the top module, and in that case it does not matter. $\endgroup$
    – Jon Nordby
    Commented Feb 16, 2022 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.