Solving Python scikit-learn ImportError: No module named sklearn.cross_validation

Python scikit-learn ImportError: No module named sklearn.cross_validation is an error which occurs when you try using cross_validation with scikit-learn.

In this article I am going to explain to you why this scikit-learn error is happening and how to solve it in the most efficient way possible.

Exploring the Python scikit-learn ImportError: No module named sklearn.cross_validation

This is an error which occurs when you try using cross_validation with scikit-learn.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
ImportError: No module named sklearn.cross_validation
                                                                       #

Bellow I will present multiple methods, some have worked for me and others have worked for other devs.

The Method that fixed my problem : Use an alternative to the deprecated sklearn.cross_validation

When you try using cross_validation with scikit-learn you will definitely end up with this error, since cross_validation is deprecated since sklearn 20.

So instead of using the import statement bellow

                                                                       #
from sklearn.cross_validation import train_test_split
                                                                       #

You should instead use sklearn.model_selection, just like in the statement bellow

                                                                       #
from sklearn.model_selection import train_test_split
                                                                       #

This is not specific to train_test_split , but you can use it with all sorts of sklearn functions. like cross_val_score for example.

                                                                       #
from sklearn.model_selection import cross_val_score
                                                                       #

or the learning_curve function

                                                                       #
from sklearn.model_selection import learning_curve
                                                                       #

Try the method above I can say with confidence that in 90 percent of cases this is going to solve the problem.

If the method above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up : 

This is the end of this article, I hope we helped you solve the error : Python scikit-learn ImportError No module named sklearn cross validation or at least show you why it happens and guided you in the right direction. If you want to help, you can donate to our Kofi account.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/