question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices

See original GitHub issue

When running a the test suite we get a number PendingDeprecationWarning,

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)

See for instance https://ci.appveyor.com/project/sklearn-ci/scikit-learn/builds/19341228/job/q6rysq7flma8hi6v Warnings seems to be hidden on Travis so we don’t see those there.

This typically happens when we have a sparse matrix, sum along one axis to get a dense one, than do some mathematical operation with this matrix. It should be explicitly converted to an array first.

This has also been reported by @drorata in https://github.com/scikit-learn/scikit-learn/pull/11251#issuecomment-427865173. There was previous discussion about this in https://github.com/scikit-learn/scikit-learn/pull/11251 but it doesn’t look like it’s fixed. I also saw this recently when running PyPy CI.

To investigate where this happen it’s sufficient to run pytest tests with -Werror::PendingDeprecationWarning to error on those warnings, as they don’t seem to be raised with the right stacklevel (see also https://github.com/scikit-learn/scikit-learn/issues/7963#issuecomment-265420200 for workarounds)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:42 (36 by maintainers)

github_iconTop GitHub Comments

5reactions
rthcommented, Oct 11, 2018

It’s not a regression, and not a blocker for 0.20.1 (please feel free to untag it), but it still fairly annoying as it means we get PendingDeprecationWarning when using the latest scipy and it’s better to fix that as soon as possible.

3reactions
Gseguelgcommented, Mar 5, 2019

In my case, within my code, changing from “.todense()” to “.toarray()” for sparse matrices removed the warnings. As well as “.sum()” over the some axis on sparse matrix are converted to np.matrix. Here may lie the specific problem. This last one forced me to convert sparse matrices toarray or only by adding use a redefinition with the following function (not fully implemented):

import numpy as np
def sum_rows(A):
    """ Sumation along the rows (add all elements of the columns for each row independently).
        Similar to A.sum(axis=1) but return csr_matrix. Used to avoid pytest deprecation warning.
        A must be csr_matrix to be 'efficient'.
    """
    # about A
    NRows_A, NCols_A = A.shape
    indxrow_A, indxcol_A = A.nonzero()
    # about Aux
    NCols_aux = 1
    NRows_aux = NCols_A
    # drop duplicates of indxcol_A to set ones on Aux (to not multiply > 1)
    row_indx_aux = np.unique(indxcol_A)
    col_indx_aux = [0] * len(row_indx_aux)
    data = [1] * len(indxcol_A)
    Aux = sps__csr_matrix( (data, (row_indx_aux, col_indx_aux)), (NRows_aux, NCols_aux) )
    return A * Aux

If you avoid them, you avoid the warnings with pytest. Hope my toc helps someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve issue of PendingDeprecationWarning: the matrix ...
PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.
Read more >
replace numpy.matrix with numpy.array (#42) - Inkscape - GitLab
Using numpy.matrix yields this annoying PendingDeprecationWarning: ... the matrix subclass is not the recommended way to represent matrices ...
Read more >
PendingDeprecationWarning: numpy matrix - Google Groups
PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see ...
Read more >
Pending DeprecationWarning for matrix subclass (#34) · Issues
... PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see ...
Read more >
Streamlit gives numpy deprecation warning with pytest
PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found