gpt4 book ai didi

python - 如何在 Python 中 reshape networkx 图?

转载 作者:IT老高 更新时间:2023-10-28 22:22:55 25 4
gpt4 key购买 nike

所以我创建了一种非常幼稚(可能效率低下)的生成 HashMap 的方法。

问题:

我有 4 个维度... p q r s .

我想统一显示它(tesseract),但我不知道如何 reshape 它。 如何在 Python 中 reshape networkx 图?

我见过一些使用 spring_layout()draw_circular() 的示例,但它的形状并不符合我的要求,因为它们是不统一。

有没有办法 reshape 我的图表并使其统一?(即将我的哈斯图 reshape 为正方体形状(最好使用 nx.draw())

这是我目前的样子: enter image description here

这是我生成 N 维 HashMap 的代码

#!/usr/bin/python

import networkx as nx
import matplotlib.pyplot as plt
import itertools

H = nx.DiGraph()

axis_labels = ['p','q','r','s']

D_len_node = {}

#Iterate through axis labels
for i in xrange(0,len(axis_labels)+1):
#Create edge from empty set
if i == 0:
for ax in axis_labels:
H.add_edge('O',ax)
else:
#Create all non-overlapping combinations
combinations = [c for c in itertools.combinations(axis_labels,i)]
D_len_node[i] = combinations
#Create edge from len(i-1) to len(i) #eg. pq >>> pqr, pq >>> pqs
if i > 1:
for node in D_len_node[i]:
for p_node in D_len_node[i-1]:
#if set.intersection(set(p_node),set(node)): Oops
if all(p in node for p in p_node) == True: #should be this!
H.add_edge(''.join(p_node),''.join(node))

#Show Plot
nx.draw(H,with_labels = True,node_shape = 'o')
plt.show()

我想像这样 reshape 它: enter image description here

如果有人知道制作哈斯图的更简单方法,请分享一些智慧,但这不是本文的主要目的。

最佳答案

这是一个务实的,而不是纯粹的数学答案。

我认为您有两个问题 - 一个与布局有关,另一个与您的网络有关。

1。网络

您的网络中有太多边,无法代表单位 tesseract。 警告我不是这里的数学专家 - 只是从绘图角度(matplotlib 标签)得出这个结论。如果我错了,请解释一下。

您想要的投影,例如 wolfram mathworld n=4 的 Hasse 图页面只有 4 条边连接所有节点,而 2 位节点有 6 条边,3 位节点有 7 条边。您的图表完全连接每个“级别”,即具有 0 个 1 值的 4-D 向量连接到具有 1 个 1 值的所有向量,然后连接到具有 2 个 1 值等等。这在基于维基百科答案的投影中最为明显(下图第二张)

2。投影

我找不到预先编写的算法或库来自动将 4D tesseract 投影到 2D 平面上,但我确实找到了几个示例,e.g. Wikipedia .由此,您可以计算出适合您的坐标集并将其传递给 nx.draw() 调用。

这是一个例子 - 我已经包含了两个坐标集,一个看起来像上面显示的投影,一个匹配 this one from wikipedia .

import networkx as nx
import matplotlib.pyplot as plt
import itertools

H = nx.DiGraph()

axis_labels = ['p','q','r','s']

D_len_node = {}

#Iterate through axis labels
for i in xrange(0,len(axis_labels)+1):
#Create edge from empty set
if i == 0:
for ax in axis_labels:
H.add_edge('O',ax)
else:
#Create all non-overlapping combinations
combinations = [c for c in itertools.combinations(axis_labels,i)]
D_len_node[i] = combinations
#Create edge from len(i-1) to len(i) #eg. pq >>> pqr, pq >>> pqs
if i > 1:
for node in D_len_node[i]:
for p_node in D_len_node[i-1]:
if set.intersection(set(p_node),set(node)):
H.add_edge(''.join(p_node),''.join(node))

#This is manual two options to project tesseract onto 2D plane
# - many projections are available!!
wikipedia_projection_coords = [(0.5,0),(0.85,0.25),(0.625,0.25),(0.375,0.25),
(0.15,0.25),(1,0.5),(0.8,0.5),(0.6,0.5),
(0.4,0.5),(0.2,0.5),(0,0.5),(0.85,0.75),
(0.625,0.75),(0.375,0.75),(0.15,0.75),(0.5,1)]

#Build the "two cubes" type example projection co-ordinates
half_coords = [(0,0.15),(0,0.6),(0.3,0.15),(0.15,0),
(0.55,0.6),(0.3,0.6),(0.15,0.4),(0.55,1)]
#make the coords symmetric
example_projection_coords = half_coords + [(1-x,1-y) for (x,y) in half_coords][::-1]

print example_projection_coords


def powerset(s):
ch = itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))
return [''.join(t) for t in ch]

pos={}
for i,label in enumerate(powerset(axis_labels)):
if label == '':
label = 'O'
pos[label]= example_projection_coords[i]

#Show Plot
nx.draw(H,pos,with_labels = True,node_shape = 'o')
plt.show()

注意 - 除非您更改我在上面 1. 中提到的内容,否则它们仍然具有您的边缘结构,因此看起来与网络上的示例不完全相同。这是您现有的网络生成代码的样子 - 如果您将它与您的示例进行比较,您可以看到额外的边缘(例如,我不认为这个 pr 应该连接到 pqs:

'两个立方体'投影

Projection of tesseract generated by code

维基媒体示例投影

Alternative projection of tesseract generated by code provided


注意

如果您想进行自己的预测(并以数学方式构建 pos)的数学,您可以查看 this research paper .


编辑:

好奇心战胜了我,我不得不寻找一种数学方法来做到这一点。我找到了 this blog - 主要结果是投影矩阵:

4D to 2D projection matrix

这导致我开发了这个函数来投影每个标签,将包含“p”的标签表示该点在“p”轴上的值为 1,即我们正在处理单位 tesseract。因此:

def construct_projection(label):
r1 = r2 = 0.5
theta = math.pi / 6
phi = math.pi / 3
x = int( 'p' in label) + r1 * math.cos(theta) * int('r' in label) - r2 * math.cos(phi) * int('s' in label)
y = int( 'q' in label) + r1 * math.sin(theta) * int('r' in label) + r2 * math.sin(phi) * int('s' in label)
return (x,y)

可以很好地投影到所有点都不同的常规二维八边形中。

这将在上面的程序中运行,只需替换

 pos[label] = example_projection_coords[i]

pos[label] = construct_projection(label)

这给出了结果:

projection onto an octagon

尽情地玩 r1r2thetaphi :)

关于python - 如何在 Python 中 reshape networkx 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com