最近在做分箱统计时,想把一些点给设为参数,方便之后调整。
用到的函数cut
import numpy as np
def cus_cut_1(low_threshole, up_threshole, bins):
"""
个性化划分:
low_threshole:要均匀划分数据的下界
up_threshole:要均匀划分数据的上界
bins: 在上下界之间的等分数
"""
bin_0 = pd.IntervalIndex.from_tuples([(0, low_threshole)]) # 0-下界
bin_1 = pd.interval_range(start=low_threshole, end=up_threshole, periods=bins) # 下界-上界均分
bin_2 = pd.IntervalIndex.from_tuples([(up_threshole, 1)]) # 上界-1
return bin_0, bin_1, bin_2
bin_0, bin_1, bin_2 = cus_cut_1(0.2,0.8,5)
bin_0 = bin_0.tolist()
bin_1 = bin_1.tolist()
bin_2 = bin_2.tolist()
bin_0.extend(bin_1) # 把各区间的list拼接起来
bin_0.extend(bin_2)
ii = pd.IntervalIndex(bin_0) # 转为区间
print(ii)