Add vertex attributes to a weighted igraph Graph in python -
i learning python-igraph, , having difficulty in handling graph divided components (which unconnected between them). when apply 1 of clustering algorithms on graph doesn't seem work properly, , need apply algorithms each subgraph (component) separately. in order maintain identification of vertices, add vertex attribute give me id number in original graph. graph constructed weighted adjacency matrix:
import numpy np import igraph def symmetrize(a): return + a.t - 2*np.diag(a.diagonal()) = symmetrize(np.random.random((100,100))) g = igraph.graph.adjacency(a.tolist(),attr="weight",mode="upper")
i see there should way add vertex attributes, don't understand how use it..
adding vertex attribute of vertices works this:
g.vs["attr"] = ["id1", "id2", "id3", ...]
you can attach vertex attribute single vertex:
g.vs[2]["attr"] = "id3"
for instance, if need unique identifier vertices, can this:
g.vs["original_id"] = list(range(g.vcount()))
(you don't need list()
part if on python 2.x range()
produces list).
Comments
Post a Comment