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

snippet
numpy.concatenate((a1, a2, ...), axis)

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()

snippet
import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y))
z

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:

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

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

snippet
import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y), axis=0)
z

Output:

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

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

snippet
import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y.T), axis=1)
z

Output:

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

snippet
import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y), axis=None)
z

Output:

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()

snippet
import numpy as np
x=np.ma.arange(3)
y=np.arange(3,6)
x[1]=np.ma.masked
x
y
z1=np.concatenate([x,y])
z2=np.ma.concatenate([x,y])
z1
z2

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:

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)
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +