BarabasiAlbert模型,错误次数指数

2024-03-29 10:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Barabasi-Albert模型生成一个无标度网络。该模型预测的度分布遵循p(k)~k^-3,而我的模型显示k^-2。在

enter image description here

算法取自Barabasi的书,网址:http://barabasi.com/networksciencebook, 以下是相关段落:

Barabasi's algorithm

这是我的密码,有人能帮我找出问题所在吗?在

import numpy as np
import matplotlib.pyplot as plt
from collections import Counter
plt.rcParams["figure.figsize"] = (15,6)

#initialize values
N = 10000
k = 2
m = int(k / 2)

#initialize matrices
adjacency = np.zeros((N,N))
degrees = np.zeros(N)

#add links
for i in range(N):
    degrees[i] = m
    for c in range(m):
        # choose a node with probability proportional to it's degree
        j = np.random.choice(N, p = degrees / (2 * m * i + m + c))
        degrees[j] += 1
        adjacency[i][j] += 1
        adjacency[j][i] += 1


def get_binned_data(labels, values, num):
    min_label, max_label = min(labels), max(labels)
    base = (max_label / min_label) ** (1 / num)
    bins = [base**i for i in range(int(np.log(max_label) / np.log(base)) + 1)]
    binned_values, binned_labels = [], []
    counter = 0
    for b in bins:
        bin_size = 0
        bin_sum = 0
        while counter < len(labels) and labels[counter] <= b:
            bin_size += values[counter]
            bin_sum += values[counter] * labels[counter]
            counter += 1
        if(bin_size):
            binned_values.append(bin_size)
            binned_labels.append(bin_sum / bin_size)
    return binned_labels, binned_values


labels, values = zip(*sorted(Counter(degrees).items(), key = lambda pair: 
pair[0]))

binned_labels, binned_values = get_binned_data(labels, values, 15)

fig, (ax1, ax2) = plt.subplots(ncols = 2, nrows = 1)
fig.suptitle('Barabasi-Albert Model',fontsize = 25)

ax1.loglog(binned_labels, binned_values, basex = 10, basey = 10, linestyle = 
'None', marker = 'o',  color = 'red')
ax1.set(xlabel = 'degree', ylabel = '# of nodes')
ax1.set_title('log-log scale (log-binned)',{'fontsize':'15'})

ax2.plot(labels, values, 'ro')
ax2.set(xlabel = 'degree', ylabel = '# of nodes')
ax2.set_title('linear scale',{'fontsize':'15'})

plt.show()

Tags: inlogforsizelabelsbinnpcounter
1条回答
网友
1楼 · 发布于 2024-03-29 10:21:00

你的代码不运行(概率np.随机选择求和不等于1)。为什么不p = degrees/np.sum(degrees)?在

根据Wikipedia,您需要从一些已经连接的节点开始,而从零开始。另外,您可能应该将degrees[i] = m放在内部循环之后,以避免形成从节点i到自身的链接。在

这可能有帮助,但我不清楚你是如何生成学位图的,所以我无法验证。在

相关问题 更多 >