Python 无法在 scikit-learn 中导入 sklearn.model_selection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40615021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:44:11  来源:igfitidea点击:

Cannot import sklearn.model_selection in scikit-learn

pythonscikit-learn

提问by Echo

I am trying to import sklearn.model_selection. I have tried to reinstall scikit-learn and anaconda, still not working. Here is the error msg I got:

我正在尝试导入sklearn.model_selection. 我尝试重新安装 scikit-learn 和 anaconda,但仍然无法正常工作。这是我收到的错误消息:

ImportError                               Traceback (most recent call last)
<ipython-input-69-e49df3a70ea4> in <module>()
      4 get_ipython().magic(u'matplotlib inline')
      5 # from sklearn.model_selection import train_test_split
----> 6 import sklearn.model_selection

/Users/Lu/anaconda/lib/python2.7/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

/Users/Lu/anaconda/lib/python2.7/site-packages/sklearn/model_selection/_split.py in <module>()
     34 from ..utils.random import choice
     35 from ..base import _pprint
---> 36 from ..gaussian_process.kernels import Kernel as GPKernel
     37 
     38 __all__ = ['BaseCrossValidator',

/Users/Lu/anaconda/lib/python2.7/site-packages/sklearn/gaussian_process/__init__.py in <module>()
     11 """
     12 
---> 13 from .gpr import GaussianProcessRegressor
     14 from .gpc import GaussianProcessClassifier
     15 from . import kernels

/Users/Lu/anaconda/lib/python2.7/site-packages/sklearn/gaussian_process/gpr.py in <module>()
     10 import numpy as np
     11 from scipy.linalg import cholesky, cho_solve, solve_triangular
---> 12 from scipy.optimize import fmin_l_bfgs_b
     13 
     14 from sklearn.base import BaseEstimator, RegressorMixin, clone

/Users/Lu/anaconda/lib/python2.7/site-packages/scipy/optimize/__init__.py in <module>()
    232 from .optimize import *
    233 from ._minimize import *
--> 234 from ._root import *
    235 from .minpack import *
    236 from .zeros import *

/Users/Lu/anaconda/lib/python2.7/site-packages/scipy/optimize/_root.py in <module>()
     17 
     18 from .optimize import MemoizeJac, OptimizeResult, _check_unknown_options
---> 19 from .minpack import _root_hybr, leastsq
     20 from ._spectral import _root_df_sane
     21 from . import nonlin

/Users/Lu/anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py in <module>()
      2 
      3 import warnings
----> 4 from . import _minpack
      5 
      6 import numpy as np

ImportError: cannot import name _minpack

采纳答案by ébe Isaac

Check your scikit-learn version;

检查您的 scikit-learn 版本;

import sklearn
print(sklearn.__version__)

sklearn.model_selectionis available for version 0.18.1.

sklearn.model_selection适用于 0.18.1 版。

What you need to import depends on what you require. For instance, in version 0.18.1, GridSearchCVcan be imported as

您需要导入什么取决于您需要什么。例如,在 0.18.1 版本中,GridSearchCV可以导入为

from sklearn.model_selection import GridSearchCV

Whereas in version 0.17.1, the same can be imported as

而在 0.17.1 版本中,相同的可以导入为

from sklearn.grid_search import GridSearchCV

If you find anything in the new scikit documentation that doesn't work for you in your system, then search the document for the current version you are using. The import path might be different but the overall functionality ought to be the same.

如果您在新的 scikit 文档中发现在您的系统中不适合您的任何内容,请在该文档中搜索您正在使用的当前版本。导入路径可能不同,但整体功能应该是相同的。

If you don't have any previous projects or code that requires the older version, the better option would be to update your scikit-learn package. As you stated that you use Anaconda, the following post would be useful:

如果您之前没有任何需要旧版本的项目或代码,更好的选择是更新您的 scikit-learn 包。正如您所说,您使用 Anaconda,以下帖子会很有用:

How to upgrade scikit-learn package in anaconda

如何在 anaconda 中升级 scikit-learn 包