调试 Numpy VisibleDeprecationWarning(来自不规则嵌套序列的 ndarray)

mis*_*oop 28 debugging numpy python-3.x

从 NumPy 19.0 版开始,dtype=object从“参差不齐”的序列创建数组时必须指定。我面临着来自我自己的代码和使用线程的 Pandas 的大量数组调用,逐行调试使我无处可去。

我想弄清楚哪个调用导致了我自己的代码中的VisibleDeprecationWarning或来自 Pandas 的调用。我怎么能调试这个?我一直在查看源代码,但看不到在 Python 中调用此警告(仅在 numpy.core._multiarray_umath.cp38-win_amd64.pyd 中)。

hpa*_*ulj 42

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

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)
Run Code Online (Sandbox Code Playgroud)

我收到警告,但也收到了预期的结果。

我可以控制警告。

例如关闭如果关闭:

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

或者引发错误:

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
Run Code Online (Sandbox Code Playgroud)

该错误提供了一个回溯,告诉我在哪里引发了警告。

可能有一些方法可以改进警告过滤器以仅捕获这个,而不是同一类别的其他过滤器。我没怎么用过这个机制。

阅读np.warnings.filterwarnings文档了解更多详情。


小智 30

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

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

  • 但这不是一个参差不齐的数组 (2认同)

小智 7

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

import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning) 
Run Code Online (Sandbox Code Playgroud)


小智 7

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

numpy.array([[1,2,3],[4,5,6]], dtype=object)
Run Code Online (Sandbox Code Playgroud)

或者,如果将名为“a”的列表或元组更改为 numpy 数组,代码如下:

numpy.asarray(a,dtype=object)
Run Code Online (Sandbox Code Playgroud)

这可以帮助您避免警告。