장 - 루의 대답은 질문에 적합합니다. 그러나 작동하면서 간단한 구현을하면 문제에 대해 좀 더 생각할 수 있었고, 유용 할 경우에 대비해 생각해 보았습니다.
def split_population2(seq, n_bins):
""" Split a population into sub-populations
Based on binning the data into n_bins and finding contigous groups of non-empty bins.
Returns [lowest, ..., highest] all of which are sorted sequences
"""
sorted_pop = sorted(seq)
# bin the data into n_bins in a 2d structure, one sequence for each bin:
_ , bins = np.histogram(sorted_pop, bins=n_bins)
bin_indices = np.digitize(sorted_pop, bins)
binned = []
for i in range(len(bins)+1):
binned.append([])
for ix_bin, v in zip(bin_indices, sorted_pop):
binned[ix_bin].append(v)
# now join-up non-empty bins
joined_bins = [[]] # so 2D, with initially 1 sub-list
len_last_bin = 0
for bin in binned:
len_bin = len(bin)
if len_bin == 0 and len_last_bin != 0: # will correctly handle the case where bin 0 is empty
joined_bins.append([])
if len_bin != 0:
joined_bins[-1].extend(bin)
len_last_bin = len_bin
return joined_bins
나는이 2 개 이상의 하위 인구가있는 경우에 작동하고, 하위 집단이 명확하게 분리되는 경우에 상대적으로 견고해야한다고 생각합니다. 단점은 어떤 경우에는 대답이
n_bins
에 대해 선택된 값에 달려 있다는 것입니다.
하나의 분할 ? 예를 들어 하나 더 많은 샘플이 있다면 어떻게 되나요? 0.4 –
글쎄요, 오직 2 개의 하위 집단 만이 원래 가정의 일부였습니다. 그러나 당신은 저에게 특별한 경우에 대해 질문하게했습니다! – lost