二项分布曲线拟合直方图

2024-03-29 02:09:02 发布

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

我有一个柱状图,它代表了一个网络的度分布。我需要把它拟合到二项式分布,但由于Scipy中没有离散分布的拟合方法,我不知道如何得到二项式函数所需的参数。在

似乎我没有从直方图中得到正确的参数,因为二项图与直方图的形状不匹配。 我做错什么了?在

G = nx.gnm_random_graph(5,5)# My Graph

d=np.array(list(dict(nx.degree(G,weight='weight')).values()))#Calculating 
degree distribution
c = np.array(range(0,len(d),1))# The number of nodes

h = plt.hist(d,density= True ,bins=25, stacked = True)#histogram

bionom_func= st.binom.pmf(k,n,p)# defining the binomial function


popt, pcov = optimize.curve_fit(f=bionom_func, xdata=c, ydata=d)

estimated_n = popt[0]
estimated_p = popt[1]
modeled_y = st.binom.pmf(c, estimated_n, estimated_p)
plt.plot(c, modeled_y, 'r-')# plotting the fit curve


plt.show()

Tags: true参数npplt直方图arrayfuncst