索引以访问矩阵的元素

Indexing to access elements of a matrix

在下面的示例中,我正在创建 idxL 并且我想遍历它的元素以执行其他操作。我试图理解为什么 idxL[0][0] returns [[ True False False False False]] 而不是只返回 TrueidxL.item(0) 似乎有效。我想我可以使用它遍历 idxL 中的所有项目。但是,出于某种原因,我认为当我开始处理更大的数组时效率会降低。

from scipy.sparse import csr_matrix
a=['foo','panda','donkey','bird','egg']
b='foo'
idxL=csr_matrix((1,5), dtype=bool)
idxTemp=np.array(list(map(lambda x: x in b, a)))
idxL = idxL + idxTemp
print(idxL[0][0])
print(idxL.item(0))

发生这种情况是因为 idxL 不是 np.array,而是 np.matrix。 要将其转换为 numpy 数组,请参考属性 'A' 其中 return np.array.

import numpy as np
from scipy.sparse import csr_matrix
a=['foo','panda','donkey','bird','egg']
b='foo'
idxL=csr_matrix((1,5), dtype=bool)
idxL.todense()
idxTemp=np.array(list(map(lambda x: x in b, a)))
idxL = idxL + idxTemp
print(idxL.A[0][0])
print(idxL.item(0))

output:
True
True

编辑:如果你想继续使用稀疏,你应该将你的原始代码更改为

import numpy as np
from scipy.sparse import csr_matrix
a=['foo','panda','donkey','bird','egg']
b='foo'
idxL=csr_matrix((1,5), dtype=bool)
idxL.todense()
idxTemp=csr_matrix(list(map(lambda x: x in b, a)))
idxL = idxL + idxTemp
print(idxL[0][0])

现在 idxL 仍然是 csr_matrix 并且尊重 [] 索引。

In [193]: from scipy import sparse                                              
In [194]: a=['foo','panda','donkey','bird','egg'] 
     ...: b='foo' 
     ...: idxL=sparse.csr_matrix((1,5), dtype=bool) 
     ...: idxTemp=np.array(list(map(lambda x: x in b, a)))  

稀疏矩阵:

In [195]: idxL                                                                  
Out[195]: 
<1x5 sparse matrix of type '<class 'numpy.bool_'>'
    with 0 stored elements in Compressed Sparse Row format>
In [196]: idxL.A                                                                
Out[196]: array([[False, False, False, False, False]])

密阵;注意它是 1d

In [197]: idxTemp                                                               
Out[197]: array([ True, False, False, False, False])

索引稀疏矩阵:

In [198]: idxL[0,0]                                                             
Out[198]: False

加法 - 现在是一个稠密矩阵:

In [199]: idxLL = idxL + idxTemp                                                
In [200]: idxLL                                                                 
Out[200]: matrix([[ True, False, False, False, False]])
In [201]: idxLL[0,0]                                                            
Out[201]: True
矩阵的

[0] 选择第一行,但结果仍然是 2d。 [0][0] 索引没有帮助。这种索引方式适用于 2d ndarray,但 [0,0] 通常更好。

In [202]: idxLL[0]                                                              
Out[202]: matrix([[ True, False, False, False, False]])
In [203]: idxTemp[0]                                                            
Out[203]: True

编辑

我们可以直接从idxTemp生成一个稀疏矩阵:

In [257]: M = sparse.csr_matrix(idxTemp)                                        
In [258]: M                                                                     
Out[258]: 
<1x5 sparse matrix of type '<class 'numpy.bool_'>'
    with 1 stored elements in Compressed Sparse Row format>
In [259]: M.A                                                                   
Out[259]: array([[ True, False, False, False, False]])
In [260]: print(M)                                                              
  (0, 0)    True

不需要添加到idxL。可以添加:

In [261]: idxL+M                                                                
Out[261]: 
<1x5 sparse matrix of type '<class 'numpy.bool_'>'
    with 1 stored elements in Compressed Sparse Row format>

我不建议通过添加矩阵来构建备用矩阵。