numpy.concatenate() – NumPy concatenate Python Function

Introduction to NumPy concatenate

numpy.concatenate() function concatenate a sequence of arrays along an existing axis.

Syntax:

numpy.concatenate((arr1, arr2, …), axis=0, out=None)

Parameters:

  • arr1, arr2, … : [sequence of array_like] The arrays must have the same shape, except in the dimension corresponding to an axis.
  • axis : [int, optional] The axis along which the arrays will be joined. If an 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.

Return: [ndarray] The concatenated array.

Code Examples of NumPy concatenate

Example 01:

# welcome to softhunt.net
# Python program explaining
# numpy.concatenate() function

# importing numpy as np
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[4, 3], [2, 1]])

softhunt = np.concatenate((arr1, arr2), axis = 0)

print (softhunt)

Output:

[[1 2]
 [3 4]
 [4 3]
 [2 1]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# numpy.concatenate() function

# importing numpy as np
import numpy as np

arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[3, 4], [7, 8]])

softhunt = np.concatenate((arr1, arr2), axis = 1)

print (softhunt)

Output:

[[1 2 3 4]
 [5 6 7 8]]

Example 03:

# welcome to softhunt.net
# Python program explaining
# numpy.concatenate() function

# importing numpy as np
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

softhunt = np.concatenate((arr1, arr2), axis = None)

print (softhunt)

Output:

[1 2 3 4 5 6 7 8]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is the NumPy library?

NumPy is a Python module that allows you to interact with arrays. It also provides functions for working with matrices, Fourier transforms, and linear algebra. Travis Oliphant invented NumPy in 2005. It is an open-source project that you are free to use. Numerical Python is referred to as NumPy.

What is the axis in NumPy?

The directions along the rows and columns are referred to as NumPy axes. NumPy arrays include axes, much like coordinate systems. The axes are the directions along the rows and columns of a 2-dimensional NumPy array.

What is the difference between axis 0 and axis 1 in Python?

The horizontal axis is axis=0 (or axis=’rows’). The vertical axis is axis=1 (or axis=’columns’). To take things a step further. If you use the pandas method drop to delete columns or rows, you will be eliminating columns if you set axis=1. You will be eliminating rows from the dataset if you choose axis=0.

NumPy concatenate: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.require()

Leave a Comment