划分scipy.lil iu矩阵通过

2024-04-20 15:42:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把我的稀疏scipy.lil iu矩阵再通过向量求出稀疏矩阵。假设我有2个lil\u矩阵变量和2个numpy数组a, b, c, d,如下所示

In [1]: a
Out[1]: 
<4x3 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in LInked List format>

In [2]: b
Out[2]: 
<4x1 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in LInked List format>

In [3]: c
Out[3]: 
array([[ 0.,  1.,  2.],
       [ 1.,  2.,  3.],
       [ 2.,  3.,  4.],
       [ 3.,  4.,  5.]])

In [4]: d
Out[4]: 
array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.]])

划分numpy矩阵/向量是完美的

^{pr2}$

但是分割lil峎矩阵/向量会引起值错误:形状不一致

^{3}$

有什么好方法可以用向量来划分稀疏矩阵吗?在


Tags: ofinnumpytypewith矩阵elementsout
1条回答
网友
1楼 · 发布于 2024-04-20 15:42:46

Is there some nice way to divide sparse matrices by vector?

您可以使用multiply()方法,结果仍然是稀疏矩阵,但采用coo格式:

>>> a
<4x3 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in LInked List format>
>>> d
array([[1.],
       [2.],
       [3.],
       [4.]])
>>> a.multiply(1/d)
<4x3 sparse matrix of type '<class 'numpy.float64'>'
    with 11 stored elements in COOrdinate format>

相关问题 更多 >