numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

目录

警告

解决

警告

这是我写的读取npz文件的代码,

datas = np.load("bsm.npz", allow_pickle=True)
print(datas.files)
for key, arr in datas.items():
    print(key, ": ", arr)

执行代码之后,可以输出预期的结果,但也得到了警告,如下:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.

  val = np.asanyarray(val)

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

解决

其实这不是错误,只是新版numpy的一个警告,可以忽略。当然总是显示影响美观,可以直接关闭该警告。
这是因为新的 numpy版本,将 创建不同长度或形状的列表或元组或ndarray的列表或ndarray元组的功能 会被弃用,虽然能够运行,但是总提示Warning还是很不舒服,况且之后也不能这样用了。

尝试网传的解决方法:

  • 使用更早的numpy版本
  • 添加dtype=object
  • 添加一行忽视报错代码

(1)尝试一下网上有种说法是添加”dtype=object”,

3.加载数据
datas = np.load("bsm.npz", allow_pickle=True, dtype=object)
print(datas.files)
for key, arr in datas.items():
    print(key, ": ", arr)

但是还是报错,甚至都不会输出结果了,

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

(2)我们再尝试网上还有一种说法是: numpy的版本过高,需要降低numpy的版本

查看了自己的numpy的版本,我现在的numpy版本是1.23.0,

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

好吧,那我们来重新安装一下低版本的numpy试试,

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

等待安装完成的过程中,报错了,

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

好吧。

(3)在网上还看见一种说法:

np.warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning)
或
np.warnings.filterwarnings('error', category=np.VisibleDeprecationWarning)

我们来试试,

numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

(4)最后还是决定忽视它……

Original: https://blog.csdn.net/qq_45956730/article/details/125544357
Author: 水w
Title: numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/730178/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球