使用networkx
生成随机图表。
我的流程如下:
从初始图G中,我们说:
G = nx.watts_strogatz_graph(100, 2, 0)
我想生成40个其他图形,这样生成的图形包含的节点比原始图形少。
在每次迭代时,l设置要删除的节点数和节点索引。
我尝试了什么?
G = nx.watts_strogatz_graph(100,2,0)#是原始图形,从中生成其他图形
number_examples = 40
for i in np.arange(examples):
F = randint(1, 5) # to get the number of nodes to remove for the example i
nodes_remove = sample(range(0, len(G.nodes())), F)# return the indexes of nodes to be removed
L=G # L will be the new graph, G is the initial graph
L.remove_nodes_from(nodes_remove)
number_nodes.append(len(L.nodes()))
print('number of nodes in the new graph L ', len(L.nodes())
print('number of nodes in the initial graph G ', len(G.nodes())
错误的输出?
是L的输出是正确的,因为l删除节点。但是G应该保持相同len(G.nodes())=100
,因为我设置L=G
,我只在 L
但是每次l删除L,G中的节点都要做同样的事情。
预期输出?
原始图表 G = 100个节点
l生成让我们说原始图表中的五个图 L1,L2,L3,L4,L5 G :
L1= 97 nodes
L2= 98 nodes
L3= 95 nodes
L4= 99 nodes
L5=97 nodes
这些是预期的输出
但是我得到:
L1= 97 nodes
L2= 95 nodes
L3= 90 nodes
L4= 89 nodes
L5=86 nodes
这意味着在迭代时我们从生成的新图中删除节点。
感谢您的帮助
答案 0 :(得分:1)
我在documentation中找到答案:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
model_prediction_i = np.zeros((200,200))
model_prediction_i[160][160] = 1
model_prediction_i[160][161] = 1
model_prediction_i[160][162] = 1
model_prediction_i[160][163] = 1
model_prediction_i[160][164] = 1
model_prediction_i[160][165] = 1
model_prediction_i[160][166] = 1
model_prediction_i[160][167] = 1
model_prediction_i[160][168] = 1
model_prediction_i[160][169] = 1
model_prediction_i[160][170] = 1
model_prediction_i[160][171] = 1
plt.imsave('outfile.jpg', model_prediction_i, cmap='hot')
rotated_img = Image.open('outfile.jpg')
background = Image.open('reference.jpg')
X_test_i = np.zeros((5, 2))
X_test_i[0] = [10 ,10]
X_test_i[1] = [60 ,60]
X_test_i[2] = [90 ,90]
X_test_i[3] = [140 ,140]
X_test_i[4] = [250 ,230]
fig, ax = plt.subplots(figsize=(10, 10))
ax.plot(X_test_i[:, 0], X_test_i[:, 1], marker='o', markersize=7, color="red", label='Current position', antialiased=True)
ax.imshow(rotated_img, cmap='hot', extent=[X_test_i[:, 0][-1]-10, X_test_i[:, 0][-1]+10, X_test_i[:, 1][-1]-10, X_test_i[:, 1][-1]+10])
ax.imshow(background)
plt.show()
这将生成图表的完整副本,包括所有节点或边缘属性。