NetworkX Network Analysis Errors

Asked 2 years ago, Updated 2 years ago, 164 views

Network with NetworkX's Kernighan-lin algorithm I tried to split the data, but I failed with the following error.
I would appreciate your advice.

IndexError Traceback (most recent call last) in () 17 for init_partition: 18 for n inc:---> 19 color_map_i[n]=colors[counter] 20 counter=counter+121

IndexError:list assignment index out of range

The code is as follows:

G=nx.read_edgelist("200224_04_act.prn", nodetype=int)

colors=["red", "blue", "green" ]
pos=nx.spring_layout(G)


init_nodes=np.array_split(G.nodes(), 2)
init_partition=[set(init_nodes[0]), set(init_nodes[1])]
print(init_partition)


from networkx.algorithms.community import kernighan_lin_bisection

color_map_i=["black"]*nx.number_of_nodes(G)
print(color_map_i)
counter = 0
Forcin init_partition:
    for ninc:
        color_map_i[n] = colors[counter]
    counter=counter+1

print(color_map_i)


nx.draw_networkx_edges(G,pos)
nx.draw_networkx_nodes(G,pos,node_color=color_map_i)
nx.draw_networkx_labels(G,pos)

plt.axis("off")
plt.show()

lst_b = kernighan_lin_bisection (G, partition = init_partition)
color_map_b=["black"]*nx.number_of_nodes(G)

counter = 0
for cinst_b:
    for ninc:
        color_map_b[n]=colors[counter]
    counter=counter+1

nx.draw_networkx_edges(G,pos)
nx.draw_networkx_nodes(G,pos,node_color=color_map_b)
nx.draw_networkx_labels(G,pos)
plt.axis("off")
plt.show()

The original data "200224_04_act.prn" is as follows, but the number is smaller than the actual number due to the limitation of the number of characters in this post.(Actually around 2000 nodes)

1415
   2     415
   3     415
   3    1350
   4    1351
   5    1352
   6     383
   7     993
   8    1353
   9     887
  10     887
  11     887
  12     887
  13     887
  14    1185
  15    1185
  16    1185
  17    1185
  18    1185
  19    1146
  20    1146
  21    1146
  22    1146
  21     776
  23     776
  24     707
  25     707
  26     707
  27     707
  28     707
  29     754
  21     754
  30     754
  31     754
  32     754
  33     778
  34     778
  35     778
  36     778
  37     778
  38     859
  39     859
  40    1354
  41     563
  42     563
  43     563
  44     563
  45     563
  46    1209
  47    1209
  48    1209
  49    1209
  50    1209
  51     715
  52     715
  53     715
  54     715
  55     715
  56    1048
  57    1048
  58    1047
  59    1047
  60    1047

python networkx

2022-09-29 21:59

1 Answers

There is a problem with this part.

 counter=0
Forcin init_partition:
    for ninc:
        color_map_i[n] = colors[counter]
    counter=counter+1

n contains the name of the vertex, but as far as 200224_04_act.prn is concerned, this seems to start with 1.However, the Python array starts with zero, so writing color_map_i[n] deviates by one.Therefore, if the vertex name of the largest number is in n, color_map_i[n] does not exist and leads to index out of range error.

The solution is:

  • 200224_04_act.prn starts with 0 or
  • color_map_i[n-1] shift one at every point, or
  • color_map_i length to nx.number_of_nodes(G)+1 so that subscript and vertex numbers correspond to each other

is possible.

Supplement: By the way, the counter doesn't have to be incremented by itself, and if you write for counter,cin enumerate(init_partition): you can add subscripts without permission.


2022-09-29 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.