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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36878089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:29:18  来源:igfitidea点击:

Python: Add a column to numpy 2d array

pythonarraysnumpy

提问by Jobs

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.

我有一个 60000 x 200 的 numpy 数组。我想通过在右侧添加一列 1 使其到 201 年达到 60000。(所以每一行都是 [prev, 1]) 与轴 = 1 连接不起作用,因为连接似乎要求所有输入数组具有相同的维度。我该怎么做?我找不到任何现有的有用答案,而且大多数关于此的答案都是几年前写的,所以现在情况可能有所不同。

回答by Hun

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.]])

回答by hpaulj

Under cover all the stackvariants (including appendand insert) end up doing a concatenate. They just precede it with some sort of array reshape.

在覆盖下所有stack变体(包括appendinsert)最终都会执行concatenate. 他们只是在它之前进行了某种数组重塑。

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. If I wanted to add a new row, I'd make a (1,4) array.

在这里,我制作了一个 (3,1) 数组,以匹配 (3,4) 数组。如果我想添加一个新行,我会创建一个 (1,4) 数组。

While the variations are handy, if you are learning, you should become familiar with concatenateand the various ways of constructing arrays that match in number of dimensions and necessary shapes.

虽然这些变化很方便,但如果您正在学习,您应该熟悉concatenate构建在维数和必要形状上匹配的数组的各种方法。

回答by Alan

The first thing to think about is that numpyarrays are really not meant to change size. So you should ask yourself, can you create your original matrix as 60k x 201 and then fill the last column afterwards. This is usually best.

首先要考虑的是numpy数组实际上并不意味着改变大小。所以你应该问问自己,你能不能将你的原始矩阵创建为 60k x 201,然后填充最后一列。这通常是最好的。

If you really must do this, see How to add column to numpy array

如果您真的必须这样做,请参阅 如何将列添加到 numpy 数组

回答by Randerson

I think the numpy method column_stackis more interesting because you do not need to create a column numpy array to stack it in the matrix of interest. With the column_stackyou just need to create a normal numpy array.

我认为 numpy 方法column_stack更有趣,因为您不需要创建列 numpy 数组来将其堆叠在感兴趣的矩阵中。使用column_stack,您只需要创建一个普通的 numpy 数组。