34

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jobs
  • 3,317
  • 6
  • 26
  • 52

5 Answers5

60

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.]])
Hun
  • 3,707
  • 2
  • 15
  • 15
24

Using numpy index trick to append a 1D vector to a 2D array

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.]])
Stone
  • 1,119
  • 9
  • 17
5

Under cover all the stack variants (including append and insert) end up doing a 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. If I wanted to add a new row, I'd make a (1,4) array.

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.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
4

The first thing to think about is that numpy arrays 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.

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

Community
  • 1
  • 1
Alan
  • 9,410
  • 15
  • 20
1

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. With the column_stack you just need to create a normal numpy array.

Randerson
  • 775
  • 1
  • 5
  • 19
  • What does `column_stack` do (under the cover) that is different from `hstack`? – hpaulj Apr 27 '16 at 01:30
  • Does the same, but do do not need to create a column array was you did. You can use a line array and the `column_stack` will stack it as a new column of the matrix you want to be stacked with a new column. With the `hstack` you must use a column array otherwise it will return a error. – Randerson Apr 27 '16 at 11:40
  • @hpaulj: the first answer just shows an example of column_stack https://stackoverflow.com/a/63144310/3854750. As Randerson mentioned, the second array being added can be either column array of shape (N,1) or just a simple linear array of shape (N,) – Stone Sep 28 '22 at 20:51
  • `column_stack` just makes sure the array(s) is 2d, changing the (N,) to (N,1) if necessary. All these 'stack' functions end up using `np.concatenate`, with varying degrees of frontend tweaking. – hpaulj Sep 28 '22 at 21:35