NumPy copysign()
The numpy.copysign()
function changes the sign of the elements in an input array to match the sign of another array or scalar.
Syntax
</>
Copy
numpy.copysign(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | The values whose sign needs to be changed. |
x2 | array_like | The sign of each element in x2 is copied to the corresponding element in x1 . If x2 is a scalar, its sign is applied to all elements of x1 . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store results. If not provided, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to process. Unselected elements retain their original value. |
casting | str, optional | Defines the casting behavior when applying the function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Specifies the data type of the output array. |
subok | bool, optional | Determines whether to preserve subclasses of ndarray in the output. |
Return Value
Returns an array where the values of x1
have the same magnitude but the sign of x2
. If both x1
and x2
are scalars, a scalar is returned.
Examples
1. Copying the Sign of a Scalar
Here, we change the sign of a single value based on another scalar.
</>
Copy
import numpy as np
# Define values
x1 = -5.0 # Value to change
x2 = 3.0 # Sign donor
# Apply copysign
result = np.copysign(x1, x2)
# Print the result
print("Original value:", x1)
print("Sign donor:", x2)
print("Result after copysign:", result)
Output:
Original value: -5.0
Sign donor: 3.0
Result after copysign: 5.0

2. Applying copysign to an Array
Using numpy.copysign()
to change the sign of multiple elements based on another array.
</>
Copy
import numpy as np
# Define input arrays
x1 = np.array([1.0, -2.5, 3.8, -4.2]) # Values to modify
x2 = np.array([-1.0, 2.0, -3.0, 4.0]) # Sign donor
# Apply copysign
result = np.copysign(x1, x2)
# Print the results
print("Original values:", x1)
print("Sign donor values:", x2)
print("Result after copysign:", result)
Output:
Original values: [ 1. -2.5 3.8 -4.2]
Sign donor values: [-1. 2. -3. 4.]
Result after copysign: [-1. 2.5 -3.8 4.2]

3. Using the out
Parameter
Storing results in a predefined output array instead of creating a new array.
</>
Copy
import numpy as np
# Define input values
x1 = np.array([-7.5, 8.2, -3.6])
x2 = np.array([2.0, -5.0, 4.0])
# Create an output array with the same shape
output_array = np.empty_like(x1)
# Apply copysign with the out parameter
np.copysign(x1, x2, out=output_array)
# Print results
print("Result after copysign:", output_array)
Output:
Result after copysign: [ 7.5 -8.2 3.6]

4. Using the where
Parameter
Applying sign changes only to selected elements based on a condition.
</>
Copy
import numpy as np
# Define input arrays
x1 = np.array([-5.0, 6.0, -7.0, 8.0])
x2 = np.array([1.0, -1.0, 1.0, -1.0])
# Define a mask to apply copysign conditionally
mask = np.array([True, False, True, False])
# Apply copysign using the where parameter
result = np.copysign(x1, x2, where=mask)
# Print the results
print("Result after copysign with mask:", result)
Output:
Result after copysign with mask: [5. 0. 7. 0.]

Only the elements where mask=True
had their signs changed, while the rest remained unchanged.