如何在numpy中获得许多滚动窗口切片? [英] How to get many rolling window slices in numpy?

查看:101
本文介绍了如何在numpy中获得许多滚动窗口切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下numpy数组:

I have the following numpy array:

[[[1], [2], [3], [1], [2], [3]],
 [[4], [5], [6], [4], [5], [6]],
 [[7], [8], [9], [7], [8], [9]]]

我想要的每个元素最后一个维度, [1] [2] [3] 等在第二维中与以下 n 数组连接。如果发生溢出,则可以用0填充元素。例如,对于 n = 2

And I want each of the elements in the last dimension, [1], [2], [3] etc. to be concatenate with the following n arrays in the second dimension. In case of overflow, elements can be filled with 0. For example, for n = 2:

[[[1, 2, 3], [2, 3, 1], [3, 1, 2], [1, 2, 3], [2, 3, 0], [3, 0, 0]],
 [[4, 5, 6], [5, 6, 4], [6, 4, 5], [4, 5, 6], [5, 6, 0], [6, 0, 0]],
 [[7, 8, 9], [8, 9, 7], [9, 7, 8], [7, 8, 9], [8, 9, 0], [9, 0, 0]]]

我想使用内置的numpy函数很好地做到这一点性能,并且也想反向执行,即 n = -2 的偏移是公平的游戏。

I want to do this with the built in numpy functions for good performance and also want to do it in reverse i.e., a shift of n = -2 is fair game. How to do this?

对于 n = -2

[[[0, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, 1], [3, 1, 2], [1, 2, 3]],
 [[0, 0, 4], [0, 4, 5], [4, 5, 6], [5, 6, 4], [6, 4, 5], [4, 5, 6]],
 [[0, 0, 7], [0, 7, 8], [7, 8, 9], [8, 9, 7], [9, 7, 8], [7, 8, 9]]]

对于 n = 3

[[[1, 2, 3, 1], [2, 3, 1, 2], [3, 1, 2, 3], [1, 2, 3, 0], [2, 3, 0, 0], [3, 0, 0, 0]],
 [[4, 5, 6, 4], [5, 6, 4, 5], [6, 4, 5, 6], [4, 5, 6, 0], [5, 6, 0, 0], [6, 0, 0, 0]],
 [[7, 8, 9, 7], [8, 9, 7, 8], [9, 7, 8, 9], [7, 8, 9, 0], [8, 9, 0, 0], [9, 0, 0, 0]]]

如果当前形状数组的值为(height,width,1),操作后的形状为(height,width,abs(n)+ 1 )

If the current shape of the array is (height, width, 1), after the operation, the shape will be (height, width, abs(n) + 1).

如何对此进行概括,以便数字1、2、3等本身可以是numpy数组?

How to generalize this so that the numbers 1, 2, 3 etc. can themselves be numpy arrays?

推荐答案

这里是一种方法:

from skimage.util import view_as_windows

if n>=0:
  a = np.pad(a.reshape(*a.shape[:-1]),((0,0),(0,n)))
else:
  n *= -1
  a = np.pad(a.reshape(*a.shape[:-1]),((0,0),(n,0)))

b = view_as_windows(a,(1,n+1))
b = b.reshape(*b.shape[:-2]+(n+1,))

a 是您的输入数组,而 b 是您的输出:

a is your input array and b is your output:

n = 2

[[[1 2 3]
  [2 3 1]
  [3 1 2]
  [1 2 3]
  [2 3 0]
  [3 0 0]]

 [[4 5 6]
  [5 6 4]
  [6 4 5]
  [4 5 6]
  [5 6 0]
  [6 0 0]]

 [[7 8 9]
  [8 9 7]
  [9 7 8]
  [7 8 9]
  [8 9 0]
  [9 0 0]]]

n = -2

[[[0 0 1]
  [0 1 2]
  [1 2 3]
  [2 3 1]
  [3 1 2]
  [1 2 3]]

 [[0 0 4]
  [0 4 5]
  [4 5 6]
  [5 6 4]
  [6 4 5]
  [4 5 6]]

 [[0 0 7]
  [0 7 8]
  [7 8 9]
  [8 9 7]
  [9 7 8]
  [7 8 9]]]

说明


  • np.pad(a.reshape(* a.shape [:-1]),(((0,0),(0,n)))将足够的零填充到数组的右侧用于窗口溢出(类似地,对负 n 用左侧填充)

  • view_as_windows(a,(1, n + 1))根据问题的需要从数组中创建形状为(1,n + 1)的窗口。

  • b.reshape(* b.shape [:-2] +(n + 1,))消除了创建的长度1的额外维度通过(1,n + 1)形状的窗口,将 b 重塑为所需的形状。请注意,参数 * b.shape [:-2] +(n + 1,)只是两个元组的连接,以创建单个元组作为形状。

  • np.pad(a.reshape(*a.shape[:-1]),((0,0),(0,n))) pads enough zeros to the right side of array for overflow of windows (similarly padding left side for negative n)
  • view_as_windows(a,(1,n+1)) creates windows of shape (1,n+1) from the array as desired by the question.
  • b.reshape(*b.shape[:-2]+(n+1,)) gets rid of the extra dimension of length 1 created by (1,n+1)-shaped windows and reshape b to desired shape. Note the argument *b.shape[:-2]+(n+1,) is simply concatenation of two tuples to create a single tuple as shape.

这篇关于如何在numpy中获得许多滚动窗口切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆