Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend numpy arrray

Tags:

python

numpy

I have 2 numpy arrays and i want to combine these two array together using extend. eg:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[0,0,0],[1,1,1]]

what i want is c = [[1,2,3],[4,5,6],[7,8,9],[0,0,0],[1,1,1]]

It seems that I cannot use extend as python list. otherwise it will raise AttributeError: 'numpy.ndarray' object has no attribute 'extend' error.

Currently I tried by transforming them into lists :

a_list = a.tolist()
b_list = b.tolist()
a_list.extend(b_list)
c = numpy.array(a_list)

I wonder if any better solution exist?

like image 489
Leoli Avatar asked Jul 29 '26 01:07

Leoli


1 Answers

Use -

np.concatenate((a, b), axis=0)

Or -

np.vstack((a,b))

Or -

a.append(b) # appends in-place, a will get modified directly

Output

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9],
       [0, 0, 0],
       [1, 1, 1]])
like image 152
Vivek Kalyanarangan Avatar answered Jul 31 '26 15:07

Vivek Kalyanarangan