numpy中具有不同窗口大小的滑动窗口

cqoc49vn  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(188)

我正在生成带有时间戳的数据(向上计数)。然后我想根据时间戳分离数组并计算每个窗口中数据的平均值。我的新数组将具有新的"时间戳"和计算的平均值数据。
我的代码工作正常,但我相信有一种更像数字的方式。我相信可以删除while循环,并使用np.where检查整个数组,因为它也已经排序了。
谢谢你的帮助。

# generating test data, first row timestamps, always counting up and random data 
data = np.array([np.cumsum(np.random.randint(100, size=20)), np.random.randint(1, 5, size=20)])
print(data)

window_size = 200
overlap = 100

i, l_lim, u_lim = 0, 0, window_size
timestamps = []
window_mean = []
while u_lim < data[0, -1]:
    window_mean.append(np.mean(data[1, np.where((data[0, :] > l_lim) & (data[0, :] <= u_lim))]))
    timestamps.append(i)
    l_lim = u_lim - overlap
    u_lim = l_lim + window_size
    i += 1

print(np.array([timestamps, window_mean]))
y3bcpkx1

y3bcpkx11#

虽然我可能减少了代码的行数,但我不认为我真的改进了那么多。主要的区别是迭代的方法,以及它用于定义数字选择边界的用途,但除此之外,我看不到任何改进代码的方法。以下是我的尝试,它是值得的:

代码:

import numpy as np

np.random.seed(5)
data = np.array([np.cumsum(np.random.randint(100, size=20)), np.random.randint(1, 5, size=20)])
print("Data:", data)

window_size = 200
overlap = 100

for i in range((max(data[0]) // (window_size-overlap)) + 1):
    result = np.mean(data[1, np.where((data[0] > i*(window_size-overlap)) & (data[0] <= (i*(window_size-overlap)) + window_size))])
    print(f"{i}: {result:.2f}")

输出:

Data: [[  99  177  238  254  327  335  397  424  454  534  541  617  632  685 765  792  836  913  988 1053]
       [   4    3    1    3    2    3    3    2    2    3    2    2    3    2    3    4    1    3    2    3]]

0: 3.50
1: 2.33
2: 2.40
3: 2.40
4: 2.25
5: 2.40
6: 2.80
7: 2.67
8: 2.00
9: 2.67
10: 3.00

相关问题