导入imblearn 报错 解决了2天 还没成功

显示导入错误 重新装了多遍环境 安装卸载conda 都不行 求大神指导 多谢

错误

导入imblearn 报错

代码

# Undersample imbalanced dataset with NearMiss-3
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import NearMiss
from matplotlib import pyplot
from numpy import where
# define dataset
X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0,
    n_clusters_per_class=1, weights=[0.99], flip_y=0, random_state=1)
# summarize class distribution
counter = Counter(y)
print(counter)
# define the undersampling method
undersample = NearMiss(version=3, n_neighbors_ver3=3)
# transform the dataset
X, y = undersample.fit_resample(X, y)
# summarize the new class distribution
counter = Counter(y)
print(counter)
# scatter plot of examples by class label
for label, _ in counter.items():
    row_ix = where(y == label)[0]
    pyplot.scatter(X[row_ix, 0], X[row_ix, 1], label=str(label))
pyplot.legend()
pyplot.show()
Jason990420
最佳答案

I got nothing wrong after install imblearn which is not installed under conda.

d:\>pip install imblearn
Collecting imblearn
  Downloading imblearn-0.0-py2.py3-none-any.whl (1.9 kB)
Collecting imbalanced-learn (from imblearn)
  Downloading imbalanced_learn-0.10.1-py3-none-any.whl (226 kB)
     ------------------------------------ 226.0/226.0 kB 811.1 kB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.3 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.23.1)
Requirement already satisfied: scipy>=1.3.2 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.9.0)
Requirement already satisfied: scikit-learn>=1.0.2 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.1.2)
Collecting joblib>=1.1.1 (from imbalanced-learn->imblearn)
  Downloading joblib-1.3.1-py3-none-any.whl (301 kB)
     -------------------------------------- 302.0/302.0 kB 1.2 MB/s eta 0:00:00
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (3.1.0)
Installing collected packages: joblib, imbalanced-learn, imblearn
  Attempting uninstall: joblib
    Found existing installation: joblib 1.1.0
    Uninstalling joblib-1.1.0:
      Successfully uninstalled joblib-1.1.0
Successfully installed imbalanced-learn-0.10.1 imblearn-0.0 joblib-1.3.1
Counter({0: 9900, 1: 100})
Counter({0: 100, 1: 100})

file

Maybe you can check if the version of sklearn.version is < “1.2”. The version of my isnstalled sklearn is ‘1.1.2’.

>>> import sklearn
>>> sklearn.__version__
'1.1.2'

or

d:\>pip show scikit-learn
Name: scikit-learn
Version: 1.1.2
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author:
Author-email:
License: new BSD
Location: c:\software\python\lib\site-packages
Requires: joblib, numpy, scipy, threadpoolctl
Required-by: imbalanced-learn

After I upgrade scikit-learn

d:\>pip install scikit-learn --upgrade
Requirement already satisfied: scikit-learn in c:\software\python\lib\site-packages (1.3.0)
Requirement already satisfied: numpy>=1.17.3 in c:\software\python\lib\site-packages (from scikit-learn) (1.23.1)
Requirement already satisfied: scipy>=1.5.0 in c:\software\python\lib\site-packages (from scikit-learn) (1.9.0)
Requirement already satisfied: joblib>=1.1.1 in c:\software\python\lib\site-packages (from scikit-learn) (1.3.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\software\python\lib\site-packages (from scikit-learn) (3.1.0)

then I got same exception as yours

Traceback (most recent call last):
  File "D:\test6.py", line 4, in <module>
    from imblearn.under_sampling import NearMiss
  File "C:\Software\Python\lib\site-packages\imblearn\__init__.py", line 52, in <module>
    from . import (
  File "C:\Software\Python\lib\site-packages\imblearn\combine\__init__.py", line 5, in <module>
    from ._smote_enn import SMOTEENN
  File "C:\Software\Python\lib\site-packages\imblearn\combine\_smote_enn.py", line 12, in <module>
    from ..base import BaseSampler
  File "C:\Software\Python\lib\site-packages\imblearn\base.py", line 21, in <module>
    from .utils._param_validation import validate_parameter_constraints
  File "C:\Software\Python\lib\site-packages\imblearn\utils\_param_validation.py", line 908, in <module>
    from sklearn.utils._param_validation import (
ImportError: cannot import name '_MissingValues' from 'sklearn.utils._param_validation' (C:\Software\Python\lib\site-packages\sklearn\utils\_param_validation.py)
9个月前 评论
lemamba (楼主) 9个月前
讨论数量: 2
Jason990420

I got nothing wrong after install imblearn which is not installed under conda.

d:\>pip install imblearn
Collecting imblearn
  Downloading imblearn-0.0-py2.py3-none-any.whl (1.9 kB)
Collecting imbalanced-learn (from imblearn)
  Downloading imbalanced_learn-0.10.1-py3-none-any.whl (226 kB)
     ------------------------------------ 226.0/226.0 kB 811.1 kB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17.3 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.23.1)
Requirement already satisfied: scipy>=1.3.2 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.9.0)
Requirement already satisfied: scikit-learn>=1.0.2 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (1.1.2)
Collecting joblib>=1.1.1 (from imbalanced-learn->imblearn)
  Downloading joblib-1.3.1-py3-none-any.whl (301 kB)
     -------------------------------------- 302.0/302.0 kB 1.2 MB/s eta 0:00:00
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\software\python\lib\site-packages (from imbalanced-learn->imblearn) (3.1.0)
Installing collected packages: joblib, imbalanced-learn, imblearn
  Attempting uninstall: joblib
    Found existing installation: joblib 1.1.0
    Uninstalling joblib-1.1.0:
      Successfully uninstalled joblib-1.1.0
Successfully installed imbalanced-learn-0.10.1 imblearn-0.0 joblib-1.3.1
Counter({0: 9900, 1: 100})
Counter({0: 100, 1: 100})

file

Maybe you can check if the version of sklearn.version is < “1.2”. The version of my isnstalled sklearn is ‘1.1.2’.

>>> import sklearn
>>> sklearn.__version__
'1.1.2'

or

d:\>pip show scikit-learn
Name: scikit-learn
Version: 1.1.2
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author:
Author-email:
License: new BSD
Location: c:\software\python\lib\site-packages
Requires: joblib, numpy, scipy, threadpoolctl
Required-by: imbalanced-learn

After I upgrade scikit-learn

d:\>pip install scikit-learn --upgrade
Requirement already satisfied: scikit-learn in c:\software\python\lib\site-packages (1.3.0)
Requirement already satisfied: numpy>=1.17.3 in c:\software\python\lib\site-packages (from scikit-learn) (1.23.1)
Requirement already satisfied: scipy>=1.5.0 in c:\software\python\lib\site-packages (from scikit-learn) (1.9.0)
Requirement already satisfied: joblib>=1.1.1 in c:\software\python\lib\site-packages (from scikit-learn) (1.3.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\software\python\lib\site-packages (from scikit-learn) (3.1.0)

then I got same exception as yours

Traceback (most recent call last):
  File "D:\test6.py", line 4, in <module>
    from imblearn.under_sampling import NearMiss
  File "C:\Software\Python\lib\site-packages\imblearn\__init__.py", line 52, in <module>
    from . import (
  File "C:\Software\Python\lib\site-packages\imblearn\combine\__init__.py", line 5, in <module>
    from ._smote_enn import SMOTEENN
  File "C:\Software\Python\lib\site-packages\imblearn\combine\_smote_enn.py", line 12, in <module>
    from ..base import BaseSampler
  File "C:\Software\Python\lib\site-packages\imblearn\base.py", line 21, in <module>
    from .utils._param_validation import validate_parameter_constraints
  File "C:\Software\Python\lib\site-packages\imblearn\utils\_param_validation.py", line 908, in <module>
    from sklearn.utils._param_validation import (
ImportError: cannot import name '_MissingValues' from 'sklearn.utils._param_validation' (C:\Software\Python\lib\site-packages\sklearn\utils\_param_validation.py)
9个月前 评论
lemamba (楼主) 9个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!