gpt4 book ai didi

python - Networkx:NetworkXException:节点列表包含随机 block 模型的重复项

转载 作者:行者123 更新时间:2023-12-01 06:50:21 26 4
gpt4 key购买 nike

我是 networkx(版本 2.4)的新手,对 stochastic_block_model 收到的错误有点困惑当我尝试添加节点列表时。我尝试使用以下代码为网络中的每个 block 设置不同的颜色属性:

import networkx as nx
N_p = 10
N_n = 10
N_0 = 30
sizes = [N_p, N_n, N_0]
probs = [[0.25, 0.05, 0.02],
[0.05, 0.35, 0.07],
[0.02, 0.07, 0.40]]
nodelist = ['blue' for i in range(N_p)]
nodelist.extend(['red' for i in range(N_n)])
nodelist.extend(['green' for i in range(N_0)])
G = nx.stochastic_block_model(sizes, probs,nodelist=nodelist, seed=0,directed=1)

但我收到以下错误消息:

...
/opt/anaconda3/lib/python3.7/site-packages/networkx/generators/community.py in stochastic_block_model(sizes, p, nodelist, seed, directed, selfloops, sparse)
576 raise nx.NetworkXException("'nodelist' and 'sizes' do not match.")
577 if len(nodelist) != len(set(nodelist)):
--> 578 raise nx.NetworkXException("nodelist contains duplicate.")
579 else:
580 nodelist = range(0, sum(sizes))

NetworkXException: nodelist contains duplicate.

我做错了什么?

最佳答案

错误只是 - 节点列表包含重复项:

>>> nodelist
['blue'*10, 'red'*10, 'green'*30]

如您的文档链接中所示:

Raises NetworkXError –

If probabilities are not in [0,1]. If the probability matrix is not square (directed case). If the probability matrix is not symmetric (undirected case). If the sizes list does not match nodelist or the probability matrix. If nodelist contains duplicate.

要解决此问题,请不要使用节点列表,或者执行如下操作:

nodelist = [f'blue_{i}' for i in range(N_p)]
nodelist.extend([f'red_{i}' for i in range(N_n)])
nodelist.extend([f'green_{i}' for i in range(N_0)])

关于python - Networkx:NetworkXException:节点列表包含随机 block 模型的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com