Modulenotfounderror no module named sklearn.cross_validation

In this tutorial, we will cover why we encounter Modulenotfounderror: no module named sklearn.cross_validation error while developing in Python.

This error can be frustrating, especially if you are new to Python programming.

But don’t worry, in this article, we will explain what this error means, what causes it, and most importantly, how to fix it.

Let’s get started…

What is Modulenotfounderror: no module named ‘sklearn.cross_validation’?

Modulenotfounderror: no module named ‘sklearn.cross_validation’ occurs since scikit-learn has evolved over the years, and some changes have been made to the library, particularly in version 0.20.

One of the significant changes was the removal of the cross_validation module.

The module was deprecated in version 0.18 and was completely removed in version 0.20.

Therefore, if you try to import the cross_validation module in Scikit-learn version 0.20 or later.

You will encounter the Modulenotfounderror: no module named sklearn.cross_validation error.

Why Modulenotfounderror no module named sklearn cross-validation Occur?

As mentioned above we encountered this error due to changes made to the library.

So the error occurs when a user tries to import the cross_validation module from scikit-learn (sklearn) in Python.

However the module is either not installed or deprecated in the latest version of scikit-learn.

Take a look at this case, supposedly we are going to split pandas DataFrame or NumPy array dataset into random train and test subsets.

Basically, we do the import train_test_split function with the cross_validation like this:

from sklearn.cross_validation import train_test_split

Since in scikit-learn version 0.20, the cross_validation module was deprecated and removed.

Then train_test _split function is now available from the model_selection module.

Therefore, it will throw Modulenotfounderror: no module named sklearn.cross_validation.

So in order to fix this, we will move to…

How to solved Modulenotfounderror no module named sklearn.cross_validation

To fix the error we present above, here is a simple way to do it.

  1. Change the import statement.

    To fix the error all we have to do is to change the import statement given from:

    from sklearn.cross_validation import train_test_split

    To this:

    from sklearn.model_selection import train_test_split

  2. Now we can split the dataset with the training set and a test set.


    sklearn test code

    This should let you use the train_test_split function without causing the error.

    Modulenotfounderror no module named sklearn.cross_validation solved

Conclusion

In conclusion, if you encounter the Modulenotfounderror: no module named sklearn.cross_validation error while using Scikit-learn in Python, it is most likely because you are using version 0.20 or later.

The solution to this problem is to replace cross_validation with model_selection in your code

That’s it we have provided solutions for fixing this error. If you follow the steps and encounter any further errors, feel free to ask for more help!

If you are interested in this kind of tutorial we also have Modulenotfounderror: no module named cryptodome.

Leave a Comment