I have an np.array d1 of (3,6), and an np.array a4 of (1,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.
>Solution :
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]])