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

标签 python-2.7 numpy matrix scipy deprecated

我正在使用 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/

相关文章:

python - 如何在Python中定义一个具有不同列值类型的矩阵或二维数组并有效地检索列

python - 如何将 dict 的值写入 openpyxl 中的空(新)列?

Python 2.7 : LookupError: unknown encoding: cp65001

python - __slots__ 和 pickle 的奇怪行为

javascript - for 循环生成的二维 JavaScript 数组正在被最后一个循环结果覆盖

python 圣人 : How do I computer a nullspace (kernel) for a stoichiometric matrix?

google-app-engine - 为什么我在 setUp() 中创建的实体无法通过 nosetests 访问?

python - UPDATEIFCOPY 标志是否为真?

python - pandas:如何将 bin 值追加回原始数据框

python - 如何在文本文件中从打印 [1, 2, 3] 到打印 1, 2, 3 Python