AttributeError: 'SMOTE' object has no attribute '_validate_data'
Asked Answered
B

7

18

I'm resampling my data (multiclass) by using SMOTE.

sm = SMOTE(random_state=1)
X_res, Y_res = sm.fit_resample(X_train, Y_train)

However, I'm getting this attribute error. Can anyone help?

Barbitone answered 17/6, 2020 at 18:48 Comment(0)
S
20

Short answer

You need to upgrade scikit-learn to version 0.23.1.

Long answer

The newest version 0.7.0 of imbalanced-learn seems to have an undocumented dependency on scikit-learn v0.23.1. It would give you AttributeError: 'SMOTE' object has no attribute '_validate_data' if your scikit-learnis 0.22 or below.

If you are using Anaconda, installing scikit-learn version 0.23.1 might be tricky. conda update scikit-learn might not update scikit-learn version 0.23 or higher because the newest scikit-learn version Conda has at this point of time is 0.22.1. If you try to install it using conda install scikit-learn=0.23.1 or pip install scikit-learn==0.23.1, you will get tons of compatibility checks and installation might not be quick. Therefore the easiest way to install scikit-learn version 0.23.1 in Anaconda is to create a new virtual environment with minimum packages so that there are less or no conflict issues. Then, in the new virtual environment install scikit-learn version 0.23.1 followed by version 0.7.0 of imbalanced-learn.

conda create -n test python=3.7.6
conda activate test
pip install scikit-learn==0.23.1
pip install imbalanced-learn==0.7.0

Finally, you need to reinstall your IDE in the new virtual environment in order to use these packages.

However, once scikit-learn version 0.23.1 becomes available in Conda and there are no compatibility issues, you can install it in the base environment directly.

Syncopation answered 30/7, 2020 at 4:38 Comment(0)
S
6

Step 1- Open your jupyter notebook

Step 2 - type pip install --upgrade scikit-learn

Step 3 - Restart the kernel

Follow all the steps as it is and it's done!!(upgraded)

Saturate answered 28/8, 2020 at 13:18 Comment(1)
Yes rather than creating a new environment update scikit learn and restart the kernelMarimaria
R
3

Welcome to SO! For your next question like this, you'll probably want to include the versions of python, sklearn, and imblearn you are using.

I ran into this same problem myself and the developers have noticed it: https://github.com/scikit-learn-contrib/imbalanced-learn/issues/727

Might want to follow this page to see if a solution is posted in the next few days. It seems to be about the sklearn library not being cleaned up properly after installing imblearn.

UPDATE
This can be fixed by updating your sklearn to Version 0.23 or higher. This should be possible for you through either:
pip update scikit-learn
OR
conda update scikit-learn

Reinaldoreinaldos answered 18/6, 2020 at 16:13 Comment(2)
This is not solving the issue.Barbitone
conda update scikit-learn does not work because the most recent version of sklearn is 0.22.1 on conda mainEvidentiary
M
1

although updating sklearn did not work for me as well, however setting up a new environment did , as proposed in one of the solutions provided in the link https://github.com/scikit-learn-contrib/imbalanced-learn/issues/727 mentioned in the answer.

Mancini answered 25/6, 2020 at 23:55 Comment(1)
please write the steps here for quick ref .. also it would get you some more upvotes ..Cenobite
O
1

Up/Downgrading scikit-learn not working?

My OS: Ubuntu MATE 18.04 x64

Had this same issue and tried other solutions to no avail.

I was originally using python 3.7.7 and got it working by using python 3.6.8 instead.

Anaconda

conda create -n myenv python=3.6.8
conda activate myenv
pip install scikit-learn
pip install imblearn

VirtualEnv - you will need python 3.6.8 already installed on your ystem

virtualenv --python=python3.6 myenv
source myenv/bin/activate
pip install scikit-learn
pip install imblearn

verify versions

import sklearn
sklearn.__version__
>>> '0.23.1'
import imblearn
imblearn.__version__
>>> '0.7.0'

...
# Now works
X_res, Y_res = sm.fit_resample(X_train, Y_train)
Openwork answered 30/6, 2020 at 19:29 Comment(0)
B
1

Error recieved was: AttributeError: 'SMOTE' object has no attribute '_validate_data'

Root Cause: Requires scikit-learn 0.23, but in conda - python 3.7 we only have scikit-learn 0.22

Solution: Create Virtual Enviornment with python3.6.8 and install scikit-learn 0.23 as below

  1. Create Virtual Env for python 3.6.8

    PS C:\Users\harish\Documents> conda create -n myenv python=3.6.8

  2. Activate the enviornment

    PS C:\Users\harish\Documents> conda activate myenv

  3. Install scikit-learn and imblearn in the virtual enviornment

    PS C:\Users\harish\Documents> pip install scikit-learn PS C:\Users\harish\Documents> pip install imblearn --user NOTE: this updates scikit-learn .... Collecting scikit-learn>=0.23 PS C:\Users\harish\Documents> conda list NOTE: it should be 0.23 ... scikit-learn 0.23.2 pypi_0 pypi

  4. Activate the kernel

    PS C:\Users\harish\Documents> python -m ipykernel install --user --name=myenv Installed kernelspec myenv in C:\Users\harish\AppData\Roaming\jupyter\kernels\myenv PS C:\Users\harish\Documents> cd C:\Users\harish\AppData\Roaming\jupyter\kernels\myenv

PS C:\Users\harish\AppData\Roaming\jupyter\kernels\myenv> ls

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        8/23/2020   6:41 PM            185 kernel.json
-a----        1/28/2020   2:18 AM           1084 logo-32x32.png
-a----        1/28/2020   2:18 AM           2180 logo-64x64.png

PS C:\Users\harish\AppData\Roaming\jupyter\kernels\myenv> cat kernel.json

{
 "argv": [
  "C:\\Users\\harish\\Anaconda3\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "myenv",
 "language": "python"
}
  1. In the Notebook you are working, Kernel>>Cange Kernel >> New Env
Bette answered 23/8, 2020 at 11:2 Comment(0)
R
1

Upgrading both sklearn and imblearn worked for me

!pip install --upgrade scikit-learn
!pip install --upgrade imblearn
Rocray answered 10/7, 2021 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.