You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NumPy与Scikit-learn兼容报错:Shapley值模型实现求助

问题

在实现Shapley值模型时,导入相关库触发AttributeError,执行的代码如下:

import random
import warnings
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

warnings.filterwarnings("ignore")

触发的报错信息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-6c2b9bf4cfd2> in <module>
      2 import warnings
      3 import numpy as np
----> 4 from sklearn.datasets import load_breast_cancer
      5 from sklearn.model_selection import train_test_split
      6 from sklearn.preprocessing import StandardScaler

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/datasets/__init__.py in <module>
     20 from ._lfw import fetch_lfw_pairs
     21 from ._lfw import fetch_lfw_people
---> 22 from ._twenty_newsgroups import fetch_20newsgroups
     23 from ._twenty_newsgroups import fetch_20newsgroups_vectorized
     24 from ._openml import fetch_openml

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/datasets/_twenty_newsgroups.py in <module>
     43 from ._base import _fetch_remote
     44 from ._base import RemoteFileMetadata
---> 45 from ..feature_extraction.text import CountVectorizer
     46 from .. import preprocessing
     47 from ..utils import check_random_state, Bunch

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/feature_extraction/__init__.py in <module>
      7 from ._dict_vectorizer import DictVectorizer
      8 from ._hash import FeatureHasher
---> 9 from .image import img_to_graph, grid_to_graph
     10 from . import text
     11 

~/opt/anaconda3/lib/python3.8/site-packages/sklearn/feature_extraction/image.py in <module>
    170 @_deprecate_positional_args
    171 def grid_to_graph(n_x, n_y, n_z=1, *, mask=None, return_as=sparse.coo_matrix,
---> 172                   dtype=np.int):
    173     """Graph of the pixel-to-pixel connections
    174 

~/opt/anaconda3/lib/python3.8/site-packages/numpy/__init__.py in __getattr__(attr)
    303 
    304         if attr in __former_attrs__:
---> 305             raise AttributeError(__former_attrs__[attr])
    306 
    307         # Importing Tester requires importing all of UnitTest which is not a

AttributeError: module 'numpy' has no attribute 'int'.
`np.int` was a deprecated alias for the builtin `int`. To avoid this error in existing code, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

已重新安装NumPy,但问题仍未解决,需解决该报错并了解Shapley值模型同类问题的处理经验。

解决方案

这个错误的核心是旧版scikit-learn与新版NumPy(1.20+)不兼容——旧版sklearn代码仍在使用np.int这个已被NumPy废弃的别名,重新安装NumPy无法解决版本匹配问题。

具体解决方法

  • 升级scikit-learn到兼容版本:在终端执行以下命令,新版sklearn已将np.int替换为标准int或指定精度的类型,升级后即可解决冲突:
    pip install --upgrade scikit-learn
    
  • 降级NumPy到兼容旧版sklearn的版本:若暂时不想升级sklearn,可安装NumPy 1.19.x版本(该版本仍保留np.int别名),执行命令:
    pip install numpy==1.19.5
    

Shapley值模型同类问题经验

  • 若后续使用shap库实现Shapley值,需注意shap与NumPy、sklearn的版本匹配,建议使用shap 0.40+版本,其对新版NumPy和sklearn支持更完善。
  • 遇到类似的属性错误(如np.float、np.bool等废弃别名),本质均为库版本不兼容,优先升级所有依赖库到最新兼容版本是最稳妥的解决方式。

内容的提问来源于stack exchange,提问作者ulissebu123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 12:07:53