【问题标题】:Is there any way to compute the Hadamard product of COO sparse matrices leveraging its structure?有什么方法可以利用其结构计算 COO 稀疏矩阵的 Hadamard 积?
【发布时间】:2019-06-25 15:36:11
【问题描述】:

我正在尝试计算以 COO 稀疏格式(在 SciPy 中)存储的两个矩阵的 Hadamard 积。有一个lecture,上面写着

快速简单的逐项操作,直接操作数据数组(快速 NumPy 机器)

我理解和和差部分,因为我们可以连接 matrix.row、matrix.column 和 matrix.data。但是,我想通过利用 COO 结构来计算 Hadamard/entry-wise 乘积实际上并不简单(可能必须匹配坐标并应用“&”)。

有什么方法可以使用 COO 稀疏格式更有效地计算 Hadamard 积,还是只是一种误解?

【问题讨论】:

  • 所有注释的意思是你可以用M.data = np.sin(M.data) 来计算矩阵中所有非零元素的sin(或其他一些函数)。它不是指二元运算(乘积、求和、减法等)。
  • @hpaulj 知道了!非常感谢!我真的读得太多了哈哈

标签: python scipy elementwise-operations


【解决方案1】:

multiply 方法进行元素乘法。

这是一个示例,其中ab 是具有COO 格式的稀疏矩阵。 (.A 属性返回一个常规的 numpy 数组。我用它来显示稀疏矩阵中的值。)

In [41]: a                                                                                                                            
Out[41]: 
<5x8 sparse matrix of type '<class 'numpy.int64'>'
    with 20 stored elements in COOrdinate format>

In [42]: a.A                                                                                                                          
Out[42]: 
array([[0, 9, 2, 9, 0, 6, 6, 2],
       [2, 0, 0, 0, 1, 0, 8, 0],
       [0, 3, 0, 0, 2, 9, 0, 4],
       [0, 0, 0, 0, 0, 0, 0, 5],
       [0, 0, 7, 1, 0, 0, 7, 7]])

In [43]: b                                                                                                                            
Out[43]: 
<5x8 sparse matrix of type '<class 'numpy.int64'>'
    with 20 stored elements in COOrdinate format>

In [44]: b.A                                                                                                                          
Out[44]: 
array([[0, 0, 0, 7, 9, 0, 5, 0],
       [0, 7, 0, 0, 6, 6, 0, 0],
       [3, 0, 2, 0, 3, 0, 0, 0],
       [5, 0, 0, 3, 0, 0, 7, 0],
       [8, 0, 6, 8, 0, 0, 4, 0]])

计算ab 的元素乘积。请注意,c 使用 CSR 格式。

In [45]: c = a.multiply(b)                                                                                                            

In [46]: c                                                                                                                            
Out[46]: 
<5x8 sparse matrix of type '<class 'numpy.int64'>'
    with 7 stored elements in Compressed Sparse Row format>

In [47]: c.A                                                                                                                          
Out[47]: 
array([[ 0,  0,  0, 63,  0,  0, 30,  0],
       [ 0,  0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 42,  8,  0,  0, 28,  0]], dtype=int64)

通过计算相应 numpy 数组的元素乘积来验证结果。

In [48]: a.A * b.A                                                                                                                    
Out[48]: 
array([[ 0,  0,  0, 63,  0,  0, 30,  0],
       [ 0,  0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 42,  8,  0,  0, 28,  0]])

【讨论】:

  • 糟糕,我意识到这种方法在文档中被描述为逐点产品。感谢你的分享!另外很高兴知道该方法本质上将其转换为 CSR 并进行计算。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 2016-10-29
  • 2016-08-15
  • 1970-01-01
  • 2017-07-20
相关资源
最近更新 更多