np.absolute
时间: 2023-10-28 21:27:34 浏览: 85
The np.absolute() function is a NumPy mathematical function that returns the absolute value of the input array. It calculates the positive distance between a number and zero, without considering its sign.
Syntax: np.absolute(x, /, out=None, *, where=True, casting='same_kind', **kwargs)
Parameters:
x : array_like - Input values.
out : ndarray, None, or tuple of ndarray and None, optional - A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
where : array_like, optional - Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
casting : {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional - Controls what kind of data casting may occur. Defaults to ‘same_kind’.
**kwargs - For other keyword-only arguments, see the ufunc docs.
Returns:
absolute : ndarray - An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is sqrt(a**2 + b**2).
Example:
import numpy as np
a = np.array([-1, 2, -3, 4, -5])
print(np.absolute(a))
Output:
[1 2 3 4 5]
阅读全文