- Create 3D Array in NumPy
- 1. Create 3D Array using numpy.array()
- 2. Create 3D Array using numpy.zeros()
- 3. Create 3D Array using numpy.ones()
- 4. Create 3D Array using numpy.empty()
- Summary
- Python numpy трехмерный массив
- 2. Изменяем форму с помощью метода reshape()
- Источники
- NumPy 3D array
- Declaring the NumPy 3D array
- Example of NumPy 3D array
- Example #1
- Example #2
- Example #3
- Recommended Articles
Create 3D Array in NumPy
To create a 3D (3 dimensional) array in Python using NumPy library, we can use any of the following methods.
- numpy.array() – Creates array from given values.
- numpy.zeros() – Creates array of zeros.
- numpy.ones() – Creates array of ones.
- numpy.empty() – Creates an empty array.
1. Create 3D Array using numpy.array()
Pass a nested list (list of lists of lists) to numpy.array() function.
In the following program, we create a numpy 3D array of shape (2, 3, 4).
Python Program
import numpy as np # create a 3D array with shape (2, 3, 4) nested_list = [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]] arr = np.array(nested_list) print(arr)
[[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]]
2. Create 3D Array using numpy.zeros()
Pass shape of the required 2D array, as a tuple, as argument to numpy.zeros() function. The function returns a numpy array with specified shape, and all elements in the array initialised to zeros.
Python Program
import numpy as np # create a 3D array with shape (2, 3, 4) shape = (2, 3, 4) arr = np.zeros(shape) print(arr)
[[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]]
3. Create 3D Array using numpy.ones()
Pass shape of the required 3D array, as a tuple, as argument to numpy.ones() function. The function returns a numpy array with specified shape, and all elements in the array initialised to ones.
Python Program
import numpy as np # create a 3D array with shape (2, 3, 4) shape = (2, 3, 4) arr = np.ones(shape) print(arr)
[[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]]
4. Create 3D Array using numpy.empty()
Pass shape of the required 3D array, as a tuple, as argument to numpy.empty() function. The function returns a numpy array with specified shape.
Python Program
import numpy as np # create a 3D array with shape (2, 3, 4) shape = (2, 3, 4) arr = np.empty(shape) print(arr)
[[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]]
Summary
In this NumPy Tutorial, we learned how to create a 3D numpy array in Python using different NumPy functions.
Python numpy трехмерный массив
2. Изменяем форму с помощью метода reshape()
Оси и измерения. Чтобы изменить форму массива a1 , используем метод reshape() . Преобразуем одномерный массив из 12 чисел в двумерную таблицу размером 3×4. Первое число – количество строк, второе – столбцов. Строки соответствуют оси (англ. axis) 0, столбцы – оси 1. Ещё их называют измерениями (англ. dimensions).
Автоматическое вычисление размерности. Если нужно, чтобы NumPy сам определил размер незаданного измерения, передайте на этой позиции значение -1 :
Какую размерность имеет исходный массив a1 ? Может показаться, что массив a1 имеет размерность (1, 12) . Но это одномерный массив с размерностью (12, ) . Чтобы преобразовать одномерный массив к двумерному, используем метод reshape() :
Теперь соберём из них трёхмерный массив:
Источники
NumPy 3D array
Arrays in NumPy are data structures with high performance suitable for mathematical operations. The three levels of arrays nested inside one another represent the three-dimensional array in Python, where each level means one dimension. We use the array function in NumPy to create a three-dimensional array with an object as the parameter passed to it. NumPy represents a three-dimensional array as an object with nested lists, where x represents the outermost list, y represents the lists nested inside x, and z represents the values inside each y-nested list. So in this topic, we will learn about NumPy 3D array.
Web development, programming languages, Software testing & others
The syntax for NumPy 3D array in Python is as follows:
In NumPy, you can create a three-dimensional array by creating an object that represents x by y by z, where x represents the outermost list, y represents the lists nested inside x, and z represents the values inside each y-nested list.
Declaring the NumPy 3D array
- Arrays in NumPy are data structures with high performance 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 NumPy package represents a three-dimensional array as an object with nested lists, where x represents the outermost list, y represents the lists nested inside x, and z represents the values inside each y-nested list.
- Consider the representation of a three-dimensional array in Python below:
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 an object as a parameter to it and then to display the elements of the array on the screen:
#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'
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 in Python, we pass an object representing x by y by z, where x represents the nested lists, y represents the nested lists inside the x nested lists, and z represents the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray. The program then displays the elements of the newly created three-dimensional array, threedimarray, on the screen.
Example #2
Python program to demonstrate NumPy three dimensional array using array function in NumPy and passing an object as a parameter to it and then to display the elements of the array on the screen:
#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'
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 in Python, we pass an object representing x by y by z, where x represents the nested lists, y represents the nested lists inside the x nested lists, and z represents the values inside each y nested list. The newly created three-dimensional array is stored in the variable called threedimarray. The program then displays the elements of the newly created three-dimensional array, threedimarray, on the screen.
Example #3
Python program to demonstrate NumPy three dimensional array using array function in NumPy and passing an object as a parameter to it and then to display the elements of the array on the screen:
#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'
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 in Python, we pass an object representing x by y by z, where x represents the nested lists, y represents the nested lists inside the x nested lists, and z represents the values inside each y nested list. The program stores the newly created three-dimensional array in a variable called threedimarray. The program displays the elements of the newly created three-dimensional array, threedimarray, 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 –
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle 3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program 1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access