简体   繁体   English

访问稀疏 CSR 矩阵中的列

[英]Accessing column in sparse CSR matrix

Having some issues with accessing the last column in the sparse CSR matrix.在访问稀疏 CSR 矩阵中的最后一列时遇到一些问题。 Ideally, I would like to convert the last column into some sort of array that can be used as my label set.理想情况下,我想将最后一列转换为某种数组,可以用作我的 label 集。 My CSR matrix looks like this:我的 CSR 矩阵如下所示:

(0, 1976)   1
  (0, 2916) 1
  (0, 3871) 1
  (0, 4437) 1
  (0, 8202) 1
  (0, 9458) 1
  (0, 10597)    1
  (1, 4801) 1
  (1, 6903) 1
  (1, 7525) 1
  (2, 873)  1
  (2, 1017) 1
  (2, 1740) 1
  (2, 1925) 1
  (3, 1976) 1
  (3, 5606) 1
  (3, 6898) 1

I want to access the last column, which contains all the '1'.我想访问包含所有“1”的最后一列。 Is there a way in which I can do this?有没有办法可以做到这一点?

CSR matrix has indicies and indptr properties, see below example which converts matrix to list using these properties: CSR 矩阵具有indiciesindptr属性,请参见下面的示例,该示例使用这些属性将矩阵转换为列表:

def sparse_to_string_list(matrix: csr_matrix):
    res = []
    indptr = matrix.indptr
    indices = matrix.indices
    for row in range(matrix.shape[0]):
        arr = [k for k in indices[indptr[row]: indptr[row + 1]]]
        arr.sort()
        res.append(' '.join([str(k) for k in arr]))
    return res

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

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