NumPy fix() Function:
The fix() function of the NumPy module, rounds an array of floats element-wise to the nearest integer towards zero. The rounded values are returned in the floats format.
Syntax:
numpy.fix(x, out=None)
Parameters
x: This is required. It is an array (array-like) having elements of floats that are to be rounded.
out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
If out=None, this method returns an array of rounded values. A reference to out is returned if an output array is given.
NumPy fix() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the above-given array as an argument to the fix() function of the numpy module to get the rounded values of the given array elements to the nearest integers towards zero.
- Store it in another variable.
- Print the rounded values of the given array elements to the nearest integers towards zero.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array.
# Store it in a variable.
gvn_arry = np.array([[2.3, 6.7],[-8.8, -15.2]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the fix() function of the
# numpy module to get the rounded values of the given array elements
# to the nearest integers towards zero.
# Store it in another variable.
rslt = np.fix(gvn_arry)
# Print the rounded values of the given array elements to the nearest integers
# towards zero.
print("The rounded values of the given array elements to the nearest integers towards zero:")
print(rslt)
Output:
The above given array is: [[ 2.3 6.7] [ -8.8 -15.2]] The rounded values of the given array elements to the nearest integers towards zero: [[ 2. 6.] [ -8. -15.]]
Example2
Similarly, check for other numbers for the 1D array
Approach:
- Import numpy module using the import keyword.
- Give the array as static input and store it in a variable.
- Print the above-given array.
- Pass the above-given array as an argument to the fix() function of the numpy module to get the rounded values of the given array elements to the nearest integers towards zero.
- Store it in another variable.
- Print the rounded values of the given array elements to the nearest integers towards zero.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy as np
# Give the array as static input and store it in a variable.
gvn_arry = [0.53, 1.4, 1.99, 2.2]
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the fix() function of the
# numpy module to get the rounded values of the given array elements
# to the nearest integers towards zero.
# Store it in another variable.
rslt = np.fix(gvn_arry)
# Print the rounded values of the given array elements to the nearest integers
# towards zero.
print("The rounded values of the given array elements to the nearest integers towards zero:")
print(rslt)
Output:
The above given array is: [0.53, 1.4, 1.99, 2.2] The rounded values of the given array elements to the nearest integers towards zero: [0. 1. 1. 2.]