2

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'
Peter
  • 7,446
  • 5
  • 19
  • 49
Tfovid
  • 195
  • 3
  • 6

1 Answers1

1

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.

Peter
  • 7,446
  • 5
  • 19
  • 49
  • You're importing slightly differently from me, though. Could that be the reason? – Tfovid Feb 16 '22 at 10:21
  • Yes, try it. I have a old version, but guess it is the same import – Peter Feb 16 '22 at 10:27
  • 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. – Tfovid Feb 16 '22 at 10:29
  • 1
    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. – Jon Nordby Feb 16 '22 at 16:12