简体   繁体   English

Python:向 numpy 二维数组添加一列

[英]Python: Add a column to numpy 2d array

I have a 60000 by 200 numpy array.我有一个 60000 x 200 的 numpy 数组。 I want to make it 60000 by 201 by adding a column of 1's to the right.我想通过在右侧添加一列 1 使其到 201 年达到 60000。 (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. (所以每一行都是 [prev, 1]) 与轴 = 1 连接不起作用,因为连接似乎要求所有输入数组具有相同的维度。 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.]])

Using numpy index trick to append a 1D vector to a 2D array使用 numpy 索引技巧将一维向量附加到二维数组

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

Under cover all the stack variants (including append and insert ) end up doing a concatenate .在掩护下,所有stack变体(包括appendinsert )最终都会执行concatenate They just precede it with some sort of array reshape.他们只是在它之前进行了某种数组重塑。

In [60]: A = np.arange(12).reshape(3,4)

In [61]: np.concatenate([A, np.ones((A.shape[0],1),dtype=A.dtype)], axis=1)
Out[61]: 
array([[ 0,  1,  2,  3,  1],
       [ 4,  5,  6,  7,  1],
       [ 8,  9, 10, 11,  1]])

Here I made a (3,1) array of 1s, to match the (3,4) array.在这里,我制作了一个 (3,1) 数组,以匹配 (3,4) 数组。 If I wanted to add a new row, I'd make a (1,4) array.如果我想添加一个新行,我会创建一个 (1,4) 数组。

While the variations are handy, if you are learning, you should become familiar with concatenate and the various ways of constructing arrays that match in number of dimensions and necessary shapes.虽然这些变化很方便,但如果您正在学习,您应该熟悉concatenate以及构建在维数和必要形状上匹配的数组的各种方法。

The first thing to think about is that numpy arrays are really not meant to change size.首先要考虑的是numpy数组实际上并不意味着改变大小。 So you should ask yourself, can you create your original matrix as 60k x 201 and then fill the last column afterwards.所以你应该问问自己,你能不能把你的原始矩阵创建为 60k x 201 然后填充最后一列。 This is usually best.这通常是最好的。

If you really must do this, see How to add column to numpy array如果您真的必须这样做,请参阅如何将列添加到 numpy 数组

I think the numpy method column_stack is more interesting because you do not need to create a column numpy array to stack it in the matrix of interest.我认为 numpy 方法column_stack更有趣,因为您不需要创建列 numpy 数组来将其堆叠在感兴趣的矩阵中。 With the column_stack you just need to create a normal numpy array.使用column_stack,您只需要创建一个普通的 numpy 数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM