【发布时间】:2017-09-26 01:15:25
【问题描述】:
我正在使用numpy.array 作为数据缓冲区,我正在寻找一种优雅的方式来处理reframe 以便它保留一部分初始数据,具体取决于新的成帧条件(缓冲区可能有shrunk、expanded、shifted 或shift + 2 前者的组合)
Reframe 在这里可能不是合适的术语。但下面的例子希望能清楚地说明:
为简单起见,我将使用False 来说明一个空的reframed 数组元素:
import numpy as np
# Init buffer
data = 10 * np.arange(6) + 10 # dummy data for this example
# Result: array([10, 20, 30, 40, 50, 60]) #
缩小缓冲区:
# shift start by 1 to the right, and end by 1 to the left
reframe(data,1,-1) # basically doing: buffer[1:-1]
# Desired Result = array([20, 30, 40, 50]) #
扩展缓冲区:
# shift start by 2 to the left, and end by 1 to the
reframe(data,-2,1)
# Desired Result: array([False, False, 10, 20, 30, 40, 50, 60, False]) #
向左或向右移动缓冲区+扩展:
# shift start by 2 to the right, and end by 4 to the right
reframe(data,2,4)
# Desired Result: array([30, 40, 50, 60, False, False, False, False]) #
在此示例中,我再次使用False,我希望有一个新的空reframed 数组元素。这可能是np.empty,或np.NaN,等等...
为了实现我的目标,我写了以下内容:
import numpy as np
def reframe(data,start,end):
# Shrinking: new array is a substet of original
if start >= 0 and end <=0:
if start > 0 and end < 0:
return data[start:end]
if start > 0:
return data[start:]
return data[:end]
# Expand, new array fully contains original
elif start <= 0 and end >= 0:
new = np.zeros(data.shape[0] + end - start).astype(data.dtype)
new[abs(start):data.shape[0]+2] = data
return new
# Shift, new array may have a portion of old
else:
new = np.zeros((data.shape[0]-start+end)).astype(data.dtype)
# Shift Right
if start > 0:
new[:data.shape[0]-start] = data[start:]
return new
# Shift Left
if end < 0:
new[:data.shape[0]+end] = data[::-1][abs(end):]
return new[::-1]
测试:
print reframe(data,1,-1) # [20 30 40 50]
print reframe(data,-2,1) # [ 0 0 10 20 30 40 50 60 0]
print reframe(data,2,4) # [30 40 50 60 0 0 0 0]
所以这适用于我的目的,但我希望会有一些更优雅的东西。
在我的实际应用程序中,我的数组也有数十万个,所以效率是必须的。
【问题讨论】:
-
仅适用于一维阵列还是您还想“重构”ND 阵列?
-
@MSeifert 在我当前的用例中,我需要“重构”一维数组和二维数组,但在二维数组的情况下,我只关心它是否沿第一个轴。因此,如果您有一个仅适用于一维数组的解决方案,我不介意沿第一个轴元素明智地应用二维数组