numpy.pad 用法

https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html
https://cugtyt.github.io/blog/2017/11022006.html



>>> import numpy as np >>> a = np.array([[1, 2, 3], [4, 5, 6]]) >>> np.pad(a, ((0, 0), (0, 0)), 'constant', constant_values=0) array([[1, 2, 3], [4, 5, 6]]) >>> np.pad(a, ((0, 0), (0, 1)), 'constant', constant_values=0) array([[1, 2, 3, 0], [4, 5, 6, 0]]) >>> np.pad(a, ((0, 0), (1, 0)), 'constant', constant_values=0) array([[0, 1, 2, 3], [0, 4, 5, 6]]) >>> np.pad(a, ((0, 1), (0, 0)), 'constant', constant_values=0) array([[1, 2, 3], [4, 5, 6], [0, 0, 0]]) >>> np.pad(a, ((1, 0), (0, 0)), 'constant', constant_values=0) array([[0, 0, 0], [1, 2, 3], [4, 5, 6]]) >>> np.pad(a, ((2, 0), (0, 0)), 'constant', constant_values=0) array([[0, 0, 0], [0, 0, 0], [1, 2, 3], [4, 5, 6]]) >>> np.pad(a, [0, 3], 'constant', constant_values=0) array([[1, 2, 3, 0, 0, 0], [4, 5, 6, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]])
原文地址:https://www.cnblogs.com/TMatrix52/p/12390754.html