Python igraph:从图中删除顶点 [英] Python igraph: delete vertices from a graph

查看:116
本文介绍了Python igraph:从图中删除顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用enron电子邮件数据集,并且试图删除没有"@ enron.com"的电子邮件地址(即,我只希望拥有enron电子邮件).当我尝试删除不带@ enron.com的那些地址时,出于某些原因,只是跳过了一些电子邮件.下面显示了一个小图,其中顶点是电子邮件地址.这是gml格式:

I am working with enron email dataset and I am trying to remove email addresses that don't have "@enron.com" (i.e. I would like to have enron emails only). When I tried to delete those addresses without @enron.com, some emails just got skipped for some reasons. A small graph is shown below where vertices are email address. This is gml format:

Creator "igraph version 0.7 Sun Mar 29 20:15:45 2015"
Version 1
graph
[
  directed 1
  node
  [
    id 0
    label "csutter@enron.com"
  ]
  node
  [
    id 1
    label "steve_williams@eogresources.com"
  ]
  node
  [
    id 2
    label "kutner.stephen@enron.com"
  ]
  node
  [
    id 3
    label "igsinc@ix.netcom"
  ]
  node
  [
    id 4
    label "dbn@felesky.com"
  ]
  node
  [
    id 5
    label "cheryltd@tbardranch.com"
  ]
  node
  [
    id 6
    label "slover.eric@enron.com"
  ]
  node
  [
    id 7
    label "alkeister@yahoo.com"
  ]
  node
  [
    id 8
    label "econnors@mail.mainland.cc.tx.us"
  ]
  node
  [
    id 9
    label "jafry@hotmail.com"
  ]
  edge
  [
    source 5
    target 5
    weight 1
  ]
]

我的代码是:

G = ig.read("enron_email_filtered.gml")
for v in G.vs:
    print v['label']
    if '@enron.com' not in v['label']:
        G.delete_vertices(v.index)
        print 'Deleted'

在此数据集中,应删除7封电子邮件.但是,根据上述代码,仅删除了5封电子邮件.

In this dataset, 7 emails should be deleted. However, based on the above code, only 5 emails are removed.

推荐答案

从教程此处,您可以访问具有特定属性的所有顶点,然后按如下所示批量删除它们:

From the tutorial here, you can access all the vertices with a specific property, and then delete them in bulk as follows:

to_delete_ids = [v.index for v in G.vs if '@enron.com' not in v['label']]
G.delete_vertices(to_delete_ids)

这是我得到的输出:

to delete ids: [1, 3, 4, 5, 7, 8, 9]
Before deletion: IGRAPH D-W- 10 1 --
+ attr: id (v), label (v), weight (e)
+ edges:
5->5
After deletion: IGRAPH D-W- 3 0 --
+ attr: id (v), label (v), weight (e)
label: csutter@enron.com
label: kutner.stephen@enron.com
label: slover.eric@enron.com

这篇关于Python igraph:从图中删除顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆