《Network Science with Python and NetworkX Quick Start Guide》导读(Chapter 4)

Code:[ZachxPKU/NetworkSciencewithPythonandNetworkXQuickStartGuide]

Chapter 4 Affiliation Networks

Nodes and affliations

Affiliation networks in NetworkX

import networkx as nx
from networkx.algorithms import bipartite
from networkx import NetworkXError

G = nx.karate_club_graph()
try:
    left, right = bipartite.sets(G)
    print("Left nodes\n", left)
    print("\nRight nodes\n", right)
except NetworkXError as e:
    print(e)
    ```
```python
B = nx.Graph()
B.add_edges_from([(v, (v, w)) for v, w in G.edges])
B.add_edges_from([(w, (v, w)) for v, w in G.edges])
try:
    left, right = bipartite.sets(B)
    print("Left nodes\n", left)
    print("\nRight nodes\n", right)
except NetworkXError as e:
    print(e)
    ```
```python
bipartite.is_bipartite(B)
import networkx as nx
from pathlib import Path
data_dir = Path('..') / 'data'
B = nx.Graph()
with open(data_dir / 'bartomeus2008' / 'Bartomeus_Ntw_nceas.txt') as f:
    next(f)
    for row in f:
        cells = row.strip().split('\t')
        plant = cells[4].replace('_', '\n')
        pollinator = cells[8].replace('_', '\n')
        B.add_edge(pollinator, plant)
        B.nodes[pollinator]["bipartite"] = 0
        B.nodes[plant]['bipartite'] = 1
B = B.subgraph(list(nx.connected_components(B))[0])
pollinators = [v for v in B.nodes if B.nodes[v]['bipartite'] == 0]
plants = [v for v in B.nodes if B.nodes[v]['bipartite'] == 1]
import matplotlib.pyplot as plt
plt.figure(figsize=(30,30))
pos = nx.spring_layout(B, k=0.9)
nx.draw_networkx_edges(B, pos, width=3, alpha=0.2)
nx.draw_networkx_nodes(B, pos, nodelist=plants, node_color="#bfbf7f", node_shape="h", node_size=3000)
nx.draw_networkx_nodes(B, pos, nodelist=pollinators, node_color="#9f9fff", node_size=3000)
nx.draw_networkx_labels(B, pos)

Projections

G = bipartite.projected_graph(B, plants)
plt.figure(figsize=(24, 24))
pos = nx.spring_layout(G, k=0.5)
nx.draw_networkx_edges(G, pos, width=3, alpha=0.2)
nx.draw_networkx_nodes(G, pos, node_color="#bfbf7f", node_shape="h", node_size=10000)
nx.draw_networkx_labels(G, pos)
G = bipartite.projected_graph(B, pollinators)
plt.figure(figsize=(30, 30))
pos = nx.spring_layout(G, k=0.5)
nx.draw_networkx_edges(G, pos, width=3, alpha=0.2)
nx.draw_networkx_nodes(G, pos, node_color="#9f9fff", node_size=6000)
nx.draw_networkx_labels(G, pos)
G = bipartite.weighted_projected_graph(B, plants)
list(G.edges(data=True))[0]
G = bipartite.overlap_weighted_projected_graph(B, pollinators)
weight = [G.edges[e]['weight'] for e in G.edges]
plt.figure(figsize=(30,30))
pos = nx.spring_layout(G, weight='weight', k=0.5)
nx.draw_networkx_edges(G, pos, edge_color=weight, edge_cmap=plt.cm.Blues,
                      width=6, alpha=0.5)
nx.draw_networkx_nodes(G, pos, node_color="#9f9fff", node_size=6000)
nx.draw_networkx_labels(G,pos)

Summary

References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值