什么是行切片?什么是列切片?

2024-09-28 19:35:43 发布

您现在位置:Python中文网/ 问答频道 /正文

是的,我读过thisthis answer,但我仍然不能把握住周围的想法。。。这是一个基本问题。在

在:

M[:, index]
M[index, :]

哪一个是行切片,哪个是列切片

对于我的问题,如果我想对列执行advanced indexing操作,例如:

^{pr2}$

{


Tags: answerindex切片thisadvancedindexingpr2把握住
3条回答

每隔一段时间把顺序弄混没关系,我的技巧是画一个矩阵,记住索引顺序从上到下,然后从左到右计数:https://en.wikipedia.org/wiki/Index_notation

down, left

因此,由于:表示all,所以您知道[:, i]表示所有行,[i, :]表示所有列。在

对于问题的第二部分:您想要M[:, indices],所以诀窍就在名称中:如果您在列上循环(这是因为您为所有行指定了列索引),那么您需要压缩稀疏列格式。在你链接的文档中是这样写的:

Advantages of the CSC format

  • ...
  • efficient column slicing

实际上,行/列切片:这些都是行/列索引的例子。在

  • M[index, :]是行索引
  • M[:, index]是列索引
  • M[start:stop, :]是行切片
  • M[:, start:stop]是列切片

CSC在检索整个列时效率更高:特定列的非零值和匹配的行索引在内存中作为连续数组存储。在

对于CSR和整行的检索,dual是正确的。在

虽然csr的行选择比列选择更快,但差别不大:

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的列索引有更大的速度改进。在

而单行或列,csrcsc有{}方法:

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://stackoverflow.com/a/39500986/901925中,我重新创建了extractor代码,sparse用来获取行或列。它构造了一个新的1和0的稀疏“向量”,并使用矩阵乘法来“选择”行或列。在

相关问题 更多 >