Reputation: 27
I am using networkx package to analyse IMDb data to compute centrality(closeness and betweenness). Problem is, the graph has two types of nodes - namely, actors and movies. I want to calculate the centrality with respect to only the actors and not the graph overall.
The code -
T = nx.Graph()
T.add_nodes_from(demo_df.primaryName,bipartite=1)
T.add_nodes_from(demo_df.primaryTitle,bipartite=0)
T = nx.from_pandas_edgelist(demo_df,'primaryName','primaryTitle')
nx.closeness_centrality(T)
nx.betweenness_centrality(T)
I don't want it to calculate/display the betweenness and closeness of the movies(Wings of Desire, Dopey Dicks, Studio Stoops). I want it to be calculated only for the actors.
Upvotes: 0
Views: 336
Reputation: 88246
For bipartite graphs, you have the networkx.algorithms.bipartite.centrality
counterpart. For instance for the closeness_centrality
the result will be a dictionary keyed by node with bipartite degree centrality as the value. In the nodes
argument specify the nodes in one bipartite node set:
from networkx.algorithms import bipartite
part0_nodes, part1_nodes = bipartite.sets(T)
cs_partition0 = bipartite.centrality.closeness_centrality(T, part0_nodes)
For disconnected graphs, you may try obtaining the nodes from a given partition with:
partition = nx.get_node_attributes(T, 'bipartite')
part0_nodes = [node for node, p in partition.items() if p==0]
Note that the returned dictionary will still contain all nodes even though you've specified the nodes from one partition in nodes
. So you can just keep those in just one set using part0_nodes
. This is mentioned in the notes
section:
The nodes input parameter must contain all nodes in one bipartite node set, but the dictionary returned contains all nodes from both bipartite node sets. See :mod:
bipartite documentation <networkx.algorithms.bipartite>
for further details on how bipartite graphs are handled in NetworkX.
Upvotes: 1