Numpy and Scipy - Difference Between. Todense() And. Toarray()

Numpy and Scipy - Difference Between. Todense() And. Toarray()

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

1 Answer

toarray returns an ndarray; todense returns a matrix. If you want a matrix, use todense; otherwise, use toarray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.