Proposal: Add a todense method to matrixes.
See original GitHub issueThe method would be an identity map for numpy matrixes.
It is needed to match the interface offered by scipy. The particular situation which tripped me up was the following:
>>> # Preamble
>>> import scipy.sparse as S
>>> diagonals = [[1, 2, 3, 4], [1, 2, 3], [1, 2]]
>>> tmp = S.diags(diagonals, [0, -1, 2])
>>> tmp_lil = tmp.tolil()
>>> tmp_lil_0 = tmp_lil[0,:]
>>> tmp_lil_0[ tmp_lil_0.nonzero() ] # calling .todense() is necessary
<1x2 sparse matrix of type '<type 'numpy.float64'>'
with 2 stored elements in LInked List format>
>>> tmp_csc = tmp.tocsc()
>>> tmp_csc_0 = tmp_csc[0,:]
>>> tmp_csc_0[ tmp_csc_0.nonzero() ] # calling .todense() raises an exception
matrix([[ 1., 1.]])
I find the case to add the method compelling and do not see any obvious downsides. If it is acceptable to add it, then should it return the same object or should it return a copy?
With this information, I should be able to work on a pull request (#6066)
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
todense(), toarray() should be a module-level function and ...
I understand and agree that the kind of duck typing proposed in #5038 (adding scipy.sparse functions to numpy.matrix ) is not necessarily ...
Read more >scipy.sparse.csc_matrix.todense — SciPy v1.9.3 Manual
A NumPy matrix object with the same shape and containing the same data represented by the sparse matrix, with the requested memory order....
Read more >NumPy matrix to SciPy sparse matrix: What is the safest ...
Admittedly sparse matrices aren't really in my wheelhouse, but ISTM the best way forward depends on the matrix type. If you're DOK:
Read more >NEP 13 — A mechanism for overriding Ufuncs
Here we propose adding a mechanism to override ufuncs based on the ufunc checking each of it's arguments for a __array_ufunc__ method.
Read more >A Gentle Introduction to Sparse Matrices for Machine ...
The sparse matrix is represented using three one-dimensional arrays for the non-zero values, the extents of the rows, and the column indexes.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @musically-ut, but I don’t think this enhancement request is going to be implemented.
Well, it is 6 years later; I’ll argue against it! Knowing that
np.matrix
is destined for deprecation, it doesn’t make sense to incur the maintenance burden (answer design questions, add unit tests and run them with every PR, update documentation, etc) just to have it all go away whenmatrix
is finally deprecated.Given the lack of activity in this issue for the last 6 years, I think we can close this now.
Actually, the fix to scipy is going to break backwards compatibility.
Hence, I am reopening this issue to see if it is worth resolving the issue at the numpy level by adding some functionality and preserving backwards compatibility.