微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用对数分级Python时的最佳分级编号

如何解决使用对数分级Python时的最佳分级编号

您好,我创建了以下代码以在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()

这就是我得到的:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。