clustering groups of negative numbers
如果您只想对负值的块进行分组,而不考虑它们的相对值,那么只需计算第二个掩码来标识每个负值块的开始:
mask = values < 0
mask2 = np.r_[True, np.diff(mask)]
out = np.array_split(values[mask], np.nonzero(mask2[mask])[0][1:])
输出:[array([-1, -7, -3]), array([-7, -8])]
clustering groups of negative numbers if they are successive in value
If you want to cluster the negative values that also a successively decreasing (e.g. -1, -2, -3, -5, -6
would form 2 clusters: -1, -2, -3
and -5, -6
. Then I would use pandas:
- 转换为
Series
- 找出负值
- 为连续的负值创建分组(
(~mask).cumsum()
)
- 添加索引(或范围)以对连续的
groupby
个
import pandas as pd
s = pd.Series(values)
# mask to keep negative values
mask = s<0
# group consecutive negatives
group1 = (~mask).cumsum()
# group successive decrementing values
s2 = s+s.index
group2 = s2.ne(s2.shift()).cumsum()
out = [g.to_numpy() for k, g in s[mask].groupby([group1, group2])]
输出:[array([-1, -2, -3]), array([-7, -8]), array([-7])]
中间体:
s mask s2 group1 group2
0 1 False 1 1 1
1 2 False 3 2 2
2 3 False 5 3 3
3 -1 True 2 3 4 # out 1
4 -2 True 2 3 4 #
5 -3 True 2 3 4 #
6 4 False 10 4 5
7 5 False 12 5 6
8 6 False 14 6 7
9 -7 True 2 6 8 # out 2
10 -8 True 2 6 8 #
11 -7 True 4 6 9 # out 3
12 10 False 22 7 10