首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ImportError:无法从'sklearn.utils‘导入名称'safe_indexing’

ImportError:无法从'sklearn.utils‘导入名称'safe_indexing’
EN

Stack Overflow用户
提问于 2021-04-04 20:58:13
回答 2查看 1.2K关注 0票数 0

每当我尝试在Jupyter notebook上运行以下代码行-从imblearn.under_sampling导入NearMiss进行欠采样(或过采样)不平衡数据时,我得到这个错误:

“”“

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-21-c701341dce49> in <module>
----> 1 from imblearn.under_sampling import NearMiss

~\anaconda3\lib\site-packages\imblearn\under_sampling\__init__.py in <module>
      4 """
      5 
----> 6 from ._prototype_generation import ClusterCentroids
      7 
      8 from ._prototype_selection import RandomUnderSampler

~\anaconda3\lib\site-packages\imblearn\under_sampling\_prototype_generation\__init__.py in <module>
      4 """
      5 
----> 6 from ._cluster_centroids import ClusterCentroids
      7 
      8 __all__ = ['ClusterCentroids']

~\anaconda3\lib\site-packages\imblearn\under_sampling\_prototype_generation\_cluster_centroids.py in <module>
     15 from sklearn.cluster import KMeans
     16 from sklearn.neighbors import NearestNeighbors
---> 17 from sklearn.utils import safe_indexing
     18 
     19 from ..base import BaseUnderSampler

ImportError: cannot import name 'safe_indexing' from 'sklearn.utils' (C:\Users\anaconda3\lib\site-packages\sklearn\utils\__init__.py)

“”“

EN

回答 2

Stack Overflow用户

发布于 2021-04-04 21:30:36

对于imblearn.under_sampling,您是否尝试过重新安装该软件包?:

代码语言:javascript
运行
复制
pip install imbalanced-learn

conda:

代码语言:javascript
运行
复制
conda install -c conda-forge imbalanced-learn

在jupyter笔记本中:

代码语言:javascript
运行
复制
import sys
!{sys.executable} -m pip install <package_name>

如果你有scikitlearn>=0.24 (据我所知,现在有一个对imblearn的依赖,就像scikit-learn (>=0.23) https://imbalanced-learn.org/stable/install.html),你可能想试试:

代码语言:javascript
运行
复制
try:
    from sklearn.utils import safe_indexing
except ImportError:
    from sklearn.utils import _safe_indexing
票数 1
EN

Stack Overflow用户

发布于 2021-05-26 21:17:32

编辑..\Anaconda3\Lib\site-packages\sklearn\utils\ init.py

复制定义_safe_indexing ...直到下一次定义。并将重命名的代码粘贴到def safe_indexing..

保持以前的_safe_indexing不变。

它解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66941530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档