装箱,然后将箱子与最少数量的观察结果组合起来?
Binning and then combining bins with minimum number of observations?
假设我创建了一些数据,然后创建了不同大小的容器:
from __future__ import division
x = np.random.rand(1,20)
new, = np.digitize(x,np.arange(1,x.shape[1]+1)/100)
new_series = pd.Series(new)
print(new_series.value_counts())
揭示:
20 17
16 1
4 1
2 1
dtype: int64
我基本上想转换底层数据,如果我设置每个 bin 至少 2 个的最小阈值,那么 new_series.value_counts()
是这样的:
20 17
16 3
dtype: int64
已编辑:
x = np.random.rand(1,100)
bins = np.arange(1,x.shape[1]+1)/100
new = np.digitize(x,bins)
n = new.copy()[0] # this will hold the the result
threshold = 2
for i in np.unique(n):
if sum(n == i) <= threshold:
n[n == i] += 1
n.clip(0, bins.size) # avoid adding beyond the last bin
n = n.reshape(1,-1)
这可以多次向上移动计数,直到箱子装满为止。
不使用np.digitize
,使用np.histogram
可能更简单,因为它会直接给你计数,所以我们不需要sum
自己.
假设我创建了一些数据,然后创建了不同大小的容器:
from __future__ import division
x = np.random.rand(1,20)
new, = np.digitize(x,np.arange(1,x.shape[1]+1)/100)
new_series = pd.Series(new)
print(new_series.value_counts())
揭示:
20 17
16 1
4 1
2 1
dtype: int64
我基本上想转换底层数据,如果我设置每个 bin 至少 2 个的最小阈值,那么 new_series.value_counts()
是这样的:
20 17
16 3
dtype: int64
已编辑:
x = np.random.rand(1,100)
bins = np.arange(1,x.shape[1]+1)/100
new = np.digitize(x,bins)
n = new.copy()[0] # this will hold the the result
threshold = 2
for i in np.unique(n):
if sum(n == i) <= threshold:
n[n == i] += 1
n.clip(0, bins.size) # avoid adding beyond the last bin
n = n.reshape(1,-1)
这可以多次向上移动计数,直到箱子装满为止。
不使用np.digitize
,使用np.histogram
可能更简单,因为它会直接给你计数,所以我们不需要sum
自己.