簡體   English   中英

ImportError:無法從“sklearn.utils”導入名稱“safe_indexing”

[英]ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'

每當我嘗試運行以下代碼行時:

from imblearn.under_sampling import NearMiss

對於 Jupyter 筆記本上的欠采樣(或過采樣)不平衡數據,我收到此錯誤:

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)

對於 imblearn.under_sampling,您是否嘗試重新安裝 package?:

pip install imbalanced-learn

康達:

conda install -c conda-forge imbalanced-learn

在 jupyter 筆記本中:

import sys
!{sys.executable} -m pip install <package_name>

If you have scikitlearn>=0.24 (as far as i see there is a dependency for imblearn now,as scikit-learn (>=0.23) https://imbalanced-learn.org/stable/install.html ) you may want to try:

try:
    from sklearn.utils import safe_indexing
except ImportError:
    from sklearn.utils import _safe_indexing

編輯..\Anaconda3\Lib\site-packages\sklearn\utils\ __init__.py

復制def _safe_indexing...直到下一個def並將代碼重命名為def safe_indexing...

保持以前_safe_indexing

它以這種方式解決了這個問題。

~\Anaconda3\Lib\site-packages\yellowbrick\classifier\threshold.py模塊中替換:

from sklearn.utils import indexable, safe_indexing

from sklearn.utils import indexable, _safe_indexing

之后,重新啟動 kernel。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM