gpt4 book ai didi

python-3.x - 调试 Numpy VisibleDeprecationWarning(来自不规则嵌套序列的 ndarray)

转载 作者:行者123 更新时间:2023-12-04 11:04:04 29 4
gpt4 key购买 nike

从 NumPy 19.0 版开始,必须指定 dtype=object从“参差不齐”的序列创建数组时。我面临着来自我自己的代码和使用线程的 Pandas 的大量数组调用,逐行调试使我无处可去。
我想弄清楚哪个调用导致了我自己的代码中的 VisibleDeprecationWarning 或来自 Pandas 的调用。我怎么能调试这个?我一直在查看源代码,但看不到在 Python 中调用此警告(仅在 numpy.core._multiarray_umath.cp38-win_amd64.pyd 中)。

最佳答案

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

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文档了解更多详情。

关于python-3.x - 调试 Numpy VisibleDeprecationWarning(来自不规则嵌套序列的 ndarray),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63097829/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com