Python 中的矩阵和逆矩阵

发布于 2024-08-13 21:32:31 字数 883 浏览 2 评论 0原文

对于我正在做的项目,我使用 NetworkX adj_matrix() 函数将使用 NetworkX 创建的图分解为邻接矩阵。然而,我遇到的问题之一是,当我尝试找到矩阵的逆矩阵时,我分解的每个图都会给出以下错误。

str: Traceback (most recent call last):
  File "C:\eclipse\plugins\org.python.pydev.debug_1.4.7.2843\pysrc\pydevd_resolver.py", line 179, in _getPyDictionary
    attr = getattr(var, n)
  File "C:\Python26\lib\site-packages\numpy\core\defmatrix.py", line 519, in getI
    return asmatrix(func(self))
  File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 355, in inv
    return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
  File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 254, in solve
    raise LinAlgError, 'Singular matrix'
LinAlgError: Singular matrix

我尝试从 5 个不同的图生成邻接矩阵,当我试图找到邻接矩阵的逆矩阵时,它们都产生了相同的错误。我提出的问题是是否有任何方法可以从 NetworkX 图到矩阵。从这里我最好的行动方针是什么?我意识到还有其他与矩阵逆有关的问题,但我的问题在某种程度上受到我需要图邻接矩阵这一事实的限制。

For a project that I am doing, I decompose a graph that I created using NetworkX into an adjacency matrix using the NetworkX adj_matrix() function. However, one of the problems that I have come across is that every single graph that I decompose gives me the following error when I try to find the inverse of the matrix.

str: Traceback (most recent call last):
  File "C:\eclipse\plugins\org.python.pydev.debug_1.4.7.2843\pysrc\pydevd_resolver.py", line 179, in _getPyDictionary
    attr = getattr(var, n)
  File "C:\Python26\lib\site-packages\numpy\core\defmatrix.py", line 519, in getI
    return asmatrix(func(self))
  File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 355, in inv
    return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
  File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 254, in solve
    raise LinAlgError, 'Singular matrix'
LinAlgError: Singular matrix

I tried generating adjacency matrices from 5 different graphs and all of them produced the same error when I tried to find the inverse of the adjacency matrix. The question that I pose is whether there is any way to go from NetworkX graph to matrix. What is my best course of action from here? I realize there are other questions pertaining to matrix inverses, but mine is somewhat limited by the fact that I need the graph adjacency matrix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

贵在坚持 2024-08-20 21:32:31

邻接矩阵并不总是可逆的。关于这个主题有论文;我不确定是否有相应图表的简单表征。一种实用的方法是在代码中捕获 LinAlgError 异常(尝试...例外...),并在邻接矩阵不可逆时发出警告(否则继续执行计算)。

Adjacency matrices are not always invertible. There are papers on this subject; I'm not sure whether there is any simple characterization of the corresponding graphs. A pragmatic approach would be to catch the LinAlgError exception in your code (try… except…), and warn when the adjacency matrix is not invertible (and keep performing your calculations otherwise).

不及他 2024-08-20 21:32:31

我不知道networkx是如何产生邻接矩阵的,但绝对没有理由让它是可逆的。例如,考虑完整的图(所有节点都相互连接),其邻接矩阵充满了 1,并且该矩阵显然具有 0 作为特征值(当然,只要节点数 >= 2 ...)。或者有 N 个节点且没有边的图,其邻接矩阵为 0...

你想做什么?我从来不需要考虑邻接矩阵的逆矩阵,但对于某些(小)x 值,通常需要考虑 I - x A 的逆矩阵。它的逆是

(I - x A) ^(-1) = I + xA + x^2 A2 + ...

对于 x 的某个值是可逆的(事实上,我认为只要 |x| < max( |1/y| for y in A eigenvalues)...这是因为你考虑了这个数图中的路径,但在其中加入一些衰减,因此它是可求和的(Pagerank有人吗?)

I don't know exactly how networkx produces the adjacency matrix, but there is absolutely no reason for it to be inversible. For example, consider the complete graph (all nodes are connected to every each other), its adacency matrix is full of ones, and the matrix has obviously 0 as an eigenvalue (as soon as the number of nodes is >= 2 of course...). Or the graph with N Nodes and no edges, its adjacency matrix is 0...

What do you want to do ? I never had to consider the inverse of the adjacency matrix, but very often the inverse of I - x A for some (small) value of x. Its inverse is

(I - x A) ^(-1) = I + xA + x^2 A2 + ...

which is inversible for some value of x (in fact, as soon as |x| < max( |1/y| for y in eigenvalues of A) I think)... this is because you consider the number of paths in your graph, but putting some decay in it, so it is summable (Pagerank anyone ?)

最笨的告白 2024-08-20 21:32:31

您是否需要一种生成邻接矩阵非奇异的图的方法?您生成的图具有没有逆的邻接矩阵,这不是 networkx 或 numpy 的错误。

are you asking for a method to generate graphs whose adjacency matrices are non-singular? it is no fault of networkx's or numpy's that the graphs you generated have adjacency matrices that do not have inverses.

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