[英]python how to pad numpy array with zeros
我想知道如何使用帶有 numpy 版本 1.5.0 的 python 2.6.6 用零填充 2D numpy 數組。 但這些都是我的局限。 因此我不能使用np.pad
。 例如,我想用零填充a
,使其形狀與b
匹配。 我想這樣做的原因是我可以這樣做:
b-a
這樣
>>> a
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]])
>>> b
array([[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.]])
>>> c
array([[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]])
我能想到的唯一方法是追加,但這看起來很丑陋。 是否有可能使用b.shape
的更清潔的解決方案?
編輯,謝謝 MSeiferts 的回答。 我不得不清理一下,這就是我得到的:
def pad(array, reference_shape, offsets):
"""
array: Array to be padded
reference_shape: tuple of size of ndarray to create
offsets: list of offsets (number of elements must be equal to the dimension of the array)
will throw a ValueError if offsets is too big and the reference_shape cannot handle the offsets
"""
# Create an array of zeros with the reference shape
result = np.zeros(reference_shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offsets[dim], offsets[dim] + array.shape[dim]) for dim in range(array.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = array
return result
NumPy 1.7.0(添加numpy.pad
時)現在已經很老了(它於 2013 年發布)所以即使問題要求不使用該功能的方法,我認為了解如何使用該功能可能會很有用numpy.pad
。
其實很簡單:
>>> import numpy as np
>>> a = np.array([[ 1., 1., 1., 1., 1.],
... [ 1., 1., 1., 1., 1.],
... [ 1., 1., 1., 1., 1.]])
>>> np.pad(a, [(0, 1), (0, 1)], mode='constant')
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
在這種情況下,我使用0
是mode='constant'
的默認值。 但它也可以通過顯式傳遞來指定:
>>> np.pad(a, [(0, 1), (0, 1)], mode='constant', constant_values=0)
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
以防第二個參數( [(0, 1), (0, 1)]
)看起來令人困惑:每個列表項(在本例中為元組)對應一個維度,其中的項表示(第一個元素)之前和之后的填充(第二個元素)。 所以:
[(0, 1), (0, 1)]
^^^^^^------ padding for second dimension
^^^^^^-------------- padding for first dimension
^------------------ no padding at the beginning of the first axis
^--------------- pad with one "value" at the end of the first axis.
在這種情況下,第一個軸和第二個軸的填充是相同的,因此也可以只傳入 2 元組:
>>> np.pad(a, (0, 1), mode='constant')
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
如果之前和之后的填充相同,甚至可以省略元組(但在這種情況下不適用):
>>> np.pad(a, 1, mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
或者,如果軸之前和之后的填充相同但不同,您也可以省略內部元組中的第二個參數:
>>> np.pad(a, [(1, ), (2, )], mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
但是我傾向於總是使用顯式的,因為它很容易出錯(當 NumPys 的期望與你的意圖不同時):
>>> np.pad(a, [1, 2], mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
這里 NumPy 認為你想在每個軸之前用 1 個元素和在每個軸之后用 2 個元素填充所有軸! 即使您打算在軸 1 中填充 1 個元素,在軸 2 中填充 2 個元素。
我使用元組列表進行填充,注意這只是“我的約定”,您也可以使用列表列表或元組元組,甚至數組元組。 NumPy 只檢查參數的長度(或者如果它沒有長度)和每個項目的長度(或者如果它有長度)!
非常簡單,您可以使用參考形狀創建一個包含零的數組:
result = np.zeros(b.shape)
# actually you can also use result = np.zeros_like(b)
# but that also copies the dtype not only the shape
然后在需要的地方插入數組:
result[:a.shape[0],:a.shape[1]] = a
瞧,您已經填充了它:
print(result)
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
如果您定義左上角元素應插入的位置,也可以使其更通用
result = np.zeros_like(b)
x_offset = 1 # 0 would be what you wanted
y_offset = 1 # 0 in your case
result[x_offset:a.shape[0]+x_offset,y_offset:a.shape[1]+y_offset] = a
result
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1.],
[ 0., 1., 1., 1., 1., 1.],
[ 0., 1., 1., 1., 1., 1.]])
但是請注意,您的偏移量不要超過允許的值。 例如,對於x_offset = 2
,這將失敗。
如果您有任意數量的維度,您可以定義一個切片列表來插入原始數組。 我發現玩一點很有趣,並創建了一個填充函數,只要數組和引用具有相同的維數並且偏移量不是太大,它就可以填充(帶有偏移量)任意形狀的數組。
def pad(array, reference, offsets):
"""
array: Array to be padded
reference: Reference array with the desired shape
offsets: list of offsets (number of elements must be equal to the dimension of the array)
"""
# Create an array of zeros with the reference shape
result = np.zeros(reference.shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offset[dim], offset[dim] + array.shape[dim]) for dim in range(a.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = a
return result
還有一些測試用例:
import numpy as np
# 1 Dimension
a = np.ones(2)
b = np.ones(5)
offset = [3]
pad(a, b, offset)
# 3 Dimensions
a = np.ones((3,3,3))
b = np.ones((5,4,3))
offset = [1,0,0]
pad(a, b, offset)
我知道您的主要問題是您需要計算d=ba
但您的數組有不同的大小。 不需要中間填充的c
您可以在不填充的情況下解決此問題:
import numpy as np
a = np.array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]])
b = np.array([[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.]])
d = b.copy()
d[:a.shape[0],:a.shape[1]] -= a
print d
輸出:
[[ 2. 2. 2. 2. 2. 3.]
[ 2. 2. 2. 2. 2. 3.]
[ 2. 2. 2. 2. 2. 3.]
[ 3. 3. 3. 3. 3. 3.]]
如果您需要將 1 的柵欄添加到數組中:
>>> mat = np.zeros((4,4), np.int32)
>>> mat
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
>>> mat[0,:] = mat[:,0] = mat[:,-1] = mat[-1,:] = 1
>>> mat
array([[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1]])
我知道我對此有點晚了,但是如果您想執行相對填充(也稱為邊緣填充),那么您可以通過以下方式實現它。 請注意,賦值的第一個實例會導致零填充,因此您可以將其用於零填充和相對填充(這是您將原始數組的邊緣值復制到填充數組的位置)。
def replicate_padding(arr):
"""Perform replicate padding on a numpy array."""
new_pad_shape = tuple(np.array(arr.shape) + 2) # 2 indicates the width + height to change, a (512, 512) image --> (514, 514) padded image.
padded_array = np.zeros(new_pad_shape) #create an array of zeros with new dimensions
# perform replication
padded_array[1:-1,1:-1] = arr # result will be zero-pad
padded_array[0,1:-1] = arr[0] # perform edge pad for top row
padded_array[-1, 1:-1] = arr[-1] # edge pad for bottom row
padded_array.T[0, 1:-1] = arr.T[0] # edge pad for first column
padded_array.T[-1, 1:-1] = arr.T[-1] # edge pad for last column
#at this point, all values except for the 4 corners should have been replicated
padded_array[0][0] = arr[0][0] # top left corner
padded_array[-1][0] = arr[-1][0] # bottom left corner
padded_array[0][-1] = arr[0][-1] # top right corner
padded_array[-1][-1] = arr[-1][-1] # bottom right corner
return padded_array
對此的最佳解決方案是 numpy 的 pad 方法。 在平均運行 5 次后,具有相對填充的 np.pad 僅比上面定義的函數好8%
。 這表明這是相對和零填充填充的最佳方法。
#My method, replicate_padding
start = time.time()
padded = replicate_padding(input_image)
end = time.time()
delta0 = end - start
#np.pad with edge padding
start = time.time()
padded = np.pad(input_image, 1, mode='edge')
end = time.time()
delta = end - start
print(delta0) # np Output: 0.0008790493011474609
print(delta) # My Output: 0.0008130073547363281
print(100*((delta0-delta)/delta)) # Percent difference: 8.12316715542522%
Tensorflow 還實現了調整圖像大小/填充圖像的功能tf.image.pad tf.pad 。
padded_image = tf.image.pad_to_bounding_box(image, top_padding, left_padding, target_height, target_width)
padded_image = tf.pad(image, paddings, "CONSTANT")
這些功能就像 tensorflow 的其他輸入管道功能一樣工作,並且對於機器學習應用程序會更好地工作。
def pad_n_cols_left_of_2d_matrix(arr, n):
"""Adds n columns of zeros to left of 2D numpy array matrix.
:param arr: A two dimensional numpy array that is padded.
:param n: the number of columns that are added to the left of the matrix.
"""
padded_array = np.zeros((arr.shape[0], arr.shape[1] + n))
padded_array[:, n:] = arr
return padded_array
def pad_n_cols_right_of_2d_matrix(arr, n):
"""Adds n columns of zeros to right of 2D numpy array matrix.
:param arr: A two dimensional numpy array that is padded.
:param n: the number of columns that are added to the right of the matrix.
"""
padded_array = np.zeros((arr.shape[0], arr.shape[1] + n))
padded_array[:, : arr.shape[1]] = arr
return padded_array
def pad_n_rows_above_2d_matrix(arr, n):
"""Adds n rows of zeros above 2D numpy array matrix.
:param arr: A two dimensional numpy array that is padded.
:param n: the number of rows that are added above the matrix.
"""
padded_array = np.zeros((arr.shape[0] + n, arr.shape[1]))
padded_array[n:, :] = arr
return padded_array
def pad_n_rows_below_2d_matrix(arr, n):
"""Adds n rows of zeros below 2D numpy array matrix.
:param arr: A two dimensional numpy array that is padded.
:param n: the number of rows that are added below the matrix.
"""
padded_array = np.zeros((arr.shape[0] + n, arr.shape[1]))
padded_array[: arr.shape[0], :] = arr
return padded_array
Original array:
[[0. 0.5 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0.5 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0.2 0.5 0.8 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0.3 0.7 1. 0. 0. 0. 0. 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]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]]
我來這里是為了尋找如何填充數組並找到很多文本,盡管我的目標很簡單,比如問題很簡單:用n
行或列填充二維數組。 我認為解釋更好,因為它們有助於建立理解。 但是,只是為了讓某些人願意節省一些時間,這里有一個可復制粘貼的功能。
每個函數將n
行或列添加到傳入數組。 該代碼假定傳入數組是一個 2D numpy 數組。 填充的方向取決於函數定義/名稱。 有關更詳細的解釋,我想請讀者參考 MSeifert 的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.