Python NumPy 和 SciPy - .todense() 和 .toarray() 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30416695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NumPy and SciPy - Difference between .todense() and .toarray()
提问by
I am wondering if there is any difference (advantage/disadvantage) of using .toarray()
vs. .todense()
on sparse NumPy arrays. E.g.,
我想知道在稀疏 NumPy 数组上使用.toarray()
与使用是否有任何区别(优势/劣势).todense()
。例如,
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
采纳答案by user2357112 supports Monica
toarray
returns an ndarray; todense
returns a matrix. If you want a matrix, use todense
; otherwise, use toarray
.
toarray
返回一个 ndarray;todense
返回一个矩阵。如果你想要一个矩阵,请使用todense
; 否则,使用toarray
.