Javatpoint Logo
Javatpoint Logo

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

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

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

Output:

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

Example 3: numpy.zeros() with shape

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

Output:

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

Example 5: numpy.zeros() with custom dtype

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.


Next Topicnumpy.log()





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA