微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

似乎无法在 Python 中计算拉普拉斯矩阵

如何解决似乎无法在 Python 中计算拉普拉斯矩阵

我一直在不知疲倦地试图找到拉普拉斯矩阵,但我似乎找不到我做错了什么。

这是我的代码

df = loadmat('/xxx/xxxx/pubmedanatomy.mat')['X']

# degree matrix
D = np.diag(np.sum(np.array(df.todense()),axis=1))
print('degree matrix:')
print(D)

# laplacian matrix
L = D - df
print('laplacian matrix:')
print(L)

其中名为 df 的矩阵的类型为:

<3291x13692 sparse matrix of type '<class 'numpy.float64'>'
    with 279414 stored elements in Compressed Sparse Column format>

输出错误

degree matrix:
[[88.000 0.000 0.000 ... 0.000 0.000 0.000]
 [0.000 59.000 0.000 ... 0.000 0.000 0.000]
 [0.000 0.000 47.000 ... 0.000 0.000 0.000]
 ...
 [0.000 0.000 0.000 ... 108.000 0.000 0.000]
 [0.000 0.000 0.000 ... 0.000 207.000 0.000]
 [0.000 0.000 0.000 ... 0.000 0.000 89.000]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-132-e404e383ee84> in <module>
      4 print(D)
      5 # laplacian matrix
----> 6 L = D - df
      7 print('laplacian matrix:')
      8 print(L)

/opt/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py in __rsub__(self,other)
    446                                       'nonzero scalar is not supported')
    447         elif isdense(other):
--> 448             other = np.broadcast_to(other,self.shape)
    449             return self._rsub_dense(other)
    450         else:

<__array_function__ internals> in broadcast_to(*args,**kwargs)

/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/stride_tricks.py in broadcast_to(array,shape,subok)
    178            [1,2,3]])
    179     """
--> 180     return _broadcast_to(array,subok=subok,readonly=True)
    181 
    182 

/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/stride_tricks.py in _broadcast_to(array,subok,readonly)
    123     it = np.nditer(
    124         (array,),flags=['multi_index','refs_ok','zerosize_ok'] + extras,--> 125         op_flags=['readonly'],itershape=shape,order='C')
    126     with it:
    127         # never really has writebackifcopy semantics

ValueError: operands Could not be broadcast together with remapped shapes [original->remapped]: (3291,3291) and requested shape (3291,13692)

正在计算度数矩阵,但拉普拉斯矩阵给了我ValueError。这里有什么问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。