简体   繁体   中英

import sklearn.model_selection.train_test_split vs import sklearn.model_selection as sm

>>> import sklearn.model_selection.train_test_split
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sklearn.model_selection.train_test_split'

The interpreter is unable to find the train_test_split module.

>>> import sklearn.model_selection as sm
>>> sm.train_test_split
<function train_test_split at 0x000001841E8FB7B8>

But importing using as solves the issue, why? Is there any difference between import and import module as name . According to Python : 'import module' vs 'import module as' these should be same.

You can't import a function in python. you should import it from the library in python with the following format

from sklearn.model_selection import train_test_split

or import a module and the use a function from it

import sklearn.model_selection as sm
sm.train_test_split

You can't do that. The format to import a function is :

from sklearn.model_selection import train_test_split

or you can import the entire module and call its function like:

import sklearn.model_selection

model_selection.train_test_split(params)

OR:

import sklearn.model_selection as sk

sk.train_test_split(params)

There is an essential difference in what you are comparing. In the first line you are doing import function vs. second line from package import module as ALIAS . The first one is incorrect, you can import the module, but the function specifically. In the second case, you are importing the module with an alias, which is correct. If you were to import the module without alias, you would also be fine.

import sklearn.model_selection.train_test_split

Is not comparable to:

import sklearn.model_selection as sm

This line, is comparable and will work the same as:

import sklearn.model_selection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM