In [288]: Mbig=sparse.rand(1000,1000,.1, 'csr')
In [289]: Mbig[:1000:50,:]
Out[289]:
<20x1000 sparse matrix of type '<class 'numpy.float64'>'
with 2066 stored elements in Compressed Sparse Row format>
In [290]: timeit Mbig[:1000:50,:]
1000 loops, best of 3: 1.53 ms per loop
In [291]: timeit Mbig[:,:1000:50]
100 loops, best of 3: 2.04 ms per loop
In [292]: Mbig=sparse.rand(1000,1000,.1, 'csc')
In [293]: timeit Mbig[:1000:50,:]
100 loops, best of 3: 2.16 ms per loop
In [294]: timeit Mbig[:,:1000:50]
1000 loops, best of 3: 1.65 ms per loop
转换格式是不值得的
^{pr2}$
与致密版的相同切片进行对比:
In [297]: A=Mbig.A
In [298]: timeit A[:,:1000:50]
...
1000000 loops, best of 3: 557 ns per loop
In [301]: timeit A[:,:1000:50].copy()
...
10000 loops, best of 3: 52.5 µs per loop
为了使比较复杂化,使用数组(numpyadvanced)建立索引实际上比使用“slice”更快:
In [308]: idx=np.r_[0:1000:50] # expand slice into array
In [309]: timeit Mbig[idx,:]
1000 loops, best of 3: 1.49 ms per loop
In [310]: timeit Mbig[:,idx]
1000 loops, best of 3: 513 µs per loop
在这里,csc的列索引有更大的速度改进。在
而单行或列,csr和csc有{}方法:
In [314]: timeit Mbig.getrow(500)
1000 loops, best of 3: 434 µs per loop
In [315]: timeit Mbig.getcol(500) # 1 column from csc is fastest
10000 loops, best of 3: 78.7 µs per loop
In [316]: timeit Mbig[500,:]
1000 loops, best of 3: 505 µs per loop
In [317]: timeit Mbig[:,500]
1000 loops, best of 3: 264 µs per loop
每隔一段时间把顺序弄混没关系,我的技巧是画一个矩阵,记住索引顺序从上到下,然后从左到右计数:https://en.wikipedia.org/wiki/Index_notation
因此,由于
:
表示all,所以您知道[:, i]
表示所有行,[i, :]
表示所有列。在对于问题的第二部分:您想要
M[:, indices]
,所以诀窍就在名称中:如果您在列上循环(这是因为您为所有行指定了列索引),那么您需要压缩稀疏列格式。在你链接的文档中是这样写的:实际上,行/列切片:这些都是行/列索引的例子。在
M[index, :]
是行索引M[:, index]
是列索引M[start:stop, :]
是行切片M[:, start:stop]
是列切片CSC在检索整个列时效率更高:特定列的非零值和匹配的行索引在内存中作为连续数组存储。在
对于CSR和整行的检索,dual是正确的。在
虽然
csr
的行选择比列选择更快,但差别不大:转换格式是不值得的
^{pr2}$与致密版的相同切片进行对比:
为了使比较复杂化,使用数组(
numpy
advanced)建立索引实际上比使用“slice”更快:在这里,
csc
的列索引有更大的速度改进。在而单行或列,}方法:
csr
和csc
有{在https://stackoverflow.com/a/39500986/901925中,我重新创建了
extractor
代码,sparse
用来获取行或列。它构造了一个新的1和0的稀疏“向量”,并使用矩阵乘法来“选择”行或列。在相关问题 更多 >
编程相关推荐