使用CSV文件作为共现矩阵使用neworkx绘制图形

问题描述

我使用sklearn CountVectorizer创建了一个共现矩阵,并将其保存为csv文件。假设它看起来像这样:

  Unnamed: 0  a  b  c  d
0          a  0  1  0  0
1          b  2  0  1  0
2          c  0  1  0  3
3          d  0  0  1  0

以该数据帧作为共现矩阵来绘制共现网络的最简单方法是什么?

解决方法

正如@ALollz在评论中提到的那样,您可以使用G=nx.from_pandas_adjacency(df)从熊猫数据框中创建图形,然后使用pyvis.network对其进行可视化,如下所示:

import pandas as pd
import numpy as np
import networkx as nx
from pyvis.network import Network

# creating a dummy adjacency matrix of shape 20x20 with random values of 0 to 3
adj_mat = np.random.randint(0,3,size=(20,20))
np.fill_diagonal(adj_mat,0)  # setting the diagonal values as 0
df = pd.DataFrame(adj_mat)

# create a graph from your dataframe
G = nx.from_pandas_adjacency(df)

# visualize it with pyvis
N = Network(height='100%',width='100%',bgcolor='#222222',font_color='white')
N.barnes_hut()
for n in G.nodes:
    N.add_node(int(n))
for e in G.edges:
    N.add_edge(int(e[0]),int(e[1]))

N.write_html('./coocc-graph.html')

enter image description here