numpy.zeros() in Python

The numpy.zeros() function is one of the most significant functions which is used in machine learning programs widely. This function is used to generate an array containing zeros.

The numpy.zeros() function provide a new array of given shape and type, which is filled with zeros.

numpy.zeros() in Python

Syntax

snippet
numpy.zeros(shape, dtype=float, order='C'

Parameters

shape: int or tuple of ints

This parameter is used to define the dimensions of the array. This parameter is used for the shape in which we want to create an array, such as (3,2) or 2.

dtype: data-type(optional)

This parameter is used to define the desired data-type for the array. By default, the data-type is numpy.float64. This parameter is not essential for defining.

order: {'C','F'}(optional)

This parameter is used to define the order in which we want to store data in memory either row-major(C-style) or column-major(Fortran-style)

Return

This function returns a ndarray. The output array is the array with specified shape, dtype, order, and contains zeros.

Example 1: numpy.zeros() without dtype and order

snippet
import numpy as np
a=np.zeros(6)
a

Output:

Output
array([0., 0., 0., 0., 0., 0.])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the variable 'a' and assigned the returned value of np.zeros() function.
  • We have passed an integer value in the function.
  • Lastly, we tried to print the value of 'a'.

In the output, an array with floating-point integers(zeros) has been shown.

Example 2: numpy.zeros() without order

snippet
import numpy as np
a=np.zeros((6,), dtype=int)
a

Output:

Output
array([0, 0, 0, 0, 0, 0])

Example 3: numpy.zeros() with shape

snippet
import numpy as np
a=np.zeros((6,2))
a

Output:

Output
array([[0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.]])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the variable 'a' and assigned the returned value of np.zeros() function.
  • We have passed the shape for the array elements.
  • Lastly, we tried to print the value of 'a'.

In the output, an array of given shape has been shown.

Example 4: numpy.zeros() with the shape

snippet
Import numpy as np
s1=(3,2)
a=np.zeros(s1)
a

Output:

Output
array([[0., 0.], [0., 0.], [0., 0.]])

Example 5: numpy.zeros() with custom dtype

snippet
Import numpy as np
a=np.zeros((3,), dtype=[('x', 'i4'), ('y', 'i4')])
a

Output:

Output
array([(0, 0), (0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])

In the above code

  • We have imported numpy with alias name np.
  • We have declared the variable 'a' and assigned the returned value of np.zeros() function.
  • We have passed the shape and custom data type in the function.
  • Lastly, we tried to print the value of 'a'.

In the output, an array contains zeros with custom data-type has been shown.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +