Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Concatenate 3 arrays

Tags:

python

numpy

I have theee arrays, and want to combine them

import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[7,8,9],[10,11,12]])
c = np.array([[13,14,15],[16,17,18]])

To get:

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

What is the function for this ?

Thanks :)

like image 800
Cyrine Abdelkafi Avatar asked Feb 27 '17 15:02

Cyrine Abdelkafi


People also ask

How do you combine 3 arrays in Python?

You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.

How do you concatenate multiple arrays in Python?

In order to concatenate more than one array, you simply concatenate the array with the concatenation of all the previous arrays. Show activity on this post. That is, Equivalent to np.

How to concatenate arrays in Python NumPy?

The Python numpy concatenate function used to Join two or more arrays together. And it returns a concatenated ndarray as an output. The syntax of the Python numpy concatenate function is array1, array2,… are the arrays that you want to concatenate. The arrays that you pass to this concatenate function must have the same shape.

How to concatenate arrays of unequal length in Python?

19. Python concatenate two-byte arrays 20. Python concatenate two different size arrays 21. Python concatenate arrays of unequal length We can use numpy.concatenate () to concatenate multiple numpy arrays. import numpy as test a = test.arange (5,9) b = test.arange (2,4) c= test.arange (6,8) test.concatenate ( [a,b,c])

How to join two dimensional arrays in NumPy?

The Python Numpy concatenate function is not limited to join two arrays. You can use this function to concatenate more than two. Here, we are joining four different arrays using this function. We are using Python Numpy concatenate function to join two dimensional arrays.

How to concatenate two arrays along the second axis in JavaScript?

We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, ie. stacking. We pass a sequence of arrays that we want to join to the stack () method along with the axis. If axis is not explicitly passed it is taken as 0.


Video Answer


2 Answers

Stack those horizontally with np.hstack and flatten with np.ravel -

np.hstack(( a,b,c )).ravel()
like image 154
Divakar Avatar answered Nov 03 '22 00:11

Divakar


d=np.concatenate((a, b,c), axis=None)

The None parameter ensures that the arrays are flattened before concatenation (the gluing of arrays).

like image 45
Slickmind Avatar answered Nov 02 '22 23:11

Slickmind