Python 零垫numpy数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38191855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zero pad numpy array
提问by Basj
What's the more pythonic way to pad an array with zeros at the end?
最后用零填充数组的更pythonic方法是什么?
def pad(A, length):
...
A = np.array([1,2,3,4,5])
pad(A, 8) # expected : [1,2,3,4,5,0,0,0]
In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3000 => 3072
在我的实际用例中,实际上我想将数组填充到最接近的 1024 倍数。例如:1342 => 2048, 3000 => 3072
回答by Psidom
numpy.pad
with constant
mode does what you need, where we can pass a tuple as second argument to tell how many zeros to pad on each size, a (2, 3)
for instance will pad 2zeros on the left side and 3zeros on the right side:
numpy.pad
with constant
mode 可以满足您的需求,我们可以传递一个元组作为第二个参数来告诉每个大小要填充多少个零(2, 3)
,例如将在左侧填充2 个零,在右侧填充3 个零:
Given A
as:
给出A
为:
A = np.array([1,2,3,4,5])
np.pad(A, (2, 3), 'constant')
# array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])
It's also possible to pad a 2D numpy arrays by passing a tuple of tuples as padding width, which takes the format of ((top, bottom), (left, right))
:
也可以通过传递元组元组作为填充宽度来填充 2D numpy 数组,其格式为((top, bottom), (left, right))
:
A = np.array([[1,2],[3,4]])
np.pad(A, ((1,2),(2,1)), 'constant')
#array([[0, 0, 0, 0, 0], # 1 zero padded to the top
# [0, 0, 1, 2, 0], # 2 zeros padded to the bottom
# [0, 0, 3, 4, 0], # 2 zeros padded to the left
# [0, 0, 0, 0, 0], # 1 zero padded to the right
# [0, 0, 0, 0, 0]])
For your case, you specify the left side to be zero and right side pad calculated from a modular division:
对于您的情况,您指定左侧为零,右侧填充根据模除法计算:
B = np.pad(A, (0, 1024 - len(A)%1024), 'constant')
B
# array([1, 2, 3, ..., 0, 0, 0])
len(B)
# 1024
For a larger A
:
对于更大的A
:
A = np.ones(3000)
B = np.pad(A, (0, 1024 - len(A)%1024), 'constant')
B
# array([ 1., 1., 1., ..., 0., 0., 0.])
len(B)
# 3072
回答by mgilson
This should work:
这应该有效:
def pad(A, length):
arr = np.zeros(length)
arr[:len(A)] = A
return arr
You mightbe able to get slightly better performance if you initialize an empty array (np.empty(length)
) and then fill in A
and the zeros
separately, but I doubt that the speedups would be worth additional code complexity in most cases.
如果您初始化一个空数组 ( ) 然后分别填充和填充,您可能会获得稍微更好的性能,但我怀疑在大多数情况下加速是否值得额外的代码复杂性。np.empty(length)
A
zeros
To get the value to pad up to, I think you'd probably just use something like divmod
:
为了获得要填充的价值,我认为您可能只使用以下内容divmod
:
n, remainder = divmod(len(A), 1024)
n += bool(remainder)
Basically, this just figures out how many times 1024 divides the length of your array (and what the remainder of that division is). If there is no remainder, then you just want n * 1024
elements. If there is a remainder, then you want (n + 1) * 1024
.
基本上,这只是计算 1024 将数组的长度除以多少次(以及该除法的剩余部分是多少)。如果没有余数,那么您只需要n * 1024
元素。如果有余数,那么你想要(n + 1) * 1024
.
all-together:
全部一起:
def pad1024(A):
n, remainder = divmod(len(A), 1024)
n += bool(remainder)
arr = np.zeros(n * 1024)
arr[:len(A)] = A
return arr
回答by Basj
For future reference:
备查:
def padarray(A, size):
t = size - len(A)
return np.pad(A, pad_width=(0, t), mode='constant')
padarray([1,2,3], 8) # [1 2 3 0 0 0 0 0]
回答by lballes
There's np.pad
:
有np.pad
:
A = np.array([1, 2, 3, 4, 5])
A = np.pad(A, (0, length), mode='constant')
Regarding your use case, the required number of zeros to pad can be calculated as length = len(A) + 1024 - 1024 % len(A)
.
关于您的用例,需要填充的零数可以计算为length = len(A) + 1024 - 1024 % len(A)
。
回答by Moses Koledoye
You could also use numpy.pad
:
您还可以使用numpy.pad
:
>>> A = np.array([1,2,3,4,5])
>>> npad = 8 - len(A)
>>> np.pad(A, pad_width=npad, mode='constant', constant_values=0)[npad:]
array([1, 2, 3, 4, 5, 0, 0, 0])
And in a function:
在一个函数中:
def pad(A, npads):
_npads = npads - len(A)
return np.pad(A, pad_width=_npads, mode='constant', constant_values=0)[_npads:]