Introduction to NumPy 3D array
Arrays in NumPy are the data structures with high performance which are suitable for mathematical operations. The three levels of arrays nested inside one another represent the three-dimensional array in python, where each level represents one dimension. We make use of the array function in NumPy to create a three-dimensional array with an object as the parameter passed to it. The object represents x by y by z three-dimensional array in NumPy where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list. So in this topic, we are going to learn about NumPy 3D array.
Syntax:
The syntax for NumPy 3D array in Python is as follows:
numpy.array(object)
where an object represents x by y by z three-dimensional array in NumPy where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list.
Declaring the NumPy 3D array
- Arrays in NumPy are the data structures with high performance that are suitable for mathematical operations. For example, the three levels of arrays nested inside one another represent the three-dimensional array in python.
- Each level in the three-dimensional array represents one dimension. We use the array function in NumPy to create a three-dimensional array with an object as the parameter passed to it.
- The object represents x by y by z three-dimensional array in NumPy where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list.
- Consider the representation of three-dimensional array in python below:
([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
Here x = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
y = [[1, 2], [3, 4]] and [[5, 6], [7, 8]],
z = [1, 2], [3, 4] and [5, 6], [7, 8]
Example of NumPy 3D array
Given below are the examples of NumPy 3D array:
Example #1
Python program to demonstrate NumPy three dimensional array using array function in NumPy and passing object as a parameter to it and then to display the elements of the array on the screen:
Code:
#importing the package numpy as pynum
import numpy as pynum
#creating a three dimensional array using the array function and storing it in the variable called threedimarray
threedimarray = pynum.array([[[10,20,30,40], [50,60,70,80]]])
#displaying the elements of the newly created three dimensional array followed by an one line space
print 'The elements of the newly created three-dimensional array are:'
print '\n'
print threedimarray
print '\n'
Output:
In the above program, we are importing a package in python called NumPy as pynum. Importing the NumPy package enables us to use the array function in python. To create a three-dimensional array, we pass the object representing x by y by z in python, where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray. Then the elements of the newly created three-dimensional array threedimarray are displayed on the screen.
Example #2
Python program to demonstrate NumPy three dimensional array using array function in NumPy and passing object as a parameter to it and then to display the elements of the array on the screen:
Code:
#importing the package numpy as pynum
import numpy as pynum
#creating a three dimensional array using the array function and storing it in the variable called threedimarray
threedimarray = pynum.array([[[10,20], [30,40], [50,60], [70,80]]])
#displaying the elements of the newly created three dimensional array followed by an one line space
print 'The elements of the newly created three dimensional array are:'
print '\n'
print threedimarray
print '\n'
Output:
In the above program, we are importing a package in python called NumPy as pynum. Importing the NumPy package enables us to use the array function in python. To create a three-dimensional array, we pass the object representing x by y by z in python, where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray. Then the elements of the newly created three-dimensional array threedimarray are displayed on the screen.
Example #3
Python program to demonstrate NumPy three dimensional array using array function in NumPy and passing object as a parameter to it and then to display the elements of the array on the screen:
Code:
#importing the package numpy as pynum
import numpy as pynum
#creating a three dimensional array using the array function and storing it in the variable called threedimarray
threedimarray = pynum.array([[[10], [40], [60], [70]]])
#displaying the elements of the newly created three dimensional array followed by an one line space
print 'The elements of the newly created three dimensional array are:'
print '\n'
print threedimarray
print '\n'
Output:
In the above program, we are importing a package in python called NumPy as pynum. Importing the NumPy package enables us to use the array function in python. To create a three-dimensional array, we pass the object representing x by y by z in python, where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray. Then the elements of the newly created three-dimensional array threedimarray are displayed on the screen.
Recommended Articles
This is a guide to NumPy 3D array. Here we discuss the concept of NumPy 3D array in Python through definition, syntax, and declaration of the 3D array in python through programming examples and their outputs. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses