我已经计算了我的观察之间的 Jaccard 距离矩阵,并得到了一个对角线距离矩阵,当且仅当 Jaccard 距离大于 0.5
时,我如何生成一个图(netowrkx 图),其中两个节点(观察)之间有一条边例如,如果我有以下距离矩阵:
A, B, C
A 1 0.4 0.7
B 0.4 1 0.6
C 0.7 0.6 1
我将得到一个图表,其中 A 连接到 C 但不连接到 B ,B 连接到 C , C 连接到 A 和 B
查看 NetworkX 库的
from_numpy_array()
https://networkx.org/documentation/stable/reference/generated/networkx.convert_matrix.from_numpy_array.html
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
weights = np.array([[1, 0.4 , 0.7], [0.4, 1, 0.6], [0.7, 0.6, 1]])
G = nx.from_numpy_array(weights)
def draw_graph(G):
pos = nx.spring_layout(G)
default_axes = plt.axes(frameon=True)
nx.draw_networkx(G, pos = pos, node_size=1000, alpha=0.8, ax=default_axes)
edge_labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos = pos, edge_labels=edge_labels)
draw_graph(G)