numpy重新索引到前N个自然数 [英] Numpy re-index to first N natural numbers
问题描述
我有一个索引非常稀疏的矩阵(行和列中的最大值都超过130000),但是实际上只有少数几行/列具有非零值.
I have a matrix that has a quite sparse index (the largest values in both rows and columns are beyond 130000), but only a few of those rows/columns actually have non-zero values.
因此,我想通过前N个自然数将行索引和列索引转换为仅表示非零索引.
Thus, I want to have the row and column indices shifted to only represent the non-zero ones, by the first N natural numbers.
视觉上,我想要一个这样的示例矩阵
Visually, I want a example matrix like this
1 0 1
0 0 0
0 0 1
看起来像这样
1 1
0 1
,但仅当行/列中的所有值均为零时. 由于我确实具有稀疏格式的矩阵,因此我可以简单地创建一个字典,通过递增的计数器存储每个值(分别用于行和矩阵),然后得到结果.
but only if all values in the row/column are zero. Since I do have the matrix in a sparse format, I could simply create a dictionary, store every value by an increasing counter (for row and matrix separately), and get a result.
row_dict = {}
col_dict = {}
row_ind = 0
col_ind = 0
# el looks like this: (row, column, value)
for el in sparse_matrix:
if el[0] not in row_dict.keys():
row_dict[el[0]] = row_ind
row_ind += 1
if el[1] not in col_dict.keys():
col_dict[el[1]] = col_ind
col_ind += 1
# now recreate matrix with new index
但是我一直在寻找NumPy的内部函数.还要注意,我真的不知道该如何措辞,所以很可能有一个我不知道的重复词;任何朝着正确方向的指针都值得赞赏.
But I was looking for maybe an internal function in NumPy. Also note that I do not really know how to word the question, so there might well be a duplicate out there that I do not know of; Any pointers in the right direction are appreciated.
推荐答案
您可以使用np.unique
:
>>> import numpy as np
>>> from scipy import sparse
>>>
>>> A = np.random.randint(-100, 10, (10, 10)).clip(0, None)
>>> A
array([[6, 0, 5, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 0, 0, 0, 0, 4, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 4, 0],
[9, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 4, 0, 0, 0, 0, 0, 0]])
>>> B = sparse.coo_matrix(A)
>>> B
<10x10 sparse matrix of type '<class 'numpy.int64'>'
with 8 stored elements in COOrdinate format>
>>> runq, ridx = np.unique(B.row, return_inverse=True)
>>> cunq, cidx = np.unique(B.col, return_inverse=True)
>>> C = sparse.coo_matrix((B.data, (ridx, cidx)))
>>> C.A
array([[6, 5, 0, 0, 0],
[0, 0, 7, 4, 9],
[0, 0, 0, 4, 0],
[9, 0, 0, 0, 0],
[0, 0, 4, 0, 0]])
这篇关于numpy重新索引到前N个自然数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!