- How To Concatenate Arrays in NumPy?
- NumPy concatenate
- How To Concatenate 2 NumPy Arrays Row-wise?
- How To Concatenate 2 NumPy Arrays Column-wise?
- How To Concatenate more than 2 NumPy Arrays Row-wise?
- NumPy vstack example
- NumPy hstack example
- How to Concatenate Multiple 1d-Arrays?
- numpy.concatenate#
- numpy.concatenate#
How To Concatenate Arrays in NumPy?
Often you may have two or more NumPY arrays and want to concatenate/join/merge them into a single array. Python offers multiple options to join/concatenate NumPy arrays.
Common operations include given two 2d-arrays, how can we concatenate them row wise or column wise. NumPy’s concatenate function allows you to concatenate two arrays either by rows or by columns. Let us see a couple of examples of NumPy’s concatenate function.
Let us first import the NumPy package.
# import numpy import numpy as np
Let us create a NumPy array using arange function in NumPy. The 1d-array starts at 0 and ends at 8
We can use NumPy’s reshape function to convert the 1d-array to 2d-array of dimension 3×3, 3 rows and 3 columns. NumPy’s reshape function takes a tuple as input.
array2D_1 = array.reshape((3,3)) array2D_1 array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
Let us create second 2d-array by using arange and reshape functions. The second 2d-array starts at 10 and ends at 18
>array2D_2 = np.arange(10,19).reshape(3,3) >array2D_2 array([[10, 11, 12], [13, 14, 15], [16, 17, 18]])
NumPy concatenate
NumPy’s concatenate function can be used to concatenate two arrays either row-wise or column-wise. Concatenate function can take two or more arrays of the same shape and by default it concatenates row-wise i.e. axis=0. The resulting array after row-wise concatenation is of the shape 6 x 3, i.e. 6 rows and 3 columns.
How To Concatenate 2 NumPy Arrays Row-wise?
# concatenate 2 numpy arrays: row-wise >np.concatenate((array2D_1, array2D_2)) array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
How To Concatenate 2 NumPy Arrays Column-wise?
We can also concatenate 2 NumPy arrays by column-wise by specifying axis=1. Now the resulting array is a wide matrix with more columns than rows; in this example, 3 rows and 6 columns.
# concatenate 2 numpy arrays: column-wise >np.concatenate((array2D_1,array2D_2),axis=1) array([[ 0, 1, 2, 10, 11, 12], [ 3, 4, 5, 13, 14, 15], [ 6, 7, 8, 16, 17, 18]])
How To Concatenate more than 2 NumPy Arrays Row-wise?
NumPy’s concatenate function can be used with more than 2 arrays. Here is an example of concatenating 3 NumPy arrays row-wise. We specify the three arrays that we want to concatenate as a tuple.
# concatenate 3 numpy arrays: row-wise >np.concatenate((array2D_1, array2D_2, array2D_1)) array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [10, 11, 12], [13, 14, 15], [16, 17, 18], [ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]])
In addition to the concatenate function, NumPy also offers two convenient functions hstack and vstack to stack/combine arrays horizontally or vertically.
Both hstack and vstack, under the hood calls on concatenate with axis =1 and axis=0 options.
Here are the examples of using hstack and vstack.
NumPy vstack example
NumPy’s vstack stacks arrays in sequence vertically i.e. row wise. And the result is the same as using concatenate with axis=0.
>np.vstack((array2D_1, array2D_2)) array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
Another common use of Numpy’s hstack is to use it to combine two 1d-numpy arrays to one 2d-numpy array. For example, if we have two one-dimensional arrays,
x = np.ones(4) y = np.arange(1,5) print(x) print(y) [1. 1. 1. 1.] [1 2 3 4]
We can use Numpy’s vstack to create 2d-array of size 2×4
print(np.vstack((x, y))) [[1. 1. 1. 1.] [1. 2. 3. 4.]]
Similarly, with transpose we get 2d-array of 4×2 using vstack.
print(np.vstack((x, y)).T) [[1. 1.] [1. 2.] [1. 3.] [1. 4.]]
NumPy hstack example
NumPy’s hstack stacks arrays horizontally i.e. column wise. And the result is the same as using concatenate with axis=1.
>np.hstack((array2D_1, array2D_2)) array([[ 0, 1, 2, 10, 11, 12], [ 3, 4, 5, 13, 14, 15], [ 6, 7, 8, 16, 17, 18]])
How to Concatenate Multiple 1d-Arrays?
NumPy’s concatenate function can also be used to concatenate more than two numpy arrays. Here is an example, where we have three 1d-numpy arrays and we concatenate the three arrays in to a single 1d-array.
Let use create three 1d-arrays in NumPy.
x = np.arange(1,3) y = np.arange(3,5) z= np.arange(5,7)
And we can use np.concatenate with the three numpy arrays in a list as argument to combine into a single 1d-array
>np.concatenate([x,y,z]) array([1, 2, 3, 4, 5, 6])
numpy.concatenate#
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
axis int, optional
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
out ndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
dtype str or dtype
If provided, the destination array will have this dtype. Cannot be provided together with out.
Controls what kind of data casting may occur. Defaults to ‘same_kind’.
Concatenate function that preserves input masks.
Split an array into multiple sub-arrays of equal or near-equal size.
Split array into a list of multiple sub-arrays of equal size.
Split array into multiple sub-arrays horizontally (column wise).
Split array into multiple sub-arrays vertically (row wise).
Split array into multiple sub-arrays along the 3rd axis (depth).
Stack a sequence of arrays along a new axis.
Assemble arrays from blocks.
Stack arrays in sequence horizontally (column wise).
Stack arrays in sequence vertically (row wise).
Stack arrays in sequence depth wise (along third dimension).
Stack 1-D arrays as columns into a 2-D array.
When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are not preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead.
>>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) >>> np.concatenate((a, b), axis=None) array([1, 2, 3, 4, 5, 6])
This function will not preserve masking of MaskedArray inputs.
>>> a = np.ma.arange(3) >>> a[1] = np.ma.masked >>> b = np.arange(2, 5) >>> a masked_array(data=[0, --, 2], mask=[False, True, False], fill_value=999999) >>> b array([2, 3, 4]) >>> np.concatenate([a, b]) masked_array(data=[0, 1, 2, 2, 3, 4], mask=False, fill_value=999999) >>> np.ma.concatenate([a, b]) masked_array(data=[0, --, 2, 2, 3, 4], mask=[False, True, False, False, False, False], fill_value=999999)
numpy.concatenate#
The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
axis int, optional
The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
out ndarray, optional
If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
dtype str or dtype
If provided, the destination array will have this dtype. Cannot be provided together with out.
Controls what kind of data casting may occur. Defaults to ‘same_kind’. For a description of the options, please see casting .
Concatenate function that preserves input masks.
Split an array into multiple sub-arrays of equal or near-equal size.
Split array into a list of multiple sub-arrays of equal size.
Split array into multiple sub-arrays horizontally (column wise).
Split array into multiple sub-arrays vertically (row wise).
Split array into multiple sub-arrays along the 3rd axis (depth).
Stack a sequence of arrays along a new axis.
Assemble arrays from blocks.
Stack arrays in sequence horizontally (column wise).
Stack arrays in sequence vertically (row wise).
Stack arrays in sequence depth wise (along third dimension).
Stack 1-D arrays as columns into a 2-D array.
When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are not preserved. In cases where a MaskedArray is expected as input, use the ma.concatenate function from the masked array module instead.
>>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) >>> np.concatenate((a, b), axis=None) array([1, 2, 3, 4, 5, 6])
This function will not preserve masking of MaskedArray inputs.
>>> a = np.ma.arange(3) >>> a[1] = np.ma.masked >>> b = np.arange(2, 5) >>> a masked_array(data=[0, --, 2], mask=[False, True, False], fill_value=999999) >>> b array([2, 3, 4]) >>> np.concatenate([a, b]) masked_array(data=[0, 1, 2, 2, 3, 4], mask=False, fill_value=999999) >>> np.ma.concatenate([a, b]) masked_array(data=[0, --, 2, 2, 3, 4], mask=[False, True, False, False, False, False], fill_value=999999)