NumPy positive()
The numpy.positive()
function returns the numerical positive value of each element in the input array.
It is an identity function that simply returns +x
for each element.
Syntax
numpy.positive(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 or scalar whose positive values are returned. |
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 compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the 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 positive values of the input array elements. If the input is a scalar, a scalar is returned.
Examples
1. Applying numpy.positive()
to a Single Value
In this example, we apply numpy.positive()
to a scalar value.
import numpy as np
# Define a scalar value
value = -5
# Apply numpy.positive()
result = np.positive(value)
# Print the result
print("Positive value:", result)
Output:
Positive value: -5

Since numpy.positive()
is an identity function, it returns the same value -5
.
2. Applying numpy.positive()
to an Array
Here, we apply numpy.positive()
to an array of integers.
import numpy as np
# Define an array with positive and negative values
arr = np.array([-3, 0, 4, -7, 8])
# Apply numpy.positive()
result = np.positive(arr)
# Print the result
print("Original array:", arr)
print("After applying numpy.positive():", result)
Output:
Original array: [-3 0 4 -7 8]
After applying numpy.positive(): [-3 0 4 -7 8]

Since numpy.positive()
does not alter the sign, it returns the same values as the original array.
3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
import numpy as np
# Define an array
arr = np.array([-2, 5, -9, 4])
# Create an output array with the same shape
output_array = np.empty_like(arr)
# Apply numpy.positive() and store the result in output_array
np.positive(arr, out=output_array)
# Print the results
print("Computed positive values:", output_array)
Output:
Computed positive values: [-2 5 -9 4]

Since numpy.positive()
is an identity operation, the values remain unchanged.
4. Using the where
Parameter
Using a condition to compute the positive values only for selected elements.
import numpy as np
# Define an array
arr = np.array([-3, 0, 7, -2, 5])
# Define a mask (apply positive only where mask is True)
mask = np.array([True, False, True, False, True])
# Apply numpy.positive() where the mask is True
result = np.positive(arr, where=mask)
# Print the results
print("Computed positive values with mask:", result)
Output:
Computed positive values with mask: [ -3 4602678819172646912 7
4609434218613702656 5]

The positive values are computed only for elements where mask=True
. The other values remain unchanged.