scikit-learn: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices
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)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 42 (36 by maintainers)
Commits related to this issue
- Filter scipy matrix subclass warning when using pytest This is known scipy/pytest issue, see here: https://github.com/scikit-learn/scikit-learn/issues/12327#issuecomment-459641865 — committed to tberlok/psecas by tberlok 5 years ago
- BUG: use `numpy.ndarray`, instead of `numpy.matrix` because matrices have been deprecated in `numpy`. Usage of `scipy.sparse` is causing this issue when conversions to matrices are performed. I chang... — committed to tulip-control/tulip-control by johnyf 3 years ago
- BUG: use `numpy.ndarray`, instead of `numpy.matrix` because matrices have been deprecated in `numpy`. Usage of `scipy.sparse` is causing this issue when conversions to matrices are performed. I chang... — committed to tulip-control/tulip-control by johnyf 3 years ago
- BUG: use `numpy.ndarray`, instead of `numpy.matrix` because matrices have been deprecated in `numpy`. Usage of `scipy.sparse` is causing this issue when conversions to matrices are performed. I chang... — committed to tulip-control/tulip-control by johnyf 3 years ago
- BUG: use `numpy.ndarray`, instead of `numpy.matrix` because matrices have been deprecated in `numpy`. Usage of `scipy.sparse` is causing this issue when conversions to matrices are performed. I chang... — committed to tulip-control/tulip-control by johnyf 3 years ago
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.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 matricestoarray
or only by adding use a redefinition with the following function (not fully implemented):If you avoid them, you avoid the warnings with pytest. Hope my toc helps someone.
This was fixed for scikit-learn in https://github.com/scikit-learn/scikit-learn/pull/13076 however keeping this issue open as this is still an issue for all projects that depend on scikit-learn (when running their test suites), unless they manually filter out the warning. I’m hoping someone comes up with some solution to avoid these manual changes.
In the project where I face the problem, I use
pytest
and I’ve added:under the
[pytest]
section ofpytest.ini
.As mentioned in https://github.com/scipy/scipy/issues/9734#issuecomment-459528915 this is sub-optimal but at least now the tests are green 😃
Opened https://github.com/scipy/scipy/issues/9734
Yes, the next scipy release is expected in a month or so https://mail.python.org/pipermail/scipy-dev/2018-October/023132.html so it shouldn’t be too long.
There is no need to filter warnings triggered by scipy itself since those will already be filtered by the next scipy version: scipy/scipy#8887.
That would not remove the warnings per se, that would just filter them out and we don’t want that.
Like @rth said, when some mathematical computation is being done on a sparse matrix, we need to convert it first to an array in order to avoid the warning.
To be fair the ‘easy’ tag is a bit misleading because when I looked at it, it wasn’t obvious at all where to make the matrix-to-array conversions. The conversions can be done in various places and it takes a good knowledge about the repo to identify where exactly it makes sense to convert an object, and where it makes sense to keep its original type.
Ok, I’ll pick it up unless @grassknoted updates soon.
Yes, thank you @grassknoted