Updated April 20, 2023
Introduction to NumPy zeros
To create a new array of the specified type and specified shape and the value of the elements in the newly created array is zero. We make use of a function in NumPy called zeros where the count of elements in each and every dimension represents the shape of the array and the data type of the shape of the array is int or tuple of ints and the zeros function takes a parameter which is optional and specifies the data type of the array and the default value of the data type of the array is float and there is another parameter called order taken by the zeros function whose value decides if the multi-dimensional array must be stored in column-major order in the memory or row-major order in the memory.
Syntax :
zeros(shape, dtype=None, order='C')
- Where the size of the array is defined by shape.
- dtype is a parameter which is optional and specifies the data type of the array and the default data type of the array is float and, the order is a parameter whose value decides if the multi-dimensional array must be stored in a column-major order which is nothing Fortran style in the memory or row-major order which is nothing but C style order in the memory.
Working of NumPy zeros
- Whenever there is a need to create a new array of the specified type and specified shape and the value of the elements in the newly created array is zero, we make use of a function in NumPy called zeros function.
- The count of elements in each and every dimension represents the shape of the array and the data type of the shape of the array is int or tuple of ints.
- The zeros function takes a parameter that is optional and specifies the data type of the array and the default value of the data type of the array is float.
- There is another parameter called order taken by the zeros function whose value decides if the multi-dimensional array must be stored in column-major order in the memory or row-major order in the memory.
- The column-major order in the memory is also called Fortran style order in the memory.
- The row-major order in the memory is also called the C style order in the memory.
Examples
Given below are the examples mentioned:
Example #1
Python program to demonstrate NumPy zeros function to create a one-dimensional array consisting of the elements whose values are zero.
Code:
#importing the package numpy
import numpy as np
#creating a variable to store the one dimensional array created by using zeros function in NumPy
arrayname = np.zeros(5)
#the one dimensional array consisting of the elements whose values are zero created using NumPy zeros function is printed as output on the screen
print("The one dimensional array consisting of the elements whose values are zero created using NumPy zeros function is:")
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of zeros function. Then a variable is created to store the one-dimensional array created by using zeros function in NumPy. Then the one dimensional array consisting of the elements whose values are zero created using NumPy zeros function is printed as output on the screen.
Example #2
Python program to demonstrate NumPy zeros function to create a multi-dimensional array consisting of the elements whose values are zero.
Code:
#importing the package numpy
import numpy as np
#creating a variable to store the mutli dimensional array created by using zeros function in NumPy
arrayname = np.zeros((5,3))
#the multi dimensional array consisting of the elements whose values are zero created using NumPy zeros function is printed as output on the screen
print("The multi dimensional array consisting of the elements whose values are zero created using NumPy zeros function is:")
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of zeros function. Then a variable is created to store the multi-dimensional array created by using zeros function in NumPy. Then the multi-dimensional array consisting of the elements whose values are zero created using function is printed as output on the screen.
Example #3
Python program to demonstrate function to create a multi-dimensional array consisting of the elements whose values are zero and the data type of the elements of the array is int.
Code:
#importing the package numpy
import numpy as np
#creating a variable to store the mutli dimensional array, the data type of whose elements are specified to be of type int created by using zeros function in NumPy
arrayname = np.zeros((5,3), dtype=int )
#the multi dimensional array consisting of the elements whose values are zero and the data type of the elements is int created using NumPy zeros function is printed as output on the screen
print("The multi dimensional array consisting of the elements whose values are zero and the data type of the elements is int created using NumPy zeros function is:")
print(arrayname)
Output:
In the above program, a package called NumPy is imported to enable us to make use of zeros function. Then a variable is created to store the multi-dimensional array created by using zeros function in NumPy. Then the multi dimensional array consisting of the elements whose values are zero and the data type of the elements of the array is int, created using function is printed as an output on the screen.
Recommended Articles
This is a guide to NumPy zeros. Here we discuss the introduction, working of NumPy zeros along with examples respectively. You may also have a look at the following articles to learn more –