在Numpy中移动不重叠的窗口 [英] Moving non-overlapping window in Numpy

查看:77
本文介绍了在Numpy中移动不重叠的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在numpy数组上移动窗口的最佳方法是什么,以使每个单独的块都不与前一个块重叠并且在块之间存在1个元素的间隙?我想我应该使用np.hstack,但是我无法弄清楚切片模式.

What's the best way to move a window over a numpy array so that each individual block does not overlap with the previous one and there is a 1 element gap between the blocks? I guess I should use np.hstack, but I can't figure out the slicing pattern.

基本上我正在寻找的是这个

Basically what I am looking for is this:

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
result = np.array([[0, 1, 2, 3],
                   [5, 6, 7, 8])

谢谢!

推荐答案

在简短示例中要实现的目标可以通过重塑数组,然后删除最后一列以创建间隙"来实现.

What you want to to achieve in your short example can be done by reshaping the array, then removing the last column to create a "gap".

import numpy as np

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# get length of flat array
a_length, =  a.shape

# reshape array 
#(column by row must respect number of elements)
b = a.reshape(( 2, a_length/2 ))

# assign array except last column to a variable
result = b[:,:-1]
print result

是否足够笼统地回答您的问题?

Would that be general enough as to answer your question?

这篇关于在Numpy中移动不重叠的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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