What is the numpy.spacing() function in NumPy?
Overview
The numpy.spacing() function in NumPy is used to compute the distance between an input value x and the nearest adjacent number.
Syntax
numpy.spacing(x, /, out=None, *, where=True)
The syntax for the numpy.spacing() function
Parameter value
The numpy.spacing() function takes the following parameter values.
x: This represents the input value whose spacing is to be returned and is a required parameter.out: This represents the location where the result is stored and is an optional parameter.where: This is the condition over which the input is being broadcasted. At a given location where this condition isTrue, the resulting array will be set to theufuncresult. Otherwise, the resulting array will retain its original value. This is an optional parameter.**kwargs: This represents other keyword arguments and is an optional parameter.
Return value
It returns a scalar value which represents the distance between an input value x and the nearest adjacent number.
Example
import numpy as np# implementing the numpy.spacing() functionprint(np.spacing(5))
Explanation
- Line 1: We import the
numpymodule. - Line 3: We implement the
numpy.spacingfunction to determine the distance between5and the nearest adjacent number. We print the result to the console.