问题描述
我正在尝试用零填充一维numpy数组.
I am trying to pad a 1d numpy array with zeros.
这是我的代码
v = np.random.rand(100, 1)
pad_size = 100
v = np.pad(v, (pad_size, 0), 'constant')
如何获取所需的数组
大小(len(v)+ pad_size,1)?
of size (len(v)+pad_size, 1)?
推荐答案
pad
输出为2D,因为pad
输入为2D.您出于某些原因用rand
制作了2D数组:
The pad
output is 2D because the pad
input was 2D. You made a 2D array with rand
for some reason:
v = np.random.rand(100, 1)
如果您想要一维数组,则应该制作一维数组:
If you wanted a 1D array, you should have made a 1D array:
v = np.random.rand(100)
如果您想要一个1列2D数组,则您错误地使用了pad
.第二个参数应为((100, 0), (0, 0))
:在第一个轴上填充100个元素,在第一个轴上填充0个元素,在第二个轴上填充0个元素,在第二个轴上填充0个元素:
If you wanted a 1-column 2D array, then you're using pad
incorrectly. The second argument should be ((100, 0), (0, 0))
: padding 100 elements before in the first axis, 0 elements after in the first axis, 0 elements before in the second axis, 0 elements after in the second axis:
v = np.random.rand(100, 1)
pad_size = 100
v = np.pad(v, ((pad_size, 0), (0, 0)), 'constant')
对于1行2D数组,您需要同时调整rand
调用和pad
调用:
For a 1-row 2D array, you would need to adjust both the rand
call and the pad
call:
v = np.random.rand(1, 100)
pad_size = 100
v = np.pad(v, ((0, 0), (pad_size, 0)), 'constant')
这篇关于具有零的Numpy Pad创建2d数组,而不是所需的1d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!