简体   繁体   English

如何在python中最好将一维数组连续元素分组

[英]how to group 1D array consecutive elements in python preferably

I have following 1D array: 我有以下一维数组:

[0, 0, 0, 1, 0, 0, 16, 249, 142, 149, 189, 135, 141, 146, 294, 3, 2, 0, 3, 3, 6, 2, 3, 4, 21, 22, 138, 95, 86, 110, 72, 89, 79, 138, 14, 18, 18, 18, 12, 15, 21, 22, 11, 20, 26, 90, 62, 128, 94, 117, 81, 81, 137, 7, 13, 14, 6, 10, 8, 11, 10, 13, 21, 18, 140, 69, 147, 110, 112, 88, 100, 197, 9, 20, 5, 6, 5, 4, 7, 10, 21, 32, 42, 56, 41, 156, 95, 112, 81, 93, 152, 14, 19, 9, 12, 20, 18, 14, 21, 18, 18, 14, 91, 47, 43, 63, 41, 45, 43, 85, 15, 16, 14, 10, 11]

I can see the pattern where the spikes are. 我可以看到尖峰所在的模式。 So I want above array grouped as below: 所以我想将上面的数组分组如下:

[[0, 0, 0, 1, 0, 0, 16], [249, 142, 149, 189, 135, 141, 146, 294], [3, 2, 0, 3, 3, 6, 2, 3, 4, 21, 22], [138, 95, 86, 110, 72, 89, 79, 138]....so on]

I tried to use K mean, some combination of mean and std deviation. 我尝试使用K均值,均值和标准偏差的某种组合。 But none of them are resulting in this kind of grouping. 但是,没有一个导致这种分组。 Please help! 请帮忙!

Edit: These data are sum of dark pixel values of gray scaled image along x axes summed up on y axes. 编辑:这些数据是沿x轴的灰度图像的暗像素值的总和,沿y轴求和。 Higher range group represent written lines and lower range group represent blank lines. 较高的范围组代表写行,而较低的范围组代表空白行。 It means, I want to separate written and blank lines on image. 这意味着,我想将图像上的书面行和空白行分开。 So there is a pattern. 因此有一种模式。 Written lines will be of same width, that is their group length will be same. 书写线将具有相同的宽度,即它们的组长将相同。 Blank lines may have sudden spike because of background noises. 由于背景噪声,空白行可能会突然出现尖峰。 But overall, manually, I can see a pattern of written and blank lines. 但总体而言,我可以手动看到空白行的模式。 I want it programmatically. 我需要以编程方式。

A simple threshold-based approach will work in this case. 在这种情况下,一种简单的基于阈值的方法将起作用。

x = np.array([0, 0, 0, 1, 0, 0, 16, 249, 142, 149, 189, 135, 141, 146, 294, 3, 2, 
              0, 3, 3, 6, 2, 3, 4, 21, 22, 138, 95, 86, 110, 72, 89, 79, 138, 14, 
              18, 18, 18, 12, 15, 21, 22, 11, 20, 26, 90, 62, 128, 94, 117, 81, 
              81, 137, 7, 13, 14, 6, 10, 8, 11, 10, 13, 21, 18, 140, 69, 147, 
              110, 112, 88, 100, 197, 9, 20, 5, 6, 5, 4, 7, 10, 21, 32, 42, 56, 
              41, 156, 95, 112, 81, 93, 152, 14, 19, 9, 12, 20, 18, 14, 21, 18, 
              18, 14, 91, 47, 43, 63, 41, 45, 43, 85, 15, 16, 14, 10, 11])

mask = x > 30  # Mark values above/below threshold

cuts = np.flatnonzero(np.diff(mask))  # find indices where mask changes
cuts = np.hstack([0, cuts + 1, -1])  # let indices point after the change and add beginning and end of the array.

groups = []
for a, b in zip(cuts[:-1], cuts[1:]):  # iterate over index pairs
    groups.append(x[a:b].tolist())
print(groups)

# [[0, 0, 0, 1, 0, 0, 16], [249, 142, 149, 189, 135, 141, 146, 294], [3, 2, 0, 3, 3, 6, 2, 3, 4, 21, 22], [138, 95, 86, 110, 72, 89, 79, 138], [14, 18, 18, 18, 12, 15, 21, 22, 11, 20, 26], [90, 62, 128, 94, 117, 81, 81, 137], [7, 13, 14, 6, 10, 8, 11, 10, 13, 21, 18], [140, 69, 147, 110, 112, 88, 100, 197], [9, 20, 5, 6, 5, 4, 7, 10, 21], [32, 42, 56, 41, 156, 95, 112, 81, 93, 152], [14, 19, 9, 12, 20, 18, 14, 21, 18, 18, 14], [91, 47, 43, 63, 41, 45, 43, 85], [15, 16, 14, 10]]

More sophisticated approaches could involve fitting a piecewise constant model or detecting statistical instationarities, but usually it's best to stick with the simplest possible method that works. 更复杂的方法可能涉及拟合分段常数模型或检测统计不平稳性,但是通常最好坚持使用最简单可行的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM