ImportError: No module named sklearn.cross_validation

后端 未结 14 1944
天涯浪人
天涯浪人 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

    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
    

提交回复
热议问题