How to Fix "AttributeError: 'SimpleImputer' Object Has No Attribute '_validate_data' in PyCaret" using Python?
Last Updated :
21 Jun, 2024
In this article, we'll address a common error encountered when using the PyCaret library in Python: AttributeError: 'SimpleImputer' object has no attribute '_validate_data'. This error typically arises during the data preprocessing phase specifically when PyCaret tries to use the SimpleImputer from the scikit-learn library. We'll explain the problem in detail show how to reproduce it and provide the different solutions to resolve it.
Problem Statement
When working with the PyCaret we might encounter an AttributeError similar to the following:

This error usually occurs when there is a version mismatch between the PyCaret and its dependencies especially scikit-learn. The SimpleImputer class in recent versions of the scikit-learn includes the _validate_data method which older versions may not have.
Showing the Problem
Here's an example that reproduces the error:
Python
from pycaret.datasets import get_data
from pycaret.classification import setup
# Load dataset
data = get_data('juice')
# Initialize setup
clf1 = setup(data, target='Purchase')
Running this code might lead to the following error:
AttributeError: 'SimpleImputer' object has no attribute '_validate_data'
Approach to Solving the Problem
To resolve this issue we need to the ensure compatibility between the PyCaret and its dependencies particularly scikit-learn. There are a few approaches to the tackle this problem:
- Updating scikit-learn: Ensure that you are using the compatible version of the scikit-learn.
- Updating PyCaret: Use the latest version of the PyCaret which is likely to be compatible with the latest dependencies.
- Downgrading PyCaret: Use an older version of the PyCaret that is compatible with the current scikit-learn version.
- Creating a Virtual Environment: The Set up a virtual environment with specific versions of the PyCaret and scikit-learn that are known to be compatible.
Different Solutions to Solve the Error
Solution 1: Update scikit-learn
First, try updating scikit-learn to the latest version:
pip install --upgrade scikit-learn
Solution 2: Update PyCaret
Ensure that we have the latest version of PyCaret:
pip install --upgrade pycaret
Solution 3: Downgrade PyCaret
If updating scikit-learn does not resolve the issue we might need to the downgrade PyCaret to a version compatible with the scikit-learn. For example:
pip install pycaret==2.3.5
Solution 4: Create a Virtual Environment
Create a new virtual environment and install compatible versions of the PyCaret and scikit-learn:
python -m venv pycaret_env
source pycaret_env/bin/activate # On Windows use `pycaret_env\Scripts\activate`
pip install pycaret==2.3.5 scikit-learn==0.24.2
Example Code
Here's an example showing how to resolve the issue by the downgrading PyCaret:
pip install pycaret==2.3.5 scikit-learn==0.24.2
Now, let's run the initial example again:
Python
from pycaret.datasets import get_data
from pycaret.classification import setup
# Load dataset
data = get_data('juice')
# Initialize setup
clf1 = setup(data, target='Purchase')
Expected Output
With the compatible versions the setup should initialize without the errors:
Setup Succesfully Completed!
Conclusion
The AttributeError: 'SimpleImputer' object has no attribute '_validate_data' in PyCaret can be resolved by the ensuring compatibility between the PyCaret and its dependencies. By updating or downgrading the libraries or by the setting up a controlled virtual environment we can effectively eliminate this error and continue with the data science workflows in PyCaret.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read