Skip to main content
 首页 » 编程设计

python之简单的肥尾对数分箱

2025年05月04日12三少

我试图从 Plotting log-binned network degree distributions 中简化日志分级 输出显示原始分布和对数分箱分布。然而,后者并没有像预期的那样单调递减,与原来的偏差很大。 这个问题的最佳解决方案是什么?

import networkx as nx 
import matplotlib.pyplot as plt 
import numpy as np 
 
m = 3 
N = 900 
 
G = nx.barabasi_albert_graph(N, m) 
 
degree_list=nx.degree(G).values() 
 
kmin=min(degree_list) 
kmax=max(degree_list) 
 
bins=[float(k-0.5) for k in range(kmin,kmax+2,1)] 
density, binedges = np.histogram(degree_list, bins=bins, density=True) 
bins = np.delete(bins, -1) 
 
logBins = np.logspace(np.log10(kmin), np.log10(kmax),num=20) 
logBinDensity, binedges = np.histogram(degree_list, bins=logBins, density=True) 
logBins = np.delete(logBins, -1) 
 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.set_xscale('log') 
ax.set_yscale('log') 
 
plt.plot(bins,density,'x',color='black') 
plt.plot(logBins,logBinDensity,'x',color='blue') 
plt.show() 

请您参考如下方法:

噪声是由于 N 较小,而小值的偏移是由于 logbin 宽度小于 1。添加

for x in range(len(logBins)): 
    logBins[x] = mt.ceil(logBins[x])