0
votes

I am trying to generate a Stochastic Block Model graph using the function in networkx "stochastic_block_model" documented in this page: https://networkx.github.io/documentation/stable/reference/generated/networkx.generators.community.stochastic_block_model.html

My networkx package is updated to the 2.2 release but I keep receiving the error: module 'networkx' has no attribute 'stochastic_block_model'. How I can solve this problem?

import networkx as nx


sizes = [75, 75, 300]
probs = [[0.25, 0.05, 0.02],
        [0.05, 0.35, 0.07],
        [0.02, 0.07, 0.40]]
g = nx.stochastic_block_model(sizes, probs, seed=0)
len(g)

H = nx.quotient_graph(g, g.graph['partition'], relabel=True)
for v in H.nodes(data=True):
    print(round(v[1]['density'], 3))




for v in H.edges(data=True):
    print(round(1.0 * v[2]['weight'] / (sizes[v[0]] * sizes[v[1]]), 3))
2
Can you show us your code and some things you've tried? - Jonathan Rys
PLease provide your code, including the import statements you are using. Since this is coming form a submodule, you may have to call it from networkx.generators.community. - G. Anderson
I edited my question by adding the code. I am just doing the given example in the documentation - Ghadir Ayache
I tried 'nx.generators.community.stochastic_block_model' but I still receiving the error saying: module 'networkx.generators.community' has no attribute 'stochastic_block_model' - Ghadir Ayache

2 Answers

0
votes

The point was that I have to kill the running instance and restart my notebook or whatever python shell after upgrading the package to get the new updates.

0
votes

I am using version 2.2 of networkx which is the latest on pypi. This should work:

from networkx.generators.community import stochastic_block_model

Inside the __init__.py for the generators networkx.generators package you will find this:

"""
A package for generating various graphs in networkx.

"""
from networkx.generators.atlas import *
from networkx.generators.classic import *
from networkx.generators.community import *
from networkx.generators.degree_seq import *
from networkx.generators.directed import *
from networkx.generators.duplication import *
from networkx.generators.ego import *
from networkx.generators.expanders import *
from networkx.generators.geometric import *
from networkx.generators.intersection import *
from networkx.generators.joint_degree_seq import *
from networkx.generators.lattice import *
from networkx.generators.line import *
from networkx.generators.mycielski import *
from networkx.generators.nonisomorphic_trees import *
from networkx.generators.random_clustered import *
from networkx.generators.random_graphs import *
from networkx.generators.small import *
from networkx.generators.social import *
from networkx.generators.spectral_graph_forge import *
from networkx.generators.stochastic import *
from networkx.generators.trees import *
from networkx.generators.triads import *

Because of that you should be able to import it like this:

from networkx.generators import stochastic_block_model