Updated April 15, 2023
Introduction to NumPy concatenate arrays
Whenever there is a need to join two or more arrays which are of the same shape, we make use of a function in NumPy called concatenate function where concatenation means joining and concatenate function in NumPy takes two parameters arrayname1 arrayname2, which represents the two arrays to be joined and axis which represents the axis along which the given two arrays must be joined and the default for axis is zero and axis parameter is optional and concatenate function returns a concatenated array which is the array resulting from joining the given arrays specified as parameters to the concatenate function in NumPy.
Syntax:
numpy.concatenate(arrayname1,arrayname2, axis)
Where arrayname1 and arrayname2 are the names of the arrays which are to be concatenated, and they must be of the same shape and axis represents the axis along which the given arrays must be joined. The default value of the axis is zero.
Working of NumPy concatenate arrays Function
- Whenever there is a need to join two or more arrays of the same shape, we use a function in NumPy called concatenate function, where concatenation means joining.
- The concatenate function in NumPy takes two parameters arrayname1 arrayname2 which represents the two arrays to be joined and axis.
- arrayname1 and arrayname2 are the names of the arrays that are concatenated, and they must be of the same shape.
- Axis represents the axis along which the given arrays must be joined. The default value of the axis is zero.
- The concatenate function returns a concatenated array which is the array resulting from joining the given arrays specified as parameters to the concatenate function in NumPy.
Examples of NumPy concatenate arrays
Different examples are mentioned below:
Example #1
Python program to demonstrate function to create two arrays of the same shape and then use concatenate function to concatenate the two arrays that are created.
Code:
#importing the package numpy
import numpy as num
#creating an array using the array function and storing it in the variable arrayname1
arrayname1 = num.array([[5,4,3],[7,8,9]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print arrayname1
print '\n'
#creating an array using the array function and storing it in the variable arrayname2
arrayname2 = num.array([[10,9,8],[2,4,6]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print arrayname2
print '\n'
#using concatenate function to concatenate the two newly created array along the axis = 0
concatenatedarray = num.concatenate((arrayname1,arrayname2))
#displaying the elements of the concatenated array along the axis = 0
print 'The elements of the concatenated array along the axis = 0 are:'
print concatenatedarray
print '\n'
#using concatenate function to concatenate the two newly created array along the axis = 1
concatenatedarray1 = num.concatenate((arrayname1,arrayname2),axis=1)
#displaying the elemnets of the concatenated array along the axis = 1
print 'The elements of the concatenated array along the axis = 1 are:'
print concatenatedarray1
print '\n'
Output:
In the above program, numpy is the package imported in python, which allows us to make use of the array function and concatenate function. Then an array is created using the array function and stored in a variable called arrayname1. Then the elements of arrayname1 are displayed. Then another array is created using the array function and stored in a variable called arrayname2. Then the elements of the arrayname2 are displayed. Then concatenate function is used to concatenate the two arrays that are created twice, once along the axis = 0 and next time along the axis = 1.
Example #2
Python program to demonstrate function to create two arrays of the same shape and then use concatenate function to concatenate the two arrays that are created.
Code:
#importing the package numpy
import numpy as num
#creating an array using the array function and storing it in the variable arrayname1
arrayname1 = num.array([[11,12],[13,14]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print arrayname1
print '\n'
#creating an array using the array function and storing it in the variable arrayname2
arrayname2 = num.array([[15,16],[17,18]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print arrayname2
print '\n'
#using concatenate function to concatenate the two newly created array along the axis = 0
concatenatedarray = num.concatenate((arrayname1,arrayname2))
#displaying the elements of the concatenated array along the axis = 0
print 'The elements of the concatenated array along the axis = 0 are:'
print concatenatedarray
print '\n'
#using concatenate function to concatenate the two newly created array along the axis = 1
concatenatedarray1 = num.concatenate((arrayname1,arrayname2),axis=1)
#displaying the elemnets of the concatenated array along the axis = 1
print 'The elements of the concatenated array along the axis = 1 are:'
print concatenatedarray1
print '\n'
Output:
In the above program, numpy is the package imported in python that allows us to use array function and concatenate function. Then an array is created using the array function and stored in a variable called arrayname1. Then the elements of arrayname1 are displayed. Then another array is created using the array function and stored in a variable called arrayname2. Then the elements of the arrayname2 are displayed. Then concatenate function is used to concatenate the two arrays that are created twice, once along the axis = 0 and next time along the axis = 1.
Recommended Articles
This is a guide to NumPy concatenate arrays. Here we discuss the introduction, working and examples for better understanding. You may also have a look at the following articles to learn more –