EasyGraph快速入门

简介:

EasyGraph是复旦大学网络大数据实验室开发的一款基于Python语言的网络分析开源工具箱。它是第一个包含较为全面的结构洞占据者检测方法的开源库,同时覆盖了网络嵌入和其他一些传统的网络分析方法。EasyGraph支持多种网络数据类型,具有良好的兼容性。此外,它利用混合编程和并行计算提高了大多数经典网络分析算法的运行效率。

安装:

要求:python版本3.9-3.10
1.使用pip安装:

$ pip install --upgrade Python-EasyGraph

2.如果预构建的 EasyGraph wheels不支持您的平台(操作系统 / CPU 架构,请在此处检查),您可以按以下方式在本地构建:

git clone https://github.com/easy-graph/Easy-Graph && cd Easy-Graph && git checkout pybind11
pip install pybind11
python3 setup.py build_ext
python3 setup.py install

注意:conda包已经不再更新或维护

图的基本属性和操作:

引用EasyGraph并创建一个无向图G:

import easygraph as eg
G=eg.Graph()

为图添加边(1,2)

G.add_edge(1,2)#Add a single edge
G.edges
[(1, 2, {})]

为图添加几条边

G.add_edges([(2, 3), (1, 3), (3, 4), (4, 5)])#Add edges
G.edges
[(1, 2, {}), (1, 3, {}), (2, 3, {}), (3, 4, {}), (4, 5, {})]

添加带属性的节点

G.add_node('hello world')
G.add_node('Jack', node_attr={
    'age': 10,
    'gender': 'M'
})
G.nodes
{1: {}, 2: {}, 3: {}, 4: {}, 5: {},
'hello world': {},
'Jack': {'node_attr':
            {'age': 10,
            'gender': 'M'}
        }
}

删除节点

G.remove_nodes(['hello world','Tom','Lily','a','b'])#remove edges
G.nodes
{1: {}, 2: {}, 3: {}, 4: {}, 5: {}}

删除边

G.remove_edge(4,5)
G.edges
[(1, 2, {}), (1, 3, {}), (2, 3, {}), (3, 4, {})]

高级python属性

print(len(G))#__len__(self)
5
for x in G:#__iter__(self)
    print(x)
1
2
3
4
5
print(G[1])# return list(self._adj[node].keys()) __contains__ __getitem__
{2: {}, 3: {}}

节点2的邻居

for neighbor in G.neighbors(node=2):
    print(neighbor)

1
3

添加带权边

G.add_edges([(1,2), (2, 3),(1, 3), (3, 4), (4, 5)], edges_attr=[
    {
        'weight': 20
    },
    {
        'weight': 10
    },
    {
        'weight': 15
    },
    {
        'weight': 8
    },
    {
        'weight': 12
    }
])#add weighted edges
G.add_node(6)
G.edges
[(1, 2, {'weight': 20}), (1, 3, {'weight': 15}), (2, 3, {'weight': 10}), (3, 4, {'weight': 8}), (4, 5, {'weight': 12})]
G.nodes
{1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}}
G.adj
{1: {2: {'weight': 20}, 3: {'weight': 15}}, 2: {1: {'weight': 20}, 3: {'weight': 10}}, 3: {2: {'weight': 10}, 1: {'weight': 15}, 4: {'weight': 8}}, 4: {3: {'weight': 8}, 5: {'weight': 12}}, 5: {4: {'weight': 12}}, 6: {}}

度数和带权度数

G.degree()
{1: 35, 2: 30, 3: 33, 4: 20, 5: 12, 6: 0}
G.degree(weight='weight')
{1: 35, 2: 30, 3: 33, 4: 20, 5: 12, 6: 0}

交换每个节点的值和索引

G_index_graph, index_of_node, node_of_index = G.to_index_node_graph()
G_index_graph.adj
{0: {1: {'weight': 20}, 2: {'weight': 15}}, 1: {0: {'weight': 20}, 2: {'weight': 10}}, 2: {0: {'weight': 15}, 1: {'weight': 10}, 3: {'weight': 8}}, 3: {2: {'weight': 8}, 4: {'weight': 12}}, 4: {3: {'weight': 12}}, 5: {}}
index_of_node
{1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5}
node_of_index
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}

图的深度复制

G1 = G.copy()
G1.adj
{1: {2: {'weight': 20}, 3: {'weight': 15}}, 2: {1: {'weight': 20}, 3: {'weight': 10}}, 3: {1: {'weight': 15}, 2: {'weight': 10}, 4: {'weight': 8}}, 4: {3: {'weight': 8}, 5: {'weight': 12}}, 5: {4: {'weight': 12}}, 6: {}}

给定节点的子图

G_sub = G.nodes_subgraph(from_nodes = [1,2,3])
G_sub.adj
{1: {2: {'weight': 20}, 3: {'weight': 15}}, 2: {1: {'weight': 20}, 3: {'weight': 10}}, 3: {1: {'weight': 15}, 2: {'weight': 10}}}

给定节点的邻域图

ego_network = G.ego_subgraph(center=1)
ego_network.adj
{2: {1: {'weight': 20}, 3: {'weight': 10}}, 1: {2: {'weight': 20}, 3: {'weight': 15}}, 3: {2: {'weight': 10}, 1: {'weight': 15}}}

连通分量

eg.number_connected_components(G)
2
eg.connected_components(G)
[{6}, {1, 2, 3, 4, 5}]
eg.connected_component_of_node(G, node=3)
{1, 2, 3, 4, 5}

结构洞占据者检测

使用 MaxD 进行结构洞占据者检测

M=eg.get_structural_holes_MaxD(G,
                          k = 5, # To find top five structural holes spanners.
                          C = [frozenset([1,2,3]), frozenset([4,5,6])] 
                         )
M
[3, 1, 2, 4, 5]

使用 HAM 进行结构洞占据者检测

top_k_nodes, SH_score, cmnt_labels = eg.get_structural_holes_HAM(G,
                        k=2,
                        c=2,
                        ground_truth_labels=[[0], [0], [1], [1], [1]]
                    )
AMI
HAM: 1.0
HAM_all: 0.25126693574443504
NMI
HAM: 1.0
HAM_all: 0.43253806776631243
Entropy
HAM: 0.0
HAM_all: 0.38190850097688767
top_k_nodes
[4, 3]
SH_score
{1: 2, 2: 1, 3: 3, 4: 4, 5: 0}
cmnt_labels
{1: 2, 2: 2, 3: 2, 4: 1, 5: 1}

使用 Common Greedy 进行结构洞占据者检测

T = eg.common_greedy(G,
          k=3,
          c=1.0,
          weight='weight')
T
[3, 5, 2]

从 Karate Club 数据集获取示例图

G=eg.datasets.get_graph_karateclub()

节点 3 的中介中心性

eg.ego_betweenness(G,3)
6.5

所有节点的有效大小

eg.effective_size(G)
{1: 11.75, 2: 4.333333333333333, 3: 5.8, 4: 0.666666666666667, 5: -0.3333333333333335, 6: 0.5, 7: 0.5, 8: -1.0, 9: 1.0, 10: 0.0, 11: -0.3333333333333335, 12: -1.0, 13: -1.0, 14: 0.5999999999999996, 15: -1.0, 16: -1.0, 17: -1.0, 18: -1.0, 19: -1.0, 20: 0.3333333333333335, 21: -1.0, 22: -1.0, 23: -1.0, 24: 1.4, 25: 0.3333333333333335, 26: 0.3333333333333335, 27: -1.0, 28: 1.5, 29: 0.3333333333333335, 30: 0.0, 31: 0.5, 32: 3.0, 33: 7.833333333333333, 34: 13.235294117647058}

所有节点的效率

eg.efficiency(G)
{1: 0.734375, 2: 0.48148148148148145, 3: 0.58, 4: 0.11111111111111116, 5: -0.11111111111111116, 6: 0.125, 7: 0.125, 8: -0.25, 9: 0.2, 10: 0.0, 11: -0.11111111111111116, 12: -1.0, 13: -0.5, 14: 0.11999999999999993, 15: -0.5, 16: -0.5, 17: -0.5, 18: -0.5, 19: -0.5, 20: 0.11111111111111116, 21: -0.5, 22: -0.5, 23: -0.5, 24: 0.27999999999999997, 25: 0.11111111111111116, 26: 0.11111111111111116, 27: -0.5, 28: 0.375, 29: 0.11111111111111116, 30: 0.0, 31: 0.125, 32: 0.5, 33: 0.6527777777777778, 34: 0.7785467128027681}

所有节点的约束

eg.constraint(G)
{1: 0.15542329764660495, 2: 0.27953510802469134, 3: 0.18517663966049389, 4: 0.39665964720507535, 5: 0.5294174382716048, 6: 0.4774848090277778, 7: 0.4774848090277778, 8: 0.4427115885416667, 9: 0.3036007136678201, 10: 0.5, 11: 0.5294174382716048, 12: 1.0, 13: 0.6225043402777779, 14: 0.32333541666666676, 15: 0.5736795943867743, 16: 0.5736795943867743, 17: 0.78125, 18: 0.590868537808642, 19: 0.5736795943867743, 20: 0.37371935013717417, 21: 0.5736795943867743, 22: 0.590868537808642, 23: 0.5736795943867743, 24: 0.30582372164552096, 25: 0.4598765432098765, 26: 0.4598765432098765, 27: 0.6709018166089966, 28: 0.2850692041522491, 29: 0.3869131530607885, 30: 0.44940900134563627, 31: 0.3460064638600538, 32: 0.24457540369088812, 33: 0.2492233622751933, 34: 0.15641868512110732}

所有节点的层次结构

eg.hierarchy(G)
{1: 0.08754463683694338, 2: 0.1544986992144599, 3: 0.04535921163684897, 4: 0.061067624090107915, 5: 0.07134469342227538, 6: 0.035305086439308436, 7: 0.03530508643930843, 8: 0.0011300905133206085, 9: 0.012305615918292673, 10: 0.0, 11: 0.07134469342227538, 13: 0.006282226820057121, 14: 0.01352163842686084, 15: 0.00037766424272729984, 16: 0.00037766424272729984, 17: 0.0, 18: 0.0014421896477064891, 19: 0.00037766424272729984, 20: 0.0033488184456886283, 21: 0.00037766424272729984, 22: 0.0014421896477064891, 23: 0.00037766424272729984, 24: 0.036897065903971515, 25: 0.024311482691998648, 26: 0.024311482691998648, 27: 0.01960343310353982, 28: 0.0086202479405721, 29: 0.007513545360870802, 30: 0.06689992156538088, 31: 0.01286931837997609, 32: 0.020491542893317758, 33: 0.3259402254099858, 34: 0.2416086531756689}

使用c++代码实现更好的表现

GraphC类提供了与Graph类拥有的大部分关键操作,如加点,加边等。
EasyGraph 还通过 C++ 实现了四个重要的网络分析函数:
1.多源迪杰斯特拉算法
2.介数中心性
3.紧密中心性
4.K-Core

使用:
对于类方法,调用和参数传递与 Python 相同。
对于模块函数,EasyGraph 将根据图的类选择特定的代码进行执行。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值