gpt4 book ai didi

python-2.7 - 如何解决 PendingDeprecationWarning : the matrix subclass is not the recommended way to represent matrices 的问题

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

我正在使用 python 代码作为我不想触摸的黑匣子。该代码在 Ubuntu 12.04 下使用 Python 运行良好,但在将系统升级到 Ubuntu 16 后,我收到以下警告,该警告会中断代码运行。知道如何在不更改代码的情况下解决此问题吗?非常感谢。

File "/home/hammouc/.local/lib/python2.7/site-packages/scipy/sparse/base.py", line 849, in todense return np.asmatrix(self.toarray(order=order, out=out))

File "/home/hammouc/.local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 71, in asmatrix return matrix(data, dtype=dtype, copy=False)

File "/home/hammouc/.local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 123, in new PendingDeprecationWarning, stacklevel=2)

PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.

最佳答案

import warnings模块,可以控制警告的显示。

M作为稀疏矩阵:

In [26]: warnings.filterwarnings('ignore', category=PendingDeprecationWarning)  
In [27]: M.todense()
Out[27]:
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
In [28]: warnings.filterwarnings('default', category=PendingDeprecationWarning)
In [29]: M.todense()
/usr/local/lib/python3.6/dist-packages/numpy/matrixlib/defmatrix.py:71: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.
return matrix(data, dtype=dtype, copy=False)
Out[29]:
matrix([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

制作 ndarray而不是 np.matrix :
In [30]: M.toarray()                                                            
Out[30]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
In [31]: M.A
Out[31]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

显然我的默认 ipython setup 会忽略这些警告,所以我以前没见过。我得看看配置文件。

关于python-2.7 - 如何解决 PendingDeprecationWarning : the matrix subclass is not the recommended way to represent matrices 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56146811/

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