Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Add a column to numpy 2d array

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.

like image 873
Jobs Avatar asked Apr 27 '16 00:04

Jobs


People also ask

Can you append to a 2D array in Python?

Use the append() Function to Append Values to a 2D Array in Python.

Can you add elements to a NumPy array?

Add array element You can add a NumPy array element by using the append() method of the NumPy module. The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above.

How do I add two columns in NumPy?

NumPy's concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more arrays of the same shape and by default it concatenates row-wise i.e. axis=0. The resulting array after row-wise concatenation is of the shape 6 x 3, i.e. 6 rows and 3 columns.


3 Answers

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.]])
like image 124
Hun Avatar answered Sep 28 '22 03:09

Hun


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.]])
like image 45
Stone Avatar answered Sep 28 '22 02:09

Stone


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.

like image 20
hpaulj Avatar answered Sep 28 '22 02:09

hpaulj