【问题标题】:How to solve issue of PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices如何解决 PendingDeprecationWarning 的问题:矩阵子类不是表示矩阵的推荐方式
【发布时间】:2019-10-02 10:45:44
【问题描述】:

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

文件“/home/hammouc/.local/lib/python2.7/site-packages/scipy/sparse/base.py”,第 849 行,以致密 return np.asmatrix(self.toarray(order=order, out=out))

文件“/home/hammouc/.local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py”,第 71 行,在 asmatrix 中 返回矩阵(数据,dtype=dtype,copy=False)

中的文件“/home/hammouc/.local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py”,第 123 行 PendingDeprecationWarning, stacklevel=2)

PendingDeprecationWarning:矩阵子类不是表示矩阵或处理线性代数的推荐方法(请参阅https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html)。请调整您的代码以使用常规 ndarray。

【问题讨论】:

  • 看起来像一个 scipy.sparse 矩阵 todense 正在触发这个。理想的情况是用toarray 方法调用替换它。
  • 非常感谢这有帮助!
  • 您使用的是哪个numpyscipy 版本?我知道我们不鼓励使用 np.matrix,但我不知道有任何实际的弃用趋势。
  • 我在 np.matrixlib.defmatrix.py 文件 (1.16.3) 中发现了该警告消息。您的警告级别必须与我的不同(默认)。

标签: python-2.7 numpy matrix scipy deprecated


【解决方案1】:

这是一个未决的弃用警告。

含义:在下一个版本中,此功能将被取消,之后您将崩溃。

这是一个警告 - 目前你的代码可以工作,下次更新:谁知道?

如果您不想接触使用已弃用部分的代码,您有几个选择: - 你可以停止升级(冻结你的 Scipy 版本 - 永远不要再升级) - 您可以更改代码以使用ndarray

无论您采取什么方式,都应该在阅读 https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html) 并进行风险分析之后完成 - 如果您使用代码进行线性代数以降落下一个 Rover III Saturn 上,您可能应该将其更改为更可靠的东西。

如果你为你的 3 株西红柿解决了“浇水”问题 - 最糟糕的事情是你买不到西红柿,你必须在商店里买它们......

【讨论】:

    【解决方案2】:

    使用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 设置忽略了这些警告,所以我以前没有见过这个。我得看看配置文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2011-01-21
      • 2015-05-17
      相关资源
      最近更新 更多