ImportError: No module named sklearn.cross_validation

后端 未结 14 1945
天涯浪人
天涯浪人 2020-12-04 08:14

I am using python 2.7 in Ubuntu 14.04. I installed scikit-learn, numpy and matplotlib with these commands:

sudo apt-get install build-essential python-dev p         


        
相关标签:
14条回答
  • 2020-12-04 08:27

    sklearn.cross_validation is now changed to sklearn.model_selection

    Just use

    from sklearn.model_selection import train_test_split
    

    I think that will work.

    0 讨论(0)
  • 2020-12-04 08:27

    If you have code that needs to run various versions you could do something like this:

    import sklearn
    if sklearn.__version__ > '0.18':
        from sklearn.model_selection import train_test_split
    else:
        from sklearn.cross_validation import train_test_split
    

    This isn't ideal though because you're comparing package versions as strings, which usually works but doesn't always. If you're willing to install packaging, this is a much better approach:

    from packaging.version import parse
    import sklearn
    if parse(sklearn.__version__) > parse('0.18'):
        from sklearn.model_selection import train_test_split
    else:
        from sklearn.cross_validation import train_test_split
    
    0 讨论(0)
  • 2020-12-04 08:33

    I guess cross selection is not active anymore. We should use instead model selection. You can write it to run, from sklearn.model_selection import train_test_split

    Thats it.

    0 讨论(0)
  • 2020-12-04 08:34

    Make sure you have Anaconda installed and then create a virtualenv using conda. This will ensure all the imports work

    Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    Anaconda is brought to you by Continuum Analytics.
    Please check out: http://continuum.io/thanks and https://binstar.org
    >>> from sklearn.cross_validation import train_test_split
    
    0 讨论(0)
  • 2020-12-04 08:36

    Past : from sklearn.cross_validation (This package is deprecated in 0.18 version from 0.20 onwards it is changed to from sklearn import model_selection).

    Present: from sklearn import model_selection

    Example 2:

    Past : from sklearn.cross_validation import cross_val_score (Version 0.18 which is deprecated)

    Present : from sklearn.model_selection import cross_val_score

    0 讨论(0)
  • 2020-12-04 08:36

    train_test_split is part of the module sklearn.model_selection, hence, you may need to import the module from model_selection

    Code:

    from sklearn.model_selection import train_test_split
    
    0 讨论(0)
提交回复
热议问题