Thursday, October 16, 2025
HomeLanguagesnumpy.copysign() in Python

numpy.copysign() in Python

numpy.copysign(arr1, arr2, out = None, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None) : This mathematical function helps user to change the sign of arr1 and arr2. Both arr1 or arr2 can be either list/sequence or a scalar value. If sequence, both must have same dimension; otherwise arr2 can be a scalar value.

Parameters :
arr1 : [array_like]Input array, values to change sign of.
arr2 : [array_like]Input array, values to change sign of.
out : [ndarray, optional]Output array with same dimensions as Input array, placed with result.
**kwargs : Allows you to pass keyword variable length of argument to a function. It is used when we want to handle named argument in a function.
where : [array_like, optional]True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.

Return : x1 with sign of x2.

Code #1: 

Python3




# Python program illustrating
# copysign() method
import numpy as np
 
arr1 = [1, -23, +34, 11]
arr2 = [-1, 2, -3, -4]
 
print("arr1 : ", arr1)
print("arr2 : ", arr2)
 
print("\nCheck sign of arr1 : ", np.signbit(arr1))
print("\nCheck sign of arr2 : ", np.signbit(arr2))
print("\nCheck for copysign : ", np.signbit(np.copysign(arr1, arr2)))


Output: 

arr1 :  [1, -23, 34, 11]
arr2 :  [-1, 2, -3, -4]
Check sign of arr1 : [False  True False False]
Check sign of arr2 : [ True False  True  True]
Check for copysign : [ True False  True  True]

Code #2: 

Python3




# Python program illustrating
# copysign() method
import numpy as np
 
arr1 = [1, -23, +34, 11]
 
print("\nCheck sign of arr2 : ", np.signbit(arr1))
print("\nCheck for copysign : ", np.signbit(np.copysign(arr1, -3)))


Output: 

Check sign of arr2 : [False  True False False]
Check for copysign :  [ True  True  True  True]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS