NumPy and SciPy – Difference between .todense() and .toarray()
Question:
I am wondering if there is any difference (advantage/disadvantage) of using .toarray()
vs. .todense()
on sparse NumPy arrays. E.g.,
import scipy as sp
import numpy as np
sparse_m = sp.sparse.bsr_matrix(np.array([[1,0,0,0,1], [1,0,0,0,1]]))
%timeit sparse_m.toarray()
1000 loops, best of 3: 299 µs per loop
%timeit sparse_m.todense()
1000 loops, best of 3: 305 µs per loop
Answers:
toarray
returns an ndarray; todense
returns a matrix. If you want a matrix, use todense
; otherwise, use toarray
.
As its documentation states, using numpy.matrix
is discouraged as it may be removed in the future. So it’s probably better to use toarray()
rather than todense()
, especially since the matrix operations that were convenient on matrix
objects are now possible on ndarray
objects since numpy 1.10.
I am wondering if there is any difference (advantage/disadvantage) of using .toarray()
vs. .todense()
on sparse NumPy arrays. E.g.,
import scipy as sp
import numpy as np
sparse_m = sp.sparse.bsr_matrix(np.array([[1,0,0,0,1], [1,0,0,0,1]]))
%timeit sparse_m.toarray()
1000 loops, best of 3: 299 µs per loop
%timeit sparse_m.todense()
1000 loops, best of 3: 305 µs per loop
toarray
returns an ndarray; todense
returns a matrix. If you want a matrix, use todense
; otherwise, use toarray
.
As its documentation states, using numpy.matrix
is discouraged as it may be removed in the future. So it’s probably better to use toarray()
rather than todense()
, especially since the matrix operations that were convenient on matrix
objects are now possible on ndarray
objects since numpy 1.10.