找不到scikit-learn train_test_split模块

时间:2019-12-06 17:29:11

标签: python scikit-learn regression data-science

我正在尝试将数据集拆分为训练集和测试集。我尝试为此使用scikit-learn的train_test_split模块,将其导入为:

from sklearn.model_selection import train_test_split

将导致以下错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-28-d05cc6ee7707> in <module>
----> 1 from sklearn.cross_validation import train_test_split

ModuleNotFoundError: No module named 'sklearn.cross_validation'

然后我在stackoverflow中找到了一些有关此的文章,他们说您应该使用以下代码导入该模块:

from sklearn.model_selection import train_test_split
再次使用

引起此问题:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-29-73edc048c06b> in <module>
----> 1 from sklearn.model_selection import train_test_split

~\Anaconda3\lib\site-packages\sklearn\model_selection\__init__.py in <module>
----> 1 from ._split import BaseCrossValidator
      2 from ._split import KFold
      3 from ._split import GroupKFold
      4 from ._split import StratifiedKFold
      5 from ._split import TimeSeriesSplit

~\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py in <module>
     20 import numpy as np
     21 
---> 22 from ..utils import indexable, check_random_state, _safe_indexing
     23 from ..utils import _approximate_mode
     24 from ..utils.validation import _num_samples, column_or_1d

ImportError: cannot import name '_safe_indexing' from 'sklearn.utils' (C:\Users\amir\Anaconda3\lib\site-packages\sklearn\utils\__init__.py)

有什么正确的方法可以导入吗?我应该怎么做?

1 个答案:

答案 0 :(得分:0)

关闭内核,然后重新启动。如果它不起作用,请停用anaconda,然后再次将其激活。再次运行它,它将运行。就我而言,我在运行时遇到了同样的问题-from imblearn.under_sampling import RandomUnderSampler