调试Numpy VisibleDeprecationWarning(ndarray from ragged nested sequences)

57hvy0tb  于 2023-04-06  发布在  其他
关注(0)|答案(6)|浏览(233)

从NumPy 19.0版本开始,当从“不规则”序列创建数组时,必须指定dtype=object。我面临着大量来自我自己代码的数组调用和使用线程的Pandas,逐行调试让我一无所获。
我想弄清楚是哪个调用在我自己的代码中导致了 VisibleDeprecationWarning,还是来自Pandas的调用。我如何能够调试这个?我已经查看了源代码,我没有看到这个警告在Python中被调用(只有在numpy.core._multiarray_umath.cp38-win_amd64.pyd中)。

n3h0vuf2

n3h0vuf21#

使用创建不规则数组的函数:

In [60]: def foo(): 
    ...:     print('one') 
    ...:     x = np.array([[1],[1,2]]) 
    ...:     return x 
    ...:                                                                                             
In [61]: foo()                                                                                       
one
/usr/local/bin/ipython3:3: 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
  # -*- coding: utf-8 -*-
Out[61]: array([list([1]), list([1, 2])], dtype=object)

我得到了警告,也得到了预期的结果。
我可以控制警告。
例如,要关闭,请执行以下操作:

In [68]: np.warnings.filterwarnings('ignore', category=np.VisibleDeprecationWarning)                 
In [69]: foo()                                                                                       
one
Out[69]: array([list([1]), list([1, 2])], dtype=object)

或引发错误:

In [70]: np.warnings.filterwarnings('error', category=np.VisibleDeprecationWarning)                  
In [71]: foo()                                                                                       
one
---------------------------------------------------------------------------
VisibleDeprecationWarning                 Traceback (most recent call last)
<ipython-input-71-c19b6d9633cf> in <module>
----> 1 foo()

<ipython-input-60-6ad21d9e07b4> in foo()
      1 def foo():
      2     print('one')
----> 3     x = np.array([[1],[1,2]])
      4     return x
      5 

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

错误提供了一个追溯,告诉我警告是在哪里发出的。
可能有一些方法可以改进警告过滤器,使其只捕获这一个警告,而不捕获同一类别的其他警告。
阅读np.warnings.filterwarnings文档了解更多详情。

6ioyuze2

6ioyuze22#

b2 = np.array(
    [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18]],
    dtype=object,
)

参考上面的示例将清除警告。您必须指定dtype=object

ct3nt3jp

ct3nt3jp3#

此警告是由NumPy版本1.19或更高版本的已弃用API引起的,您可以继续使用它,只需抑制警告:

import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)
ndasle7k

ndasle7k4#

你可以在创建numpy数组时添加dtype=object:

numpy.array([[1,2,3],[4,5,6]], dtype=object)

或者,如果你将一个列表或一个名为'a'的元组更改为一个numpy数组代码:

numpy.asarray(a,dtype=object)

这有助于您避免警告。

jckbn6z7

jckbn6z75#

我在堆叠包含WAV文件中的音频数据的列表时遇到了np.VisibleDeprecationWarning。出现这个问题是因为音频文件的长度不同。因此,我需要堆叠到一个numpy数组中的列表也有不同的长度。
忽略或抑制此警告并没有给予所需的堆叠np数组,因此,我使用pydub.AudioSegment使所有音频文件具有相同的长度,如本答案中所述。
这解决了警告。

zazmityj

zazmityj6#

每个列表中元素的长度应该相同。由于数组的长度不同,会提示此警告。arr3 = np.array([[5,6],[8,9,3]],dtype = object)将dtype指定为对象,会将每个元素使用的字节数增加到8字节。但如果不指定dtype,则分配给数组中每个元素的内存大小为4字节。

相关问题