调试Numpy VisibleDeprecationWarning(破烂的嵌套序列中的ndarray) [英] Debugging Numpy VisibleDeprecationWarning (ndarray from ragged nested sequences)
问题描述
自NumPy 19.0版以来,从参差不齐"的数组创建数组时,必须指定dtype = object.序列.我面对着来自我自己的代码的大量数组调用以及使用线程的Pandas,逐行调试使我无处可去.我想找出哪个调用导致了我自己的代码中的VisibleDeprecationWarning或来自Pandas的调用.我将如何调试呢?我一直在浏览源代码,但看不到此警告在Python中被调用(仅在numpy.core._multiarray_umath.cp38-win_amd64.pyd中).
Since NumPy version 19.0, one must specify dtype=object when creating an array from "ragged" sequences. I'm faced with a large number of array calls from my own code and Pandas using threading, line-by-line debugging led me nowhere. I'd like to figure out which call resulted in VisibleDeprecationWarning in my own code or a call from Pandas. How would I be able to debug this? I've been looking through the source and I cannot see this warning getting called in Python (only in numpy.core._multiarray_umath.cp38-win_amd64.pyd).
推荐答案
具有创建粗糙数组的函数:
With a function that creates a ragged array:
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)
我得到了警告,但也得到了预期的结果.
I get the warning, but also the expected result.
我可以控制警告.
例如,如果关闭则将其关闭:
For example to turn if off:
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
该错误提供了追溯信息,告诉我在何处发出警告.
The error gives a traceback telling me where the warning was raised.
可能有一些方法可以改进警告过滤器,使其仅捕获此过滤器,而不捕获相同类别的其他过滤器.我没有使用太多这种机制.
There may be ways of refining the warning filter to catch just this one, and not others of the same category. I haven't used this mechanism much.
有关更多详细信息,请阅读 np.warnings.filterwarnings
文档.
Read np.warnings.filterwarnings
docs for more details.
这篇关于调试Numpy VisibleDeprecationWarning(破烂的嵌套序列中的ndarray)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!