In Python, libraries can have updates that result in changes to the names of modules, classes, and functions. If you are using Scikit-Learn and attempt to import the cross-validation module you will raise the error: ModuleNotFoundError: No module named ‘sklearn.cross_validation’.
This tutorial will go through how to solve this problem with the help of a code example.
ModuleNotFoundError: No module named ‘sklearn.cross_validation’
Solution
You may get this error when trying to access a method from the cross-validation module, for example:
from sklearn.cross_validation import train_test_split
Which will raise the error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
1 from sklearn.cross_validation import train_test_split
ModuleNotFoundError: No module named 'sklearn.cross_validation'
All of the methods previously under cross-validation are now under model_selection. Therefore, you need to import train_test_split from sklearn.model_selection. Let’s look at the revised code:
from sklearn.model_selection import train_test_split
This import statement will not raise the modulenotfounderror and you can use the train_test_split helper function.
If you are not able to import the Scikit-Learn library at all, go to the following article to solve this problem: How to Solve Python ModuleNotFoundError: no module named ‘sklearn’.
Summary
Congratulations on reading to the end of this tutorial! The error ModuleNotFoundError: No module named ‘sklearn.cross_validation’ occurs when you try to import functions or classes from the cross_validation module instead of model_selection module. The cross_validation module no longer exists in Scikit-Learn.
For similar problems, you can search for the function or class you want to use in the Scikit-Learn API Reference. You will be able to find which module the class or function you want belongs to.
To learn more about Python for data science and machine learning, go to the online courses page for Python.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.