问题描述
您好,我创建了以下代码以在Python中使用logaritmhic binning创建PDF。到目前为止,我一直在手动选择垃圾箱的数量。但是我想知道在使用对数分箱时是否有确定最佳箱数的特定方法。有特定的公式吗?
### Just to creat a power law distribution for example#################
import numpy as np
import powerlaw
a,xmin = 1.5,1.0
N = 10000
######### generates random variates of power law distribution
dt = powerlaw.Power_Law(xmin=xmin,parameters=[a]).generate_random(N)
##############################################################################
#### Manualy selecting the number of bin#### THATS WHAT I WANT TO CHANGE-> i AM NOT SURE IF I AM USING THE CORRECT NUMBER
bins1 =80
### CREATE THE BIN EDGES logarithmically####################################
split1 = np.logspace(0,np.log10(max(dt)),bins1)
half1 = np.zeros((bins1))
p11 = len(dt)*np.ones((bins1))
for jN in range(1):
half1[jN] = (1/2)*split1[jN]
for i in range(len(dt)):
if dt[i] >split1[jN]:
p11[jN]-=1
for jN in range(1,bins1):
half1[jN] = (1/2)*(split1[jN] - split1[jN-1]) + split1[jN-1]
half1[jN] = split1[jN-1]
for i in range(len(dt)):
if dt[i] <split1[jN-1] or dt[i] > split1[jN]:
#PVI[jN,i]=0
p11[jN]-=1
### REMOVE BINS WITH LESS THAN TEN OR FIVE COUNTS#####NOT RELEVAN HERE
idx2= np.where(p11 >5)[0]
###################################################3
### DEVIDE EACH BIN BY ITS WIDTH##########33
total1 = len(dt)
relative1 = np.zeros((bins1))
for jN in range(0,bins1):
if jN==0:
relative1[jN] = p11[jN]/(total1*split1[jN])
else:
relative1[jN] = p11[jN]/((total1)*(split1[jN]-split1[jN-1]))
###################################################################3
####### PLOT PDF ##########################################
plt.scatter(half1[idx2],relative1[idx2],s=40,facecolors='none',edgecolors='r')
plt.yscale("log")
plt.xscale("log")
plt.ylabel('PDF',fontsize=18)
plt.xlabel('WT (sec)',fontsize=18)
plt.xticks(fontsize=17)
plt.show()
这就是我得到的:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)