NumPy negative()
The numpy.negative() function computes the numerical negative of each element in an input array.
It returns an array with the negated values of the input.
Syntax
</>
Copy
numpy.negative(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | array_like or scalar | Input array whose elements will be negated. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to negate. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the negative function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the negated values of the input array elements. If the input is a scalar, a scalar is returned.
Examples
1. Negating a Single Value
Here, we compute the negative of a single scalar value.
</>
Copy
import numpy as np
# Define a single value
value = 5
# Compute the negative value
result = np.negative(value)
# Print the result
print("Negative of 5:", result)
Output:
Negative of 5: -5
