Convert VertexCluster to 2d Numpy array

It might be most useful to convert the results first in an easy to use representation. The following code transforms the membership into a dict-of-dicts, representing the membership of each node in each slice:

# Get results for each slice separately
membership_slice = {}
for v, m in zip(G.vs, partitions[0].membership):
  if v['slice'] not in membership_slice:
    membership_slice[v['slice']] = {}
  membership_slice[v['slice']][v['name']] = m

This produces something like

{0: {'A': 1, 'B': 1, 'D': 0, 'C': 1, 'E': 0, 'G': 0, 'F': 0},
 1: {'A': 1, 'B': 1, 'D': 0, 'C': 0, 'E': 0, 'G': 0, 'F': 0},
 2: {'B': 0, 'D': 0, 'C': 0, 'E': 2, 'G': 2, 'F': 2}}

Note that indeed the second slice does not have a node A. This is something that is expected, graphs across different slices do not all necessarily need to have all nodes present. It is then most convenient to convert it to a pandas dataframe, using

membership_df = pd.DataFrame(membership_slice)

We now have a pandas DataFrame that looks like

   0  1    2
A  1  1  NaN
B  1  1  0.0
D  0  0  0.0
C  1  0  0.0
E  0  0  2.0
G  0  0  2.0
F  0  0  2.0

Here, node A for slice 2 is missing, indicated by NaN (not a number). Notice that the membership for slice 2 is now a float instead of an int, because an int cannot represent NaN.

If you really need a numpy representation of this, you can simply get that by using membership_df.values.