I cannot import the cross_validation from sklearn library; I use sklearn version 0.20.0
from sklearn import cross_validation
later in the code:
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(word_data, authors, test_size=0.1, random_state=42)
Traceback (most recent call last): File "D:meM.ScUdacity_ML_courseud120-projects- masternaive_bayesnb_author_id.py", line 16, in <module> from email_preprocess import preprocess File "../tools/email_preprocess.py", line 8, in <module> from sklearn import cross_validation ImportError: cannot import name cross_validation
This happens because there is no
cross_validation object in
sklearn. You’re likely looking for something more like the
cross_validate function. You can access that through
from sklearn.model_selection import cross_validate
However, you don’t need to import any cross-validation software to perform the train-test split, since that will just randomly sample from the data. Try
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = train_test_split(word_data, authors, test_size=0.1, random_state=42)
cross_validation used to exist as a Scikit package*, but was deprecated at some point.
If you’re looking for
train_test_split as your code indicates, it’s in
from sklearn import model_selection features_train, features_test, labels_train, labels_test = model_selection.train_test_split( word_data, authors, test_size=0.1, random_state=42)
*Looks like this changed in 0.18.
In my case, I was using some files from a Udacity course, which used an older version of sklearn. Instead of spending unnecessary time reformatting code usage to meet the latest versions of all their dependencies, it was easier to install the old version.
This was possible because they provide a requirements.txt file.
python -m pip install -r requirements.txt
In my case I was also trying to install an old version of sklearn which is required for mini projects for ‘Intro to Machine Learning’ Udacity course.
I use Miniconda 3 with Python 2 environment on Windows 10.
Unfortunately, @Ben B’s method with
pip didn’t work for me. I had errors which look like errors in the scipy github issue:
Installing collected packages: scipy Running setup.py install for scipy ... error Complete output from command "c:program filespython2.xpython.exe" -u -c "import setuptools, tokenize;__file__='c:\users\reacodes\appdata\local\temp\pip-build-jzv_lz\scipy\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record c:usersreacodesappdatalocaltemppip-mqeonc-recordinstall-record.txt --single-version-externally-managed --compile: lapack_opt_info: openblas_lapack_info: libraries openblas not found in ['c:\program files\python\2.x\lib', 'C:\', 'c:\program files\python\2.x\libs'] NOT AVAILABLE lapack_mkl_info: mkl_info: libraries mkl,vml,guide not found in ['c:\program files\python\2.x\lib', 'C:\', 'c:\program files\python\2.x\libs'] NOT AVAILABLE ...
So I tried another method with
conda described in the following answer:
conda install -c free scikit-learn=0.18.0