You might face ImportError: No module namedsklearn.cross_validation error when working with sklearn module. The error occurs even if you have installed sklearn on your system. Usually, we face this problem, when we tried to import train and test split from sklearn.cross_validation. In this short article, we will learn how we can solve ImportError: No module namedsklearn.cross_validation using various methods. We will also discuss how to understand such errors so that in the future we will be able to solve them easily.

ImportError: No module namedsklearn.cross_validation – Possible solutions
The error ImportError: No module namedsklearn.cross_validation occurs when you tried to import the train and test split function from the sklearn module. Although you have successfully installed sklearn module but still getting this error:
# importing train test split function
from sklearn.cross_validation import train_test_split
Output:
ImportError: No module named sklearn.cross_validation
The reason for getting this error is because the sklearn.cross_validation is no more active in the sklearn module. Instead, now, there is model_selection which can be used to import train_test_split.
In this short article, will go through the following possible solutions:
- Using model_selection
- Degrading sklearn module
- Alternative methods
- Installing sklearn module
Now let us jump into the possible solutions and explain each one with examples.
Solution-1: Using model_selection
As we said coss_validation module is not more active in sklearn module. In order to get the train_test_split function, we have to import it from sklearn.model_selection as shown below:
#importing the submodule
from sklearn.model_selection import train_test_split
When you run the above code and remove the previous import, you will get rid of the problem.
Solution-2: Degrading the sklearn module
As we discussed that the cross-validation module is no more active in the new versions of sklearn but it is still working in the old versions. So, if you for some cases need to run the code with an older version, but get ImportError: No module namedsklearn.cross_validation error then you can degrade your module.
You can uninstall the module and then install the old version on your system.
# uninstalling the module
pip uninstall scikit-learn
# using pip3
pip3 uninstall scikit-learn
# installing specific version
pip install scikit-learn==0.18
This method is not recommended but can be used as an alternative.
Solution-3: Alternative method
If you have code that needs to run various versions you could do something like this in order to get rid of the error:
# importing the sklearn module
import sklearn
# using if-else to check the versions
if sklearn.__version__ > '0.18':
from sklearn.model_selection import train_test_split
else:
from sklearn.cross_validation import train_test_split
Or here is a better version of the same method:
from packaging.version import parse
import sklearn
if parse(sklearn.__version__) > parse('0.18'):
from sklearn.model_selection import train_test_split
else:
from sklearn.cross_validation import train_test_split
This method is also not recommended but can be used as an alternative.
Solution-4: Installing sklearn module
If you haven’t installed the sklearn module on your system yet then you can use any of the following methods to install the module and then import the train_test_split function.
Installing using pip command:
# using Python 2
pip install scikit-learn scipy matplotlib numpy
# pip3.10
pip3 install scikit-learn scipy matplotlib numpy
If the pip command is not in the path then you can use the following commands:
# if you don't have pip in your PATH environment variable
python -m pip install scikit-learn scipy matplotlib numpy
# for python 3
python3 -m pip install scikit-learn scipy matplotlib numpy
Alternatively, you can use the following commands as well:
py -m pip install scikit-learn scipy matplotlib numpy
# if you get permissions error
sudo pip3 install scikit-learn scipy matplotlib numpy
pip install scikit-learn scipy matplotlib numpy --user
# for Anaconda
conda install -c anaconda scikit-learn
# for Jupyter Notebook
!pip install scikit-learn scipy matplotlib numpy
Hopefully, these methods will help you to get rid of ImportError: No module namedsklearn.cross_validation error.
Understanding the ImportError: No module namedsklearn.cross_validation error error
In Python, the errors give a lot of information about themselves. Usually, there are two main parts of errors in Python. The first part gives information about the type of error which in this case is ImportError which means there the script couldn’t import some modules. While the second part of the error gives more specific information about the error which in this case helps us to figure out which module is not being imported by the script.
What is ImportError in Python?
This error typically appears when one of the following prevents the import of a class: There is a cyclic dependency on the imported class. The imported class either doesn’t exist or wasn’t made. The class name imported is spelled incorrectly. The module’s imported class is in the wrong place.
What is the Sklearn module?
In the Python ecosystem, Scikit-learn, an open-source data analysis toolkit, is considered to be the pinnacle of machine learning (ML). Important ideas and traits include: algorithms for making decisions, such as: Identifying and classifying data based on patterns is called classification.
What is cross-validation in sklearn?
Cross-validation is a crucial machine learning concept that benefits data scientists in two essential ways: it can reduce the amount of data needed and makes sure the artificial intelligence model is reliable.
What is model_selection in sklearn?
Model selection is the process of selecting one final machine learning model from among a collection of candidate machine learning models for a training dataset. Model selection is a process that can be applied both across different types of models (e.g. logistic regression, SVM, KNN, etc.)
Summary
In this short article, we learned how we can solve ImportError: No module namedsklearn.cross_validation error using various methods. We discussed three different methods to get rid of ImportError: No module namedsklearn.cross_validation error. Moreover, we also discussed how to understand the errors in Python.
Related Issues:
- AttributeError: module ‘seaborn’ has no attribute ‘histplot’
- TypeError: only size-1 arrays can be converted to Python scalars solved
- Importerror: numba needs numpy 1.21 or less-Solved
- Typeerror: type numpy.ndarray doesnt define round method solved
- AttributeError: ‘list’ object has no attribute ‘split’
- ModuleNotFoundError: No module named ‘cv2’
- TypeError: not all arguments converted during string formatting
- Typeerror: ‘float’ object is not iterable
- Importerror: DLL load failed: the specified procedure could not be found
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- ModuleNotFoundError: No module named ‘bs4’
- Typeerror: string indices must be integers
- TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
- TypeError: only integer scalar arrays can be converted to a scalar index
- ‘numpy.ndarray’ object has no attribute ‘append’
- numpy.core.multiarray failed to import
- Attributeerror: module matplotlib has no attribute subplots