简体   繁体   English

如何在Scipy Python稀疏矩阵中实现CSR_Matrix的循环置换(左移和右移)?

[英]How to implement Circular Permutation (left and right shift) of CSR_Matrix in Scipy Python Sparse Matrices?

I am using Scipy sparse matrix csr_matrix to be used as context vectors in word-context vectors. 我使用Scipy稀疏矩阵csr_matrix作为字上下文向量中的上下文向量。 My csr_matrix is a (1, 300) shape so it is a 1-dimensional vector. 我的csr_matrix(1, 300) csr_matrix (1, 300)形状,因此它是一维向量。

I need to use permutation (circular right shift or circular left shift) on the sparse vector (for showing left context and right context). 我需要在稀疏向量上使用置换(圆右移或圆左移)(用于显示左上下文和右上下文)。

example: i have [1, 2, 3, 4] and i want to create right and left permutations as follow: 例如:我有[1, 2, 3, 4] ,我想创建左右排列,如下所示:

right permutation: [4, 1, 2, 3] 正确的排列: [4, 1, 2, 3] 4,1,2,3 [4, 1, 2, 3]
left permutation: [2, 3, 4, 1] 左置换: [2, 3, 4, 1] 2,3,4,1 [2, 3, 4, 1]

In csr matrices i can't access to column indices so i can not just change the column indices. 在csr矩阵中,我无法访问列索引,所以我不能只改变列索引。

Is there any efficient high performance solution for row permutations in csr_matrix or am i missing something? csr_matrix行排列是否有任何有效的高性能解决方案,或者我错过了什么?

runnable code: 可运行代码:

from scipy.sparse import csr_matrix
rows = [0, 0, 0]
columns = [100, 47, 150]
data = [-1, +1, -1]
contextMatrix = csr_matrix( (data,(rows, columns)), shape=(1, 300) )

it means that i have a 300-column vector whose columns 100, 47, 150 all from row 0 are non-zero valued and their value is in data list respectively. 这意味着我有一个300列向量,其列100,47,150都来自行0,它们的值都是非零值,它们的值分别在数据列表中。

now what i want is a permutation which means i want the columns array be changed into [101, 48, 151] for right permutation and [99, 46, 149] for left permutation. 现在我想要的是一个排列,这意味着我希望将列数组更改为[101,48,151]以进行右置换,并将[99,46,149]更改为左置换。

It should be noted that permutations are circular which means if column 299 has non-zero data, using a right permutation the data will be moved to column 0. 应当注意,排列是循环的,这意味着如果列299具有非零数据,则使用右排列,数据将被移动到列0。

You can access and alter the data and indices attributes of your CSR matrix, which are stored as NumPy arrays. 您可以访问和更改CSR矩阵的dataindices属性,这些属性存储为NumPy数组。

http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix

So using your code and following the suggestion in the comments you could do this: 因此,使用您的代码并遵循评论中的建议,您可以这样做:

from scipy.sparse import csr_matrix
rows = [0, 0, 0]
columns = [100, 47, 150]
data = [-1, +1, -1]
m = csr_matrix( (data,(rows, columns)), shape=(1, 300) )

indices = m.indices

# right permutation
m.indices = (indices + 1) % m.shape[1]

# left permutation
m.indices = (indices - 1) % m.shape[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM