繁体   English   中英

Python 3:来自数据框顶点的 iGraph 缺失

[英]Python 3: iGraph from Data Frame verticies missing

我在通过 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 正确读取数据帧的能力有关。

当我尝试单独使用边缘运行它时,它运行得很好,我能够 plot 如下所示: 在此处输入图像描述

但是,我需要顶点属性来计算我正在分析的网络的 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() 行改回默认值,等待进一步的帮助。

感谢您的询问,我添加了 function 并且有点错误,所以我在两天前左右修复了它。 请从 master 分支尝试 python-igraph。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM