 Python

# How to Use Python NumPy zeros() and ones() Functions

NumPy library is one of the useful libraries of python that can be used to create arrays. zeros() and ones() are the NumPy library functions to create two different arrays. zeros() function is used to create an array based on the particular shape and type. All array elements are initialized to 0, which is created by the zeros() function. ones() function works like the zeros() function. But the elements of the array created by the ones() function are initialized to  1. The uses of both functions have shown in this tutorial by using multiple examples.

## zeros() function

The syntax of this function is given below.

array numpy.zeros(shape, [ dtype=None ], [order=’C’])

This function can take three arguments and returns an array. The first argument, the shape is mandatory that is used to define the size of the array. The value of this argument can be an integer or tuple of integers. The second argument, the dtype is optional to define the type of the array element. The default value of this argument is None. The third argument is that the order is optional and used to describe the order of a multi-dimensional array. This argument’s value can be ‘C’ (C-style) or ‘F’ (F-style). ‘C’ is used to set row-based order, and ‘F’ is used to set column-based order.

## Example-1: Use of zeros() function with one argument

The following example shows the use of the zeros() function. 10 has given in the argument value of this function to create a one-dimensional NumPy array. The data type of the array will be printed. reshape() function is used to change the one-dimensional into a two-dimensional array of 2 rows and 5 columns.

# Import NumPy

import numpy as np

# Create NumPy array using zeros() function

np_array = np.zeros(10)

# Print the data type of the array values

print("The data type of the array is: ", np_array.dtype)

# Print the array values after reshape

print("The values of reshaped array are: \n", np_array.reshape(2, 5))

Output:

The following output will appear after executing the above script. ## Example-2: Use of zeros() function with two arguments

The following example shows the use of the zeros() function with two arguments. The first zeros() function of the script will create a one-dimensional NumPy array of integers. The data type and the values of the first array will be printed in the next statement. The second zeros() function of the script will create a two-dimensional NumPy array of integers. The data type and the values of the second array will be printed in the next statement.

# Import NumPy

import numpy as np

# Create one-dimensional NumPy array using zeros() function

np_array1 = np.zeros(4, dtype=int)

# Print the data type

print("The data type of the array is : ", np_array1.dtype)

# Print the array values

print("The values of one-dimensional array are: \n", np_array1)

# Create two-dimensional NumPy array using zeros() function

np_array2 = np.zeros((2, 3), int)

# Print the data type

print("\nThe data type of the array is : ", np_array2.dtype)

# Print the array values

print("The values of two-dimensional array are: \n", np_array2)

Output:

The following output will appear after executing the above script. ## Example-3: Use of zeros() function with three arguments

The following example shows the use of the zeros() function with three arguments. The zeros() function of the script will create a three-dimensional NumPy array float numbers. The data type and the first array’s values will be printed in the next statement based on C-style ordering.

# Import NumPy

import numpy as np

# Create three-dimensional NumPy array using zeros() function with C-style ordering

np_array = np.zeros((2, 3, 2), float, 'C')

# Print the array values

print("The values of two-dimensional array are: \n", np_array)

Output:

The following output will appear after executing the above script. ## ones() function:

The syntax of this function is given below.

array numpy.ones(shape, [ dtype=None ], [order=’C’])

The uses of the arguments of ones() function are the same as the argument of zeros() function that has explained in the part of the zeros() function.

## Example-1: Use of ones() function with one argument

The following example shows the use of the ones() function with one argument. ones() function of this script will create a one-dimensional array of 5 elements.

# Import NumPy

import numpy as np

# Create NumPy array using zeros() function

np_array = np.ones(5)

# Print the array values

print("The values of array are: \n", np_array)

Output:

The following output will appear after executing the above script. ## Example-2: Use of ones() function with two arguments

The following example shows the use of the ones() function with two arguments. The first ones() function will create a two-dimensional array of integers that will contain 5 rows and 2 columns. The second ones() function will create a two-dimensional array where the first column will contain integers and the second column will include floats.

# Import NumPy

import numpy as np

# Create two-dimensional NumPy array using zeros() function with integer data type

np_array1 = np.ones((5, 2), int)

# Print the array values

print("The values of array are: \n", np_array1)

# Create two-dimensional NumPy array using zeros() function with integer and float data type

np_array2 = np.ones((2, 2), dtype=[('x', 'int'), ('y', 'float')])

# Print the array values

print("The values of array are: \n", np_array2)

Output:

The following output will appear after executing the above script. ## Example-3: Use of ones() function with three arguments

The following example shows the use of the ones() function with three arguments. ones() function will create a one-dimensional array of 5 elements that will contain floating numbers with row-based ordering.

# Import NumPy

import numpy as np

# Create NumPy array using zeros() function

np_array = np.ones(5, dtype=np.float64, order='F')

# Print the array values

print("The values of array are: \n", np_array)

Output:

The following output will appear after executing the above script. ## Conclusion:

The uses of zeros() and ones() functions of the NumPy library have been explained in this tutorial by using multiple examples. The reader will be able to use these functions in their script after reading this tutorial. 