numpy.zeros function in Python
Hello coders, in this tutorial we will study about numpy.zeros function in Python. we will study the syntax of the function and also study some of the parameters that help us to control the function. I’ll show you some examples of how it works.
numpy.zeros function in Python
Ques What is numpy.zeros function?
Ans numpy.zeros is a Python inbuilt function which is used to create a matrix full of zeros.
Syntax of numpy.zeros :-
numpy.zeros(shape, dtype=float, order='c')
- shape:- shape defines the shape of the array.
- dtype :- dtype defines the datatype, it is optional and its default value is float64.
- order:- order is C which is an essential row style.
For this, we have to import NumPy library by the syntax:-
import numpy
Let’s take the example of creating a single dimension array containing two columns.
import numpy b=numpy.zeros(2) print("Matrix b :-\n",b)
Output:-
Matrix b:-
[0. 0.]
Example 2:- For the creation two-dimensional array:-
#Creationn of 2-D array import numpy a = numpy.zeros([2, 2]) print("\nMatrix a : \n", a)
Output:- Matrix a : [[0. 0.] [0. 0.]]
Example 3:- For the creation of three-dimensional array:-
#Three_dimensional array import numpy c = geek.zeros([3, 3]) print("\nMatrix c : \n", c)
Output:- Matrix c : [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
Example 4:- For the creation of an array of zeros with a specific data type
#Providing the datatype int import numpy as np a = np.zeros(8, int) print(a)
Output :-
[0 0 0 0 0 0 0 0]
Leave a Reply