[英]Identifying contiguously repeating sub-sequence arrays
假设我遇到一个问题,我的序列应如下所示:
>>> np.repeat([1,2,3,4],6)
array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
4, 4])
但实际上,由于噪声,传感器损坏或其他原因,它看起来更像这样:
array([6, 1, 1, 6, 1, 2, 2, 4, 2, 2, 2, 3, 3, 3, 3, 3, 8, 4, 4, 6, 4, 4])
值已丢失或注册错误。
现在我要:
1,1,1,1,1,1
是长度为6的序列,所有值均为1
)。 True
或False
。 您可以使用medfilt
从scipy
signal
模块
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
org = np.array([6, 1, 1, 6, 1, 2, 2, 4, 2, 2, 2, 3, 3, 3, 3, 3, 8, 4, 4, 6, 4, 4])
filt = signal.medfilt(org)
plt.plot(range(len(org)), org, label='orgiginal')
plt.plot(range(len(filt)), filt, label='filtered')
plt.legend()
print(filt)
sub_arrays = np.split(filt, np.where(np.diff(filt))[0]+1)
print(sub_arrays)
number_contiguous_sequences = len(sub_arrays)
for array in sub_arrays:
print(len(array)) # gives 4, 7, 5, 6
FILT:
[1. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 3. 3. 3. 3. 3. 4. 4. 4. 4. 4. 4.]
sub_arrays:
[array([1., 1., 1., 1.]), array([2., 2., 2., 2., 2., 2., 2.]), array([3., 3., 3., 3., 3.]), array([4., 4., 4., 4., 4., 4.])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.