首页 > 解决方案 > 模块“networkx”没有属性“stochastic_block_model”

问题描述

我正在尝试使用此页面中记录的networkx“stochastic_block_model”中的函数生成随机块模型图:https ://networkx.github.io/documentation/stable/reference/generated/networkx.generators.community.stochastic_block_model.html

我的 networkx 包已更新到 2.2 版本,但我不断收到错误消息:模块“networkx”没有属性“stochastic_block_model”。我该如何解决这个问题?

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))

标签: pythonpython-3.xnetworkxgraph-theory

解决方案


关键是我必须在升级包后终止正在运行的实例并重新启动我的笔记本或任何 python shell 以获得新的更新。


推荐阅读