使用 Numpy 进行矩阵乘法
假设我有一个亲和矩阵 A 和一个对角矩阵 D。如何使用 nympy 在 Python 中计算拉普拉斯矩阵?
L = D^(-1/2) AD^(1/2)
目前,我使用 L = D**(-1/2) * A * D**(1/2)。这是正确的方法吗?
谢谢。
Assume that I have an affinity matrix A and a diagonal matrix D. How can I compute the Laplacian matrix in Python with nympy?
L = D^(-1/2) A D^(1/2)
Currently, I use L = D**(-1/2) * A * D**(1/2). Is this a right way?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,建议使用 numpy 的
array
而不是matrix
:参见 用户指南中的此段落。一些响应中的混乱是可能出错的一个例子......特别是,D**0.5 和乘积如果应用于 numpy 数组,则元素级,这会给你一个错误的答案。例如:在您的情况下,矩阵是对角矩阵,因此矩阵的平方根只是另一个对角矩阵与对角元素的平方根。使用 numpy 数组,方程变为
Please note that it is recommended to use numpy's
array
instead ofmatrix
: see this paragraph in the user guide. The confusion in some of the responses is an example of what can go wrong... In particular, D**0.5 and the products are elementwise if applied to numpy arrays, which would give you a wrong answer. For example:In your case, the matrix is diagonal, and so the square root of the matrix is just another diagonal matrix with the square root of the diagonal elements. Using numpy arrays, the equation becomes
Numpy 允许您直接用正元素和正指数对对角“矩阵”求幂:
在这种情况下,结果正是您所期望的,因为 NumPy 实际上将求幂分别应用于 NumPy 数组的每个元素。
然而,它确实不允许您直接对任何 NumPy 矩阵求幂:
产生您观察到的 TypeError(例外情况是指数必须是整数 - 即使对于可以用正系数对角化的矩阵也是如此)。
因此,只要你的矩阵 D 是对角矩阵并且你的指数是正数,你就应该能够直接使用你的公式。
Numpy allows you to exponentiate a diagonal "matrix" with positive elements and a positive exponent directly:
The result is what you expect in this case because NumPy actually applies the exponentiation to each element of the NumPy array individually.
However, it indeed does not allow you to exponentiate any NumPy matrix directly:
produces the TypeError that you have observed (the exception says that the exponent must be an integer–even for matrices that can be diagonalized with positive coefficients).
So, as long as your matrix D is diagonal and your exponent is positive, you should be able to directly use your formula.
好吧,我看到的唯一问题是,如果您使用的是 Python 2.6.x(没有
from __future__ import div
),那么 1/2 将被解释为 0,因为它将被视为整数除法。您可以通过使用 D**(-.5) * A * D**.5 来解决此问题。您还可以强制使用 1./2 而不是 1/2 进行浮点除法。除此之外,它对我来说看起来是正确的。
编辑:
我试图对一个numpy数组求幂,而不是之前的矩阵,它与
D**.5
一起使用。您可以使用 numpy.power 对矩阵按元素求幂。所以你只需使用Well, the only problem I see is that if you are using Python 2.6.x (without
from __future__ import division
), then 1/2 will be interpreted as 0 because it will be considered integer division. You can get around this by using D**(-.5) * A * D**.5 instead. You can also force float division with 1./2 instead of 1/2.Other than that, it looks correct to me.
Edit:
I was trying to exponentiate a numpy array, not a matrix before, which works with
D**.5
. You can exponentiate a matrix element-wise using numpy.power. So you would just usenumpy 有矩阵的平方根函数吗?然后你可以做 sqrt(D) 而不是 (D**(1/2))
也许这个公式真的应该写成
基于之前的评论这个公式应该在 D 是对角矩阵的情况下工作(我没有机会证明它现在)。
Does numpy have square root function for matrixes? Then you could do sqrt(D) instead of (D**(1/2))
Maybe the formula should realy be written
Based on previous comment this formula should work in case of D being diagonal matrix (I have not chance to prove it now).