简体   繁体   English

AttributeError: 'SMOTE' object 没有属性 '_validate_data'

[英]AttributeError: 'SMOTE' object has no attribute '_validate_data'

I'm resampling my data (multiclass) by using SMOTE.我正在使用 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?任何人都可以帮忙吗?

Short answer简短的回答

You need to upgrade scikit-learn to version 0.23.1.您需要将scikit-learn升级到版本 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. imbalanced-learn的最新版本 0.7.0 似乎对scikit-learn v0.23.1 存在未记录的依赖关系。 It would give you AttributeError: 'SMOTE' object has no attribute '_validate_data' if your scikit-learn is 0.22 or below.如果您的scikit-learn为 0.22 或更低,它会给您AttributeError: 'SMOTE' object has no attribute '_validate_data'

If you are using Anaconda , installing scikit-learn version 0.23.1 might be tricky.如果您使用的是Anaconda ,安装scikit-learn版本 0.23.1 可能会很棘手。 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. conda update scikit-learn可能不会更新scikit-learn版本 0.23 或更高版本,因为 Conda 目前的最新scikit-learn版本是 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.如果您尝试使用conda install scikit-learn=0.23.1pip install scikit-learn==0.23.1 ,您将获得大量的兼容性检查并且安装可能不会很快。 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.因此,在 Anaconda 中安装scikit-learn版本 0.23.1 的最简单方法是使用最少的包创建一个新的虚拟环境,从而减少或没有冲突问题。 Then, in the new virtual environment install scikit-learn version 0.23.1 followed by version 0.7.0 of imbalanced-learn .然后,在新的虚拟环境中安装scikit-learn版本 0.23.1,然后安装 0.7.0 版本的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.最后,您需要在新的虚拟环境中重新安装 IDE 才能使用这些软件包。

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.但是,一旦scikit-learn版本 0.23.1 在 Conda 中可用并且没有兼容性问题,您可以直接在基础环境中安装它。

Step 1 - Open your jupyter notebook第 1 步- 打开你的 jupyter 笔记本

Step 2 - type pip install --upgrade scikit-learn第 2 步- 输入 pip 安装 -- 升级 scikit-learn

Step 3 - Restart the kernel第 3 步- 重新启动 kernel

Follow all the steps as it is and it's done!!(upgraded)照原样按照所有步骤完成!(升级)

Welcome to SO, For your next question like this, you'll probably want to include the versions of python, sklearn.欢迎来到 SO,对于这样的下一个问题,您可能希望包括 python 的版本,sklearn。 and imblearn you are using.和 imblearn 你正在使用。

I ran into this same problem myself and the developers have noticed it: https://github.com/scikit-learn-contrib/imbalanced-learn/issues/727我自己也遇到了同样的问题,开发人员也注意到了: 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.这似乎是关于安装 imblearn 后没有正确清理 sklearn 库。

UPDATE更新
This can be fixed by updating your sklearn to Version 0.23 or higher.这可以通过将您的 sklearn 更新到 0.23 或更高版本来解决。 This should be possible for you through either:这应该可以通过以下任一方式为您实现:
pip update scikit-learn
OR或者
conda update scikit-learn

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.尽管更新 sklearn 对我也不起作用,但是设置一个新环境确实如此,正如链接https://github.com/scikit-learn-contrib/imbalanced-learn/issues/中提供的解决方案之一中所建议的那样727在答案中提到。

Up/Downgrading scikit-learn not working?升级/降级 scikit-learn 不起作用?

My OS: Ubuntu MATE 18.04 x64我的操作系统: 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.我最初使用python 3.7.7并通过使用python 3.6.8来使其工作。

Anaconda 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 3.6.8

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)

Error recieved was : AttributeError: 'SMOTE' object has no attribute '_validate_data'收到的错误是: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根本原因:需要 scikit-learn 0.23,但在 conda - python 3.7 我们只有 scikit-learn 0.22

Solution : Create Virtual Enviornment with python3.6.8 and install scikit-learn 0.23 as below解决方案:使用 python3.6.8 创建虚拟环境并安装 scikit-learn 0.23 如下

  1. Create Virtual Env for python 3.6.8为 python 3.6.8 创建虚拟环境

    PS C:\Users\harish\Documents> conda create -n myenv 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 PS C:\Users\harish\Documents> conda activate myenv

  3. Install scikit-learn and imblearn in the virtual enviornment在虚拟环境中安装 scikit-learn 和 imblearn

    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 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 注意:它应该是 0.23... scikit-learn 0.23.2 pypi_0 pypi

  4. Activate the kernel激活 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\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 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 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在您正在工作的笔记本中,内核>>Cange Kernel >> New Env

Upgrading both sklearn and imblearn worked for me升级 sklearn 和 imblearn 对我有用

!pip install --upgrade scikit-learn
!pip install --upgrade imblearn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM