数据集简介

Python 网络分析中级

Eric Ma

Data Carpentry instructor and author of nxviz package

数据集与案例介绍

  • 大学论坛发帖数据集,6个月
  • 节点分区:学生、论坛
  • 本章活动:
    • 从 pandas DataFrame 构建图
    • 计算二分图的单分投影
    • 可视化
    • 时间序列过滤与分析
  • 回顾已用函数
Python 网络分析中级

从 DataFrame 构建图

df
   customers    products
0  customerA    product1
1  customerB    product2
...
G = nx.Graph()

G.add_nodes_from(df['products'], bipartite='products') G.add_nodes_from(df['customers'], bipartite='customers')
list(G.nodes())
['product1', 'customerC', 'product2', 'customerB', 'customerA']
list(G.edges())
[]
Python 网络分析中级

从 DataFrame 构建图

G.add_edges_from(zip(df['customers'], df['products']))

list(G.edges())
[('product1', 'customerC'), ('product1', 'customerA'), 
    ('customerC', 'product2'), ('product2', 'customerB')]
Python 网络分析中级

二分图投影

cust_nodes = [n for n in G.nodes() if G.nodes[n]
                 ['bipartite'] == 'customers']

prod_nodes = [n for n in G.nodes() if G.nodes[n] ['bipartite'] == 'products']
prodG = nx.bipartite.projected_graph(G, nodes=prod_nodes) custG = nx.bipartite.projected_graph(G, nodes=cust_nodes)
list(prodG.nodes())
['product1', 'product2']
list(custG.nodes())
['customerC', 'customerB', 'customerA']
Python 网络分析中级

开始练习!

Python 网络分析中级

Preparing Video For Download...