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
followed by
features_train, features_test, labels_train, labels_test = train_test_split(word_data, authors, test_size=0.1, random_state=42)
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
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 model_selection
:
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.