How to solve issue of PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices How to solve issue of PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices numpy numpy

How to solve issue of PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices


With the import warnings module, it's possible to control the display of warnings.

With M as a sparse matrix:

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.]])

Producing a ndarray instead of a 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.]])

Apparently my default ipython setup ignores these warnings, so I haven't seen this before. I'll have to look at the config file.


It is a pending deprecation warning.

Meaning: in one of the next versions this functionality will be taken out and your will crash after that.

It is a WARNING - currently your code will work, next update: who knows?

If you do not want to touch the code that uses the deprecated parts you have few options: - you can stop upgrading (freeze your Scipy version - never to upgrade again)- you can change the code to use ndarray

Whatever path you take should probably be done after some reading into https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html) and making a risk analysis - if you use the code to do linear algrebra to land the next Rover III on Saturn, you should probably change it to something more reliable.

If you solve "watering" problems for your 3 tomatoe plants - worst things happening is you not going to get tomatoes and you have to buy them in a shop...