使用 Numpy 进行矩阵乘法

发布于 2024-09-16 01:30:29 字数 157 浏览 4 评论 0原文

假设我有一个亲和矩阵 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵情客 2024-09-23 01:30:29

请注意,建议使用 numpy 的 array 而不是 matrix:参见 用户指南中的此段落。一些响应中的混乱是可能出错的一个例子......特别是,D**0.5 和乘积如果应用于 numpy 数组,则元素级,这会给你一个错误的答案。例如:

import numpy as np
from numpy import dot, diag
D = diag([1., 2., 3.])
print D**(-0.5)
[[ 1.                 Inf         Inf]
 [        Inf  0.70710678         Inf]
 [        Inf         Inf  0.57735027]]

在您的情况下,矩阵是对角矩阵,因此矩阵的平方根只是另一个对角矩阵与对角元素的平方根。使用 numpy 数组,方程变为

D = np.array([1., 2., 3.]) # note that we define D just by its diagonal elements
A = np.cov(np.random.randn(3,100)) # a random symmetric positive definite matrix
L = dot(diag(D**(-0.5)), dot(A, diag(D**0.5)))

Please note that it is recommended to use numpy's array instead of matrix: 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:

import numpy as np
from numpy import dot, diag
D = diag([1., 2., 3.])
print D**(-0.5)
[[ 1.                 Inf         Inf]
 [        Inf  0.70710678         Inf]
 [        Inf         Inf  0.57735027]]

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

D = np.array([1., 2., 3.]) # note that we define D just by its diagonal elements
A = np.cov(np.random.randn(3,100)) # a random symmetric positive definite matrix
L = dot(diag(D**(-0.5)), dot(A, diag(D**0.5)))
太阳哥哥 2024-09-23 01:30:29

Numpy 允许您直接用正元素和正指数对对角“矩阵”求幂:

m = diag(range(1, 11))
print m**0.5

在这种情况下,结果正是您所期望的,因为 NumPy 实际上将求幂分别应用于 NumPy 数组的每个元素。

然而,它确实不允许您直接对任何 NumPy 矩阵求幂:

m = matrix([[1, 1], [1, 2]])
print m**0.5

产生您观察到的 TypeError(例外情况是指数必须是整数 - 即使对于可以用正系数对角化的矩阵也是如此)。

因此,只要你的矩阵 D 是对角矩阵并且你的指数是正数,你就应该能够直接使用你的公式。

Numpy allows you to exponentiate a diagonal "matrix" with positive elements and a positive exponent directly:

m = diag(range(1, 11))
print m**0.5

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:

m = matrix([[1, 1], [1, 2]])
print m**0.5

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.

可是我不能没有你 2024-09-23 01:30:29

好吧,我看到的唯一问题是,如果您使用的是 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 对矩阵按元素求幂。所以你只需使用

from numpy import power
power(D, -.5) * A * power(D, .5)

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 use

from numpy import power
power(D, -.5) * A * power(D, .5)
月寒剑心 2024-09-23 01:30:29

numpy 有矩阵的平方根函数吗?然后你可以做 sqrt(D) 而不是 (D**(1/2))

也许这个公式真的应该写成

L = (D**(-1/2)) * A * (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

L = (D**(-1/2)) * A * (D**(1/2)) 

Based on previous comment this formula should work in case of D being diagonal matrix (I have not chance to prove it now).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文