说明:
- 输入: 两个np.array数组
- 如数组[1,2,3,4,5,6,7,8,9,10], 在小于[1,2,8]中各个数值的占比如下
- [('data<1', '0.0'), ('data<2', '0.1'), ('data<8', '0.7')]
- 输出: 数组中指定分段间的比例值
代码
import sys
import numpy as np
def statistic_arr(myarr, bins):
"""
print the data distribution in myarr based on the segment in bins
Args:
myarr (np.array): the data for statistic
bins (np.array): the segment of the data, such as[0.5, 1, 5, 10]
"""
statis= np.arange(bins.size)
result = []
for i in range(0, bins.size):
statis[i]= myarr[myarr < bins[i]].size
str_item = ("data<" + str(bins[i]) , str(round(statis[i]/myarr.size,5) ))
result.append(str_item)
print(result)
# 测试代码
myarr=np.array([1,2,3,4,5,6,7,8,9,10])
bins=np.array([1, 2,8])
statistic_arr(myarr, bins)
结果
[('data<1', '0.0'), ('data<2', '0.1'), ('data<8', '0.7')]