ImportError: No module named model_selection

后端 未结 11 594
攒了一身酷
攒了一身酷 2021-01-30 08:16

I am trying to use train_test_split function and write:

from sklearn.model_selection import train_test_split

and this causes

相关标签:
11条回答
  • 2021-01-30 08:43

    I guess you have the wrong version of scikit-learn, a similar situation was described here on GitHub. Previously (before v0.18), train_test_split was located in the cross_validation module:

    from sklearn.cross_validation import train_test_split
    

    However, now it's in the model_selection module:

    from sklearn.model_selection import train_test_split
    

    so you'll need the newest version.

    To upgrade to at least version 0.18, do:

    pip install -U scikit-learn
    

    (Or pip3, depending on your version of Python). If you've installed it in a different way, make sure you use another method to update, for example when using Anaconda.

    0 讨论(0)
  • 2021-01-30 08:45

    Adding some info to the previous answer from @linusg :

    sklearn keeps a release history of all its changes. Think of checking it from time to time. Here is the link to the documentation.

    As you can see in the documentation for the version 0.18, a new module was created called model_selection. Therefore it didn't exist in previous versions.

    Update sklearn and it will work !

    0 讨论(0)
  • 2021-01-30 08:46

    Update sklearn

    conda update scikit-learn

    0 讨论(0)
  • 2021-01-30 08:49

    Your sklearn version is too low, model_selection is imported by 0.18.1, so please update the sklearn version.

    0 讨论(0)
  • 2021-01-30 08:50

    As @linusg said, one option is just import crossvalidation as follows:

    from sklearn import cross_validation
    X_train,X_test,y_train,y_test = cross_validation.train_test_split(X,y,test_size=0.3)
    
    0 讨论(0)
提交回复
热议问题