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
6条答案
按热度按时间n3h0vuf21#
使用创建不规则数组的函数:
我得到了警告,也得到了预期的结果。
我可以控制警告。
例如,要关闭,请执行以下操作:
或引发错误:
错误提供了一个追溯,告诉我警告是在哪里发出的。
可能有一些方法可以改进警告过滤器,使其只捕获这一个警告,而不捕获同一类别的其他警告。
阅读
np.warnings.filterwarnings
文档了解更多详情。6ioyuze22#
参考上面的示例将清除警告。您必须指定
dtype=object
。ct3nt3jp3#
此警告是由NumPy版本1.19或更高版本的已弃用API引起的,您可以继续使用它,只需抑制警告:
ndasle7k4#
你可以在创建numpy数组时添加dtype=object:
或者,如果你将一个列表或一个名为'a'的元组更改为一个numpy数组代码:
这有助于您避免警告。
jckbn6z75#
我在堆叠包含WAV文件中的音频数据的列表时遇到了
np.VisibleDeprecationWarning
。出现这个问题是因为音频文件的长度不同。因此,我需要堆叠到一个numpy数组中的列表也有不同的长度。忽略或抑制此警告并没有给予所需的堆叠np数组,因此,我使用
pydub.AudioSegment
使所有音频文件具有相同的长度,如本答案中所述。这解决了警告。
zazmityj6#
每个列表中元素的长度应该相同。由于数组的长度不同,会提示此警告。arr3 = np.array([[5,6],[8,9,3]],dtype = object)将dtype指定为对象,会将每个元素使用的字节数增加到8字节。但如果不指定dtype,则分配给数组中每个元素的内存大小为4字节。