问题描述
我有一个2D NumPy数组的时间序列,我想找出具有特定窗口大小(n)的最大和的位置(中心),
我已经尝试过使用conv2,如下所示:
from scipy.signal import convolve2d as conv2
def largest_sum_pos_app1(a,n):
idx = conv2(a,np.ones((n,n),dtype=int),'same').argmax()
return np.unravel_index(idx,a.shape)
但是,这将提供单个2D数组的位置,现在我想根据历史序列找出窗口的位置。 numpy或scipy中是否有内置模块来处理此类3D数组。
举个例子: 输入数组
([[0 1 4 0]
[1 2 5 1]
[2 3 6 0]],[[1 2 9 4]
[2 4 6 2]
[1 5 1 3]],[[0 2 3 1]
[0 3 5 0]
[1 4 6 1]])
以3 x 3大小的窗口为例,每个窗口的总和为:
[[24 22]
[31 36]
[24 25]]
现在,当我们计算总和为[79 83]
时,我将选择第二个窗口。这是简单的情况,但是我有更大的数组大小和数千个时间步长。有没有办法解决这个问题而没有任何循环。
解决方法
您可能想要oaconvolve
,它可以处理多个尺寸,并允许您选择要对其进行操作的尺寸。假设您有一个形状为a
的数组(k,width,height)
,其中k
是平面数:
from scipy.signal import oaconvolve
c = oaconvolve(a,np.ones((1,n,n)),axes=(-2,-1),mode='same')
idx = c.reshape(a.shape[0],-1).argmax(axis=1)
result = np.unravel_index(idx,a.shape[1:])
这不允许您选择进行卷积的方法,因此它可能不是算法的最佳选择。
,您的问题可能需要一些澄清,但我将假设您的“窗口”重叠。在这种情况下,您可以这样做:
import numpy as np
import scipy.ndimage as ndi
def largest_sum_pos_app1(a,n):
# assumes that your data is arranged as (time,y,x)
# uniform_filter will essentially calculate the sum of all pixels in a
# neighborhood around each pixel in your original array
window_sums = ndi.uniform_filter(a,n)
# to find the index we use argmax,but that requires a little acrobatics
max_idx = window_sums.reshape((len(a),-1)).argmax(1)
# the result of unravel_index is designed to be used for NumPy fancy indexing,# so we need to reshape it.
coords = np.array(np.unravel_index(max_idx,a.shape[1:])).T
# the result is the y,x coordinates for each time point
return coords
您需要注意的一件事是mode
的{{1}}参数,它确定如何处理图像的边缘。默认设置是仅用零填充边缘,这可能是您想要的,也可能不是。