Reputation: 543
I have performed some clustering in Python.
For example, I have an igraph graph object:
# Generate a graph with 100 nodes and 250 edges.
import igraph
g = igraph.Graph.Erdos_Renyi(n=100,m=250)
I then apply my clustering algorithm. In this case, for example purposes, I just randomly assign each node to cluster 0-3.
# Make a random partition of 4 clusters.
from random import randint
partition = [randint(0,3) for x in range(100)]
I can generate an igraph clustering object:
# Cluster the graph.
clusters = igraph.VertexClustering(g,membership = partition)
This enables me to access all the useful methods of this class, for example:
clusters.modularity
But, I'd like to update the original graph, g with the results of my clustering.
# Checks.
clusters.membership == partition # True
clusters.graph.clusters().membership == partition # False - I want this to be True.
Is there any way I can do this?
Upvotes: 0
Views: 269
Reputation: 817
The Graph.clusters()
method returns a new VertexClustering
object each time it is called, the clustering is never stored as part of the graph.
If you really need to store the clustering in the graph object, you can always use setattr
.
>>> setattr(g, "clustering", clusters)
>>> clusters.graph.clustering.membership == partition
True
If you pickle the object, the clusters will be retained.
>>> import pickle
>>> pickle.dump(g, open("graph.pickle", "wb"))
>>> ng = pickle.load(open("graph.pickle", "rb"))
>>> ng.clustering.membership == partition
True
Upvotes: 1