[英]flip rows of sparse array in python
I have a sparse array, say: 我有一个稀疏数组,说:
from scipy import sparse
a = sparse.lil_matrix((2,3),)
a[0] = [1, 2, 3]
a[1, 2] = 5
so it looks like: 所以看起来像:
(0, 0) 1.0
(0, 1) 2.0
(0, 2) 3.0
(1, 2) 5.0
I was wondering - is there an easy way to flip the rows (something like numpy.fliplr
equivalent)? 我想知道-是否有一种简单的方法来翻转行(类似于
numpy.fliplr
)? ...so I would get the output as: ...所以我将得到的输出为:
(0, 0) 3.0
(0, 1) 2.0
(0, 2) 1.0
(1, 0) 5.0
One way would be to convert the array to csr
format, and then manipulate the row indices: 一种方法是将数组转换为
csr
格式,然后处理行索引:
from scipy import sparse
a = sparse.lil_matrix((2,3),)
a[0] = [1, 2, 3]
a[1, 2] = 5
a = a.tocsr()
a.indices = -a.indices + a.shape[1] - 1
print(a)
yields 产量
(0, 2) 1.0
(0, 1) 2.0
(0, 0) 3.0
(1, 0) 5.0
You could do multi-index assignment: 您可以执行多索引分配:
ii = [2,1,0,3]
a[:,1] = a[ii,:]
Where ii
is an array of indexes you have to create somehow. 其中
ii
是必须以某种方式创建的索引数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.