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 :)
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.
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.
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.
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])
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.
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.
Stack those horizontally with np.hstack
and flatten with np.ravel
-
np.hstack(( a,b,c )).ravel()
d=np.concatenate((a, b,c), axis=None)
The None
parameter ensures that the arrays are flattened before concatenation (the gluing of arrays).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With