4
votes

I am using a python code as a black box that I do not want to touch. The code was working well using Python under Ubuntu 12.04 but after upgrading the system to Ubuntu 16, I got the below warning which interrupt the code from running. Any idea how I can fix this without changing the code? Many thanks.

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.

2
Looks like a scipy.sparse matrix todense is triggering this. The ideal is to replace this with a toarray method call.hpaulj
Many thanks this helps!user144209
Which numpy and scipy versions are you using? I know we discourage the use of np.matrix, but I wasn't aware of any actual move toward deprecation.hpaulj
I found that warning message in the np.matrixlib.defmatrix.py file (1.16.3). Your warnings level must be different than mine (default).hpaulj

2 Answers

2
votes

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.

0
votes

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