Python:将列添加到numpy 2d数组

Python:将列添加到numpy 2d数组

问题描述:

我有一个60000 x 200 numpy数组.我想通过在右边添加1的列来使它乘以60000,然后乘以201. (所以每一行都是[prev,1]) 用axis = 1串联不起作用,因为看起来串联需要所有输入数组都具有相同的维数. 我应该怎么做?我找不到任何有用的答案,有关此问题的大多数答案是几年前写的,所以现在情况可能有所不同.

I have a 60000 by 200 numpy array. I want to make it 60000 by 201 by adding a column of 1's to the right. (so every row is [prev, 1]) Concatenate with axis = 1 doesn't work because it seems like concatenate requires all input arrays to have the same dimension. How should I do this? I can't find any existing useful answer, and most of the answers about this were written a few years ago so things might be different now.

让我举一个非常简单的示例,它的大小要小得多.原理应该相同.

Let me just throw in a very simple example with much smaller size. The principle should be the same.

a = np.zeros((6,2))
    array([[ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.]])
b = np.ones((6,1))
    array([[ 1.],
           [ 1.],
           [ 1.],
           [ 1.],
           [ 1.],
           [ 1.]])

np.hstack((a,b))
array([[ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.]])