导入sklearn.model_selection.train_test_split与导入sklearn.model_selection为sm

时间:2020-02-03 11:19:33

标签: python scikit-learn

/directus/public/server/ping

解释器无法找到train_test_split模块。

>>> 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'

但是使用 as 导入可以解决此问题,为什么? 导入导入模块的名称之间有什么区别。根据{{​​3}},这些应该相同。

3 个答案:

答案 0 :(得分:1)

您无法在python中导入函数。您应该使用以下格式从python库中导入它

from sklearn.model_selection import train_test_split

或导入模块并使用其中的功能

import sklearn.model_selection as sm
sm.train_test_split

答案 1 :(得分:1)

您不能那样做。 导入函数的格式为:

from sklearn.model_selection import train_test_split

或者您可以导入整个模块并按以下方式调用其功能:

import sklearn.model_selection

model_selection.train_test_split(params)

OR:

import sklearn.model_selection as sk

sk.train_test_split(params)

答案 2 :(得分:1)

您要比较的内容有本质的区别。在第一行中,您正在执行import function,而在第二行中,您正在执行from package import module as ALIAS。第一个是不正确的,您可以导入模块,但要专门指定功能。在第二种情况下,您将使用正确的别名导入模块。如果要导入不带别名的模块,也可以。

import sklearn.model_selection.train_test_split

与以下项目不具有可比性:

import sklearn.model_selection as sm

这行是可比较的,其工作原理与以下相同:

import sklearn.model_selection
相关问题