用networkx可视化图结构

今天用networkx可视化图结构可视化了一下图结构记录一下:

import os
import os.path as osp
from torch_geometric.datasets import Planetoid
import torch_geometric.transforms as T

dataset = 'Cora'
path = osp.join(osp.dirname(osp.realpath('__file__')), 'data', dataset)

##加载数据集
dataset = Planetoid(path, dataset, T.NormalizeFeatures())
data = dataset[0]

import numpy as np
print(data.edge_index.shape)
print(data.edge_index)

print(type(data.edge_index))
#将data.edge_index转化为邻接矩阵
W = np.zeros((2708,2708))
print(data.edge_index[0].shape)
W[data.edge_index[0],data.edge_index[1]]=1

#画Cora图结构
import matplotlib.pyplot as plt
import networkx as nx
plt.figure(figsize=(20,20))
graph = nx.from_numpy_matrix(W)
nx.draw(graph,with_labels=True,font_color='#000000',node_color = 'r',font_size =8,node_size =20)
plt.show()

运行结果:

用networkx可视化图结构_第1张图片

你可能感兴趣的:(GSP,可视化,深度学习,python)