【问题标题】:Python 3: iGraph from Data Frame verticies missingPython 3:来自数据框顶点的 iGraph 缺失
【发布时间】:2021-03-19 16:25:53
【问题描述】:

我在通过 pandas 数据框导入一些数据并在 iGraph 中使用它时遇到了一些问题。我首先尝试使用我的数据,但由于它不起作用,我创建了一个较小的数据集来重现该问题。我用excel做测试数据,然后用pandas.read_clipboard(逐行运行)分析。

至于我正在运行的设置,我使用的是 Spyder 4.1.5、Python 3.8、iGraph 0.8.3 和 Pandas 1.1.3。

数据显示在以下两个块中。

顶点文件:

1    1
2    1
3    1
4    1

边缘文件:

1    2    0.15
1    3    0.15
2    3    0.3
2    4    0.45
3    4    0.15

然后python代码是这样的

from igraph import Graph
from pandas import read_table, read_clipboard
from cairo import *

#copy data from vertices file and run line once
test_nodes = read_clipboard(header = None, names = ["NodeID" , "Attribute"])

#copy data from edges file and run line once
test_edges = read_clipboard(header = None, names = ["From", "To", "Length"])    


G2 = Graph.DataFrame(edges = test_edges, directed = False, vertices = test_nodes)

这会导致以下错误

[Folder location]\anaconda3\lib\site-packages\numpy\lib\arraysetops.py:576: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  mask &= (ar1 != a)
Traceback (most recent call last):

  File "<ipython-input-95-1b701f504cfc>", line 1, in <module>
    G2 = Graph.DataFrame(edges = test_edges, directed = False, vertices = test_nodes)

  File "[Folder Location]\anaconda3\lib\site-packages\igraph\__init__.py", line 3124, in DataFrame
    raise ValueError(

ValueError: Some vertices in the edge DataFrame are missing from vertices DataFrame

如果我再次尝试使用 .astype(Float),它不再具有包含 FutureWarning 的行,但 ValueError 仍然存在,这让我相信这与 iGraph 正确读取数据帧的能力有关。

当我尝试单独使用边缘运行它时,它运行得很好,我可以像这里看到的那样进行绘图:

但是,我需要顶点属性来计算我正在分析的网络的 PC。

提前感谢您提供的任何帮助!

编辑:所以我在查看 iGraph 代码时注意到,以下内容用于比较顶点与边

    names_vertices = vertices.iloc[:, 0].astype(str)
    names_edges = np.unique(edges.values[:, :2])
    if len(np.setdiff1d(names_edges, names_vertices)):
                raise ValueError(
                    'Some vertices in the edge DataFrame are missing from vertices DataFrame')

但是,这并没有多大意义,因为 names_edges 是数字,而 names_vertices 是字符串。因此,我尝试编辑此代码以将具有 len() 的行更改为

    if len(np.setdiff1d(names_edges, names_vertices)) != len(names_vertices):

但现在我收到一个新错误:

raise KeyError(f"None of [{key}] are in the [{axis_name}]")

KeyError: "None of [Float64Index([1.0, 1.0, 2.0, 2.0, 3.0], dtype='float64')] are in the [index]"

我将 len() 行改回默认值,等待进一步的帮助。

【问题讨论】:

    标签: python python-3.x pandas igraph


    【解决方案1】:

    谢谢你的询问,我添加了这个功能,它有点bug,所以我在两天前左右修复了它。请从 master 分支尝试 python-igraph。

    即使在您的版本中,顶点名称的概念也是字符串。将前两列边和第一列顶点转换为字符串,甚至错误版本也可以工作。

    【讨论】:

    • 您好,感谢您的回复!这是个好消息。我对 python 还是有点新,我想知道你从 master 分支尝试具体是什么意思?
    • 这不是 Python,而是 git:github.com/igraph/python-igraph。如果您不知道如何使用 git,我建议您将列转换为上述字符串,然后等待下一个版本。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2015-09-08
    • 2016-04-17
    • 2016-10-17
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多