Python 访问矩阵元素的索引

Python 访问矩阵元素的索引,python,numpy,matrix,indexing,sparse-matrix,Python,Numpy,Matrix,Indexing,Sparse Matrix,在下面的示例中,我正在创建idxL,我希望通过它的元素循环执行其他操作。我试图理解为什么idxL[0][0]返回[[True-False-False]],而不是只返回True。idxL.item0似乎可以工作。我想我可以使用它循环idxL中的所有项目。然而,出于某种原因,我认为当我开始处理更大的阵列时,效率会降低 from scipy.sparse import csr_matrix a=['foo','panda','donkey','bird','egg'] b='foo' idxL=csr

在下面的示例中,我正在创建idxL,我希望通过它的元素循环执行其他操作。我试图理解为什么idxL[0][0]返回[[True-False-False]],而不是只返回True。idxL.item0似乎可以工作。我想我可以使用它循环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,而是。 要将其转换为numpy数组,请参考返回np.array的属性“A”

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_矩阵,并尊重[]索引

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]])
In [198]: idxL[0,0]                                                             
Out[198]: False
为稀疏矩阵编制索引:

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]])
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[0,0]是稀疏矩阵的正确方法,甚至对于numpy数组也是首选。@hpaulj但它似乎不起作用。我想知道为什么。什么不起作用?它有什么作用?在稀疏矩阵上循环是低效的,比在密集数组上循环更糟糕。坚持使用列表或命令如果我不想将idxL转换为密集矩阵以节省内存,请使用该项。这是np.matrix api.BTW。打印中的idxL不再稀疏,因为添加了。它被铸造到np.MatrixIDXL[0,0]做什么?