我正在尝试将数据集拆分为训练集和测试集。我尝试为此使用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)
有什么正确的方法可以导入吗?我应该怎么做?
答案 0 :(得分:0)
关闭内核,然后重新启动。如果它不起作用,请停用anaconda,然后再次将其激活。再次运行它,它将运行。就我而言,我在运行时遇到了同样的问题-from imblearn.under_sampling import RandomUnderSampler