AppDividend
Latest Code Tutorials

np.zeros: How to Use numpy zeros() Function

Numpy is a third-party computational Python library that provides support for large multidimensional arrays and matrices and collects mathematical functions to operate on these items. Numpy array is the central data structure of the numpy library. 

To create a numpy array, you can use the np.array() function. Then, all you need to do is pass a list to it, and optionally, you can specify the data type.

np.zeros

The np.zeros() is a numpy library function used to return an array of similar shape and size with values of elements of the array as zeros. The zeros() function takes three arguments and returns the array filled with zero values.

The zeros() method is defined under NumPy, imported as import numpy as np. We can create multidimensional arrays and derive other mathematical statistics with the help of NumPy, a library in Python.

Creating Empty Arrays in Numpy

There will be times when you will have to create an empty array. For instance, you may be performing some computations, and you want a data structure to hold the results of those computations. In such cases, you may want to create an empty array, and that array is filled with all zeroes.

One way of doing this is with a NumPy array() function. You can create an empty NumPy array by passing in a Python list with all zeros.

import numpy as np

print(np.array([[0, 0, 0], [0, 0, 0]]))

Output

[[0 0 0]
 [0 0 0]]

The problem with the np.array(), though, is that it may not always be efficient. It’s a little tiresome. And it would be very bulky if you needed to create a vast array or an array with high dimensions.

Enter the Numpy zeros() method that can solve our problem.

The NumPy zeros method enables you to create Numpy arrays that contain only zeros. The zeros() function is used to get a new array of given shape and type filled with zeros.

Syntax

numpy.zeros(shape, dtype, order) 

Parameters

It takes three parameters, out of which one parameter is optional. 

The first parameter is the shape; it is an integer or a sequence of integers.

The second parameter is optional and is the datatype of the returning array. If you don’t define the data type, then np.zeros() will use float data type by default.

The third parameter is an order, representing the order in the memory, such as C_contiguous or F_contiguous.

Return Value

The np zeros() function returns an array with element values as zeros.

Example programs on numpy.zeros() method in Python

The syntax for using the np.zeros function is pretty straightforward, but it’s always easier to understand code when you have very few examples of working with. That being said, let’s look at some of the following examples.

Write a program to show the working of zeros() function in Python.

import numpy as np

arr1 = np.zeros(4, dtype=int)
print("Matrix arr1 : \n", arr1)

arr2 = np.zeros([2, 2], dtype=int)
print("\nMatrix arr2 : \n", arr2)

arr3 = np.zeros([3, 3])
print("\nMatrix arr3 : \n", arr3)

Output

Matrix arr1 :
 [0 0 0 0]

Matrix arr2 :
 [[0 0]
 [0 0]]

Matrix arr3 :
 [[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

In this example, we can see that after passing the shape of the matrix, we are getting zeros as its element by using numpy zeros(). So 1st example is 1×4, and all values filled with zeros are the same as the other two matrices.

In the third matrix arr3, they are all floating-point numbers. Remember, the items of the Numpy array must be of the same data type, and if we don’t define the data type, then the function will create floats by default.

Write a program to take a 4×4 matrix and then apply the zeros() function.

See the following code.

import numpy as np

arr1 = np.zeros([4, 4], dtype=int)
print("Matrix arr1 : \n", arr1)

Output

Matrix arr1 :
 [[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

In the above example, we can see that by passing a 4×4 matrix, we are getting a matrix of 16 elements with all its values 0.

Create A Numpy Zeroes Array With A Specific Shape

We can create arrays with a particular shape. We can do this by specifying the shape parameter.

import numpy as np

print(np.zeros(shape=(2, 3)))

Output

[[0. 0. 0.]
 [0. 0. 0.]]

Technically, you don’t have to explicitly call out the shape = parameter; you can define the shape with a tuple of values. Python will infer that it refers to shape (i.e., the shape is a “positional argument”).

import numpy as np

print(np.zeros((2, 3)))

Output

[[0. 0. 0.]
 [0. 0. 0.]]

You will see people do this a lot. For example, they will create Numpy arrays with a specific shape but won’t explicitly include the shape= parameter in the syntax.

What is numpy.ones()

The np.ones() function creates a matrix full of ones. It can be used to initialize the weights during the first iteration in TensorFlow and other statistic tasks.

Syntax

numpy.ones(shape, dtype=float, order='C')

Parameters

  1. Shape: is the shape of the array
  2. Dtype: is the datatype. It is optional. The default value is float64
  3. Order: Default is C, which is an essential row style.

Return Value

The np ones() function returns an array with element values as ones.

Example

import numpy as np
np.ones((1,2,3), dtype=np.int16)

Output

[[[1 1 1]
  [1 1 1]]]

Conclusion

In this tutorial, we have seen what numpy zeros() and ones() function is, then we have seen the variations of zeros() function based on its arguments.

Finally, np.zeros() function tutorial is over.

See also

Numpy fliplr()

NumPy triu()

NumPy tril()

NumPy tri()

NumPy empty_like()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.