jaried
jaried

Reputation: 672

How to append np.array?

I have an np.array d1 of (3,6), and an np.array a4 of (6,).

How can I combine the two np.arrays to form an np.array d2 of (4,6)?

My code is as follows:

import numpy as np
a1=np.array(range(6))
a2=a1+2
a3=a2+3
a4=a3+4
d1=np.array([a1,a2,a3])
d1.shape
Out[44]: (3, 6)
d2=np.array([a1,a2,a3,a4])
d2.shape
d2
Out[45]: (4, 6)
Out[46]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 5,  6,  7,  8,  9, 10],
       [ 9, 10, 11, 12, 13, 14]])

How to get d2 from d1 and a4?

I tried np.insert and np.append, but maybe my usage is wrong and I didn't get the correct result.

Upvotes: 1

Views: 2130

Answers (4)

hpaulj
hpaulj

Reputation: 231385

This may be belaboring the point, but:

In [156]: d1.shape
Out[156]: (3, 6)
In [157]: a4.shape
Out[157]: (6,)               # not (1,6)
In [158]: np.append(d1,a4)
Out[158]: 
array([ 0,  1,  2,  3,  4,  5,  2,  3,  4,  5,  6,  7,  5,  6,  7,  8,  9,
       10,  9, 10, 11, 12, 13, 14])

np.append says it flattens the arrays, unless we provide an axis.

In [159]: np.append(d1,a4, axis=0)
Traceback (most recent call last):
  File "<ipython-input-159-dd72c66fd0c0>", line 1, in <module>
    np.append(d1,a4, axis=0)
  File "<__array_function__ internals>", line 180, in append
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 5392, in append
    return concatenate((arr, values), axis=axis)
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

Note that append has called np.concatenate. That's all it really does. np.append should be removed - it misleads too many novices.

Any ways, concatenate expects the arrays to match in number of dimensions. The error should be clear about the problem. The solution is, as the other answers show, to turn the (6,) into a (1,6).

Upvotes: 1

Aniekan jr
Aniekan jr

Reputation: 149

a4 is a 1D array, so you can expand it

import numpy as np 

d1 = np.array([[ 0,  1,  2,  3,  4,  5],
               [ 2,  3,  4,  5,  6,  7],
               [ 5,  6,  7,  8,  9, 10]])

a4 = np.array(np.arange(9,15))

d2 = np.concatenate((d1, np.expand_dims(a4,0)), 0)

print(d2)

Upvotes: 1

Mohamed Mostafa
Mohamed Mostafa

Reputation: 620

Firstly you need to reshape a4 which has currently the shape of (6,)

a4 = a4.reshape(1,-1)  # shape=(1,6)

then use np.concatenate

d2 = np.concatenate((d1, a4), axis=0)

output:

array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 5,  6,  7,  8,  9, 10],
       [ 9, 10, 11, 12, 13, 14]])

Upvotes: 1

Tyler Robbins
Tyler Robbins

Reputation: 111

I believe https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html concatenate is the command you are looking for.

foo = np.array([[1,2,3],
                [4,5,6]])
bar = np.array([7,8,9])

# axis 0 will combine rows, axis 1 will combine columns
foobar = numpy.concatenate(foo,bar,axis = 0)

array([[1,2,3],
       [4,5,6],
       [7,8,9]])

Upvotes: 2

Related Questions