numpy.pad中pad_width用法

发布于 2023-10-11  274 次阅读


numpy.pad中pad_width用法

代码:

# 研究下怎么padding
a = np.arange(1, 7).reshape(2, 3)
print(a.shape)
print('a:\n', a)
a_1_0 = np.pad(a, (1,0), 'edge')
print('a_1_0:\n',a_1_0)
a_0_1 = np.pad(a, (0,1), 'edge')
print('a_0_1:\n',a_0_1)
a_1_1 = np.pad(a, (1,1), 'edge')
print('a_1_1:\n',a_1_1)
a_1_0_0_0 = np.pad(a, ((1,0),(0,0)), 'edge')
print('a_1_0_0_0:\n',a_1_0_0_0)
a_1_1_0_0 = np.pad(a, ((1,1),(0,0)), 'edge')
print('a_1_1_0_0:\n',a_1_1_0_0)
a_1_0_1_0 = np.pad(a, ((1,0),(1,0)), 'edge')
print('a_1_0_1_0:\n',a_1_0_1_0)
# 根据测试发现,pad_width的用法:((上,下),(左,右))
# (1,0) 等价于 ((1, 0), (1, 0))

运行结果

(2, 3)
a:
 [[1 2 3]
 [4 5 6]]
a_1_0:
 [[1 1 2 3]
 [1 1 2 3]
 [4 4 5 6]]
a_0_1:
 [[1 2 3 3]
 [4 5 6 6]
 [4 5 6 6]]
a_1_1:
 [[1 1 2 3 3]
 [1 1 2 3 3]
 [4 4 5 6 6]
 [4 4 5 6 6]]
a_1_0_0_0:
 [[1 2 3]
 [1 2 3]
 [4 5 6]]
a_1_1_0_0:
 [[1 2 3]
 [1 2 3]
 [4 5 6]
 [4 5 6]]
a_1_0_1_0:
 [[1 1 2 3]
 [1 1 2 3]
 [4 4 5 6]]

整天不想事儿,就想着干饭