java 移动其他窗口_移动窗口平均值不等
TL;DR: 無論如何我可以擺脫我的第二個 for -loop?
我在2D網格上有一系列時間點 . 為了消除它們位置的快速波動,我在一個幀窗口上平均坐標 . 現在在我的情況下,它想要包含特定點的幀,如果它的行程比 cut_off 值更遠 .
在第一個 for -loop中,我遍歷所有幀并定義移動窗口 . 然后,我計算當前幀與移動窗口中每個幀之間的距離 . 在我從所有幀中僅抓取那些位置后, x 和 y 組件的行程都沒有超過 cut_off . 現在我想計算移動窗口所有這些選定幀中每個點的平均位置( note: 所選幀的數量可以小于 n_window ) . 這導致我第二個 for -loop . 在這里,我迭代所有點并實際 grab 幀中的位置,其中當前點沒有比 cut_off 傳播更遠 . 從這些選定的幀中,我計算坐標的平均值,并將其用作當前幀的新值 .
這最后 for -loop減慢了整個處理過程 . 我無法想出一個更好的方法來完成這個計算 . 有什么建議?
MWE
提出評論以澄清 .
import numpy as np
# Generate a timeseries with 1000 frames, each
# containing 50 individual points defined by their
# x and y coordinates
n_frames = 1000
n_points = 50
n_coordinates = 2
timeseries = np.random.randint(-100, 100, [n_frames, n_points, n_coordinates])
# Set window size to 20 frames
n_window = 20
# Distance cut off
cut_off = 60
# Set up empty array to hold results
avg_data_store = np.zeros([n_frames, timeseries.shape[1], 2])
# Iterate over all frames
for frame in np.arange(0, n_frames):
# Set the frame according to the window size that we're looking at
t_before = int(frame - (n_window / 2))
t_after = int(frame + (n_window / 2))
# If we're trying to access frames below 0, set the lowest one to 0
if t_before < 0:
t_before = 0
# Trying to access frames that are not in the trajectory, set to last frame
if t_after > n_frames - 1:
t_after = n_frames - 1
# Grab x and y coordinates for all points in the corresponding window
pos_before = timeseries[t_before:frame]
pos_after = timeseries[frame + 1:t_after + 1]
pos_now = timeseries[frame]
# Calculate the distance between the current frame and the windows before/after
d_before = np.abs(pos_before - pos_now)
d_after = np.abs(pos_after - pos_now)
# Grab indices of frames+points, that are below the cut off
arg_before = np.argwhere(np.all(d_before < cut_off, axis=2))
arg_after = np.argwhere(np.all(d_after < cut_off, axis=2))
# Iterate over all points
for i in range(0, timeseries.shape[1]):
# Create temp array
temp_stack = pos_now[i]
# Grab all frames in which the current point did _not_
# travel farther than `cut_off`
all_before = arg_before[arg_before[:, 1] == i][:, 0]
all_after = arg_after[arg_after[:, 1] == i][:, 0]
# Grab the corresponding positions for this points in these frames
all_pos_before = pos_before[all_before, i]
all_pos_after = pos_after[all_after, i]
# If we have any frames for that point before / after
# stack them into the temp array
if all_pos_before.size > 0:
temp_stack = np.vstack([all_pos_before, temp_stack])
if all_pos_after.size > 0:
temp_stack = np.vstack([temp_stack, all_pos_after])
# Calculate the moving window average for the selection of frames
avg_data_store[frame, i] = temp_stack.mean(axis=0)
總結
以上是生活随笔為你收集整理的java 移动其他窗口_移动窗口平均值不等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 灵锡如何注销账号
- 下一篇: oracle xe gentoo,Ora