【问题标题】:Indexing to access elements of a matrix索引以访问矩阵的元素
【发布时间】:2020-02-18 16:17:37
【问题描述】:

在下面的示例中,我正在创建 idxL,并且我想遍历其元素以执行其他操作。我试图理解为什么idxL[0][0] 返回[[ 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[0,0] 是稀疏矩阵的正确方法,甚至对于 numpy 数组也是首选。
  • @hpaulj 不过,它似乎不起作用。我想知道为什么。
  • 什么不起作用?它有什么作用?在稀疏矩阵上循环是低效的,比在密集数组上循环更糟糕。坚持使用列表或字典

标签: python numpy matrix indexing sparse-matrix


【解决方案1】:
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>

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

【讨论】:

    【解决方案2】:

    这是因为 idxL 不是 np.array 而是 np.matrix。 要将其转换为 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_matrix 并支持 [] 索引。

    【讨论】:

    • 如果我不想将 idxL 转换为密集矩阵以节省内存怎么办?
    • 所以使用 item()。这是 np.matrix api。
    • 顺便说一句。打印中的 idxL 不再稀疏,因为添加。它被转换为 np.matrix
    • idxL[0,0] 是做什么的?
    猜你喜欢
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多