Signup/Sign In

NumPy fix() function

In this tutorial, we will cover the numpy.fix() function of the Numpy Library.

The numpy.fix() function is used to round the array values to the nearest integers towards zero.

  • The rounded values returned by this function are in floats.

Syntax of numpy.fix():

The syntax required to use this function is as follows:

numpy.fix(array, b = None)  

Parameters:

The description of the parameters of this function is as follows:

  1. array: This parameter is used to indicate the array whose elements are to be rounded.

  2. b: This parameter is used to provide an ndarray (which is optional) which represents the location into which the result will be stored.

Returned Values:

This function always returns an array containing the rounded values.

Now it's time to cover an example.

Basic Example for numpy.fix():

Below we have the code snippet to gain the understanding of how this method works:

import numpy as np  
  
a = [0.289, 0.089, 1.2, 1.566, 9.909]  
  
print("The Input array is :")  
print(a)
  
y = np.fix(a)  
  
print("The Output array is :")
print(y)


The Input array is :
[0.289, 0.089, 1.2, 1.566, 9.909]
The Output array is :
[0. 0. 1. 1. 9.]

Summary

In this tutorial we covered the numpy.fix() mathematical function of the Numpy library which can be used to round off the values in an array. If we want to round only some values, then we can use other Numpy functions to create a new array and then use the fix() function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.