无法遍历数据集遇AttributeError,咨询适配的numpy与datasets版本
问题:加载
jacktol/atc-dataset时出现NumPy属性错误 环境信息
- 系统:Windows
- Python版本:3.10.0
- 已安装依赖:
datasets==2.21.0,numpy==1.24.4
执行代码
from datasets import load_dataset dataset = load_dataset("jacktol/atc-dataset", download_mode='force_redownload') dataset['train'][0]
错误信息
AttributeError: module 'numpy' has no attribute 'complex'. `np.complex` was a deprecated alias for the builtin `complex`. To avoid this error in existing code, use `complex` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.complex128` here. 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
解决方案
有两种可行方案解决版本兼容问题:
方案1:降级NumPy版本
np.complex在NumPy 1.20版本开始被弃用,后续版本(如你使用的1.24.4)直接移除了这个别名,而datasets==2.21.0的代码仍依赖该别名。可以将NumPy降级到1.19.x系列(推荐1.19.5),该版本保留了np.complex,能与当前datasets版本兼容。
执行命令:
pip install numpy==1.19.5
方案2:升级datasets版本
datasets的后续稳定版本已修复对NumPy新版本的兼容问题,不再使用已弃用的np.complex。可以升级datasets到较新的稳定版本(比如2.14.0及以上或最新稳定版),同时保留numpy==1.24.4。
执行命令:
pip install --upgrade datasets
内容的提问来源于stack exchange,提问作者user3668129
相关产品推荐
相关产品推荐

