If I use from sklearn import * or from skleanr import datasets, then I can use datasets in the following way: iris = datasets.load_iris().
However, import sklearn and import sklearn as sk do not work as I expect. For example I cannot use sklearn.datasets.import_iris() or sk.datasets.import_iris(). Do I misinterpret the import syntax? And it it is the case, what is the correct way to use import.
No, you are not misinterpreting it. It’s the package structure of this particular project.
When you import
sklearn, you import a special python file__init__.pyin a directorysklearn, that has inside of it another package calleddatasets. But ifsklearnitself doesn’t import the nested package into it’s__init__.pyfile, you cannot use attribute traversal to get to that nested package.The solution is to import the nested package explicitly, yourself: