numpy.concatenate() in Python

The concatenate() function is a function from the NumPy package. This function essentially combines NumPy arrays together. This function is basically used for joining two or more arrays of the same shape along a specified axis. There are the following things which are essential to keep in mind:

  1. NumPy's concatenate() is not like a traditional database join. It is like stacking NumPy arrays.
  2. This function can operate both vertically and horizontally. This means we can concatenate arrays together horizontally or vertically.
numpy.concatenate()

The concatenate() function is usually written as np.concatenate(), but we can also write it as numpy.concatenate(). It depends on the way of importing the numpy package, either import numpy as np or import numpy, respectively.

Syntax

Parameters

1) (a1, a2, ...)

This parameter defines the sequence of arrays. Here, a1, a2, a3 ... are the arrays which have the same shape, except in the dimension corresponding to the axis.

2) axis : int(optional)

This parameter defines the axis along which the array will be joined. By default, its value is 0.

Result

It will return a ndarray containing the elements of both the arrays.

Example 1: numpy.concatenate()

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'x' using np.array() function.
  • Then, we have created another array 'y' using the same np.array() function.
  • We have declared the variable 'z' and assigned the returned value of np.concatenate() function.
  • We have passed the array 'x' and 'y' in the function.
  • Lastly, we tried to print the value of 'z'.

In the output, values of both the arrays, i.e., 'x' and 'y' shown as per the axis=0.

Output:

array([[ 1,  2],
       	[ 3,  4],
       	[12, 30]])

Example 2: numpy.concatenate() with axis=0

Output:

array([[ 1,  2],
       	[ 3,  4],
       	[12, 30]])

Example 3: numpy.concatenate() with axis=1

Output:

array([[ 1,  2, 12],
       	[ 3,  4, 30]])

In the above example, the '.T' used to change the rows into columns and columns into rows.

Example 4: numpy.concatenate() with axis=None

Output:

array([ 1,  2,  3,  4, 12, 30])

In the above examples, we have used np.concatenate() function. This function is not preserved masking of MaskedArray inputs. There is the following way through which we can concatenate the arrays that can preserve masking of MaskedArray inputs.

Example 5: np.ma.concatenate()

In the above code

  • We have imported numpy with alias name np.
  • We have created an array 'x' using np.ma.arrange() function.
  • Then, we have created another array 'y' using the same np.ma.arrange() function.
  • We have declared the variable 'z1' and assigned the returned value of np.concatenate() function.
  • We have declared variable 'z2' and assigned the returned value of np.ma.concatenate() function.
  • Lastly, we tried to print the value of 'z1' and 'z2'.

In the output, values of both the arrays 'z1' and 'z2' have preserved the masking of MaskedArray input.

Output:

masked_array(data=[0, --, 2],
             	mask=[False,  True, False],
       		fill_value=999999)
array([3, 4, 5])
masked_array(data=[0, 1, 2, 3, 4, 5],
             	mask=False,
       		fill_value=999999)
masked_array(data=[0, --, 2, 3, 4, 5],
             	mask=[False,  True, False, False, False, False],
       		fill_value=999999)





Latest Courses