iPython Notebook Python 2
Complaining about this line:
from sklearn.model_selection import train_test_split
Why isn't model selection working?
In order to remedy this issue, you need to first find out if you are importing the actual sklearn
package, and not just some script with the name sklearn.py
saved somewhere in your working directory. The way Python imports modules is somewhat similar to the way it finds variables in its namespace (Local
, Enclosed
, Global
, Built-in
). In this case, Python will start importing module by first looking in the current directory and then the site-packages
. If it looks in the current working directory and finds a python script with the same name as the module you are trying to import, then it will import that script instead of the actual module.
You can usually find out whether or not the actual module is imported by checking its __file__
or __path__
attribute:
import sklearn
print(sklearn.__file__)
print(sklearn.__path__)
Reviewing the output of those print statements will tell you whether the imported package is the module you are after, or just some script lying somewhere in your working directory. If, in case, the output does not point to the site-packages
of your Python version then you have imported some script somewhere that's not the module itself. Your quick fix would be to exit the console first, rename the .py
script and its compiled version (the .pyc
file) and go back to your console and try again.
However, if the output points to your python version's site-packages, then there is something wrong with how the package was installed in the first place. In which case, you will probably need to update or reinstall it.
Particular to this, it turns out that the issue is with the version of sklearn
you are using; because the model_selection
module in sklearn
is available in versions 0.18+
. If you're using a version number (sklearn.__version__
) lower than 0.18
, then you will have to use the old cross_validation
module instead of the model_selection
module:
from sklearn.cross_validation import train_test_split
You can also just upgrade to the latest version of the package with your preferred package management system.
I hope this is helpful.