Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Barabasi-Albert模型,错误的度指数_Python_Python 3.x_Graph Theory - Fatal编程技术网

Python Barabasi-Albert模型,错误的度指数

Python Barabasi-Albert模型,错误的度指数,python,python-3.x,graph-theory,Python,Python 3.x,Graph Theory,我试图用Barabasi-Albert模型生成一个无标度网络。该模型预测的度分布遵循p(k)~k^-3,但我的模型显示的是k^-2 该算法取自Barabasi的书,URL为:, 以下是相关段落: 这是我的密码,有人能帮我找出哪里出了问题吗 import numpy as np import matplotlib.pyplot as plt from collections import Counter plt.rcParams["figure.figsize"] = (15,6) #ini

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

该算法取自Barabasi的书,URL为:, 以下是相关段落:

这是我的密码,有人能帮我找出哪里出了问题吗

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()
将numpy导入为np
将matplotlib.pyplot作为plt导入
从收款进口柜台
plt.rcParams[“figure.figsize”]=(15,6)
#初始化值
N=10000
k=2
m=int(k/2)
#初始化矩阵
邻接=np.零((N,N))
度=np.零(N)
#添加链接
对于范围(N)中的i:
度[i]=m
对于范围(m)内的c:
#选择一个概率与其度成正比的节点
j=np.随机选择(N,p=度/(2*m*i+m+c))
度[j]+=1
邻接[i][j]+=1
邻接[j][i]+=1
def get_binned_数据(标签、值、数量):
最小标签,最大标签=最小(标签),最大(标签)
基本=(最大标签/最小标签)**(1/num)
bins=[base**i代表范围内的i(int(np.log(max_标签)/np.log(base))+1]
binned_值,binned_标签=[],[]
计数器=0
对于垃圾箱中的b:
bin_大小=0
bin_sum=0
当counterp=degrees/np.sum(degrees)

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


这可能会有所帮助,但我不清楚如何生成学位图,因此我无法验证。

非专家不清楚:“紧跟k^-3的学位指数,但我的显示k^-2。”在哪里?编辑。现在好点了吗?嗯,对我来说很好。我没有使用sum(度数),因为与计算me表达式(当前迭代中度数的和)相比,计算起来要昂贵得多。即使切换到求和法,它似乎也不起作用。我根据Barabasi书中的说明实现了我的模型。将使用我的绘图代码编辑帖子。