- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change the sign of a value to that of another in Numpy
To change the sign of a value to that of another, use the numpy.copysign() method in Python Numpy. The 1st parameter of the copysign() is the value to change the sign of. The 2nd parameter is the sign to be copied to 1st parameter value.
The out is 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.
The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
Steps
At first, import the required library −
import numpy as np
Change the sign of a value to that of another, use the numpy.copysign() method in Python Numpy. Checking for integer −
print("Result? ", np.copysign(0, 1)) print("
Result? ", np.copysign(0, -1))
Checking for float −
print("
Result? ", np.copysign(14., -1)) print("
Result? ", np.copysign(-3.6, 1))
Checking for NaN −
print("
Result? ", np.copysign(np.nan, -1)) print("
Result? ", np.copysign(np.NAN, 1))
Checking for infinity −
print("
Result? ", np.copysign(np.inf, -1)) print("
Result? ", np.copysign(np.NINF, -1))
Checking for log −
print("
Result? ", np.copysign(np.log(1), 1)) print("
Result? ", np.copysign(np.log(2), -1))
Example
import numpy as np # To change the sign of a value to that of another, use the numpy.copysign() method in Python Numpy # The 1st parameter of the copysign() is the value to change the sign of. # The 2nd parameter is the sign to be copied to 1st parameter value. print("Result? ", np.copysign(0, 1)) print("
Result? ", np.copysign(0, -1)) # Checking for float print("
Result? ", np.copysign(14., -1)) print("
Result? ", np.copysign(-3.6, 1)) # Checking for NaN print("
Result? ", np.copysign(np.nan, -1)) print("
Result? ", np.copysign(np.NAN, 1)) # Checking for infinity print("
Result? ", np.copysign(np.inf, -1)) print("
Result? ", np.copysign(np.NINF, -1)) # Checking for log print("
Result? ", np.copysign(np.log(1), 1)) print("
Result? ", np.copysign(np.log(2), -1))
Output
Result? 0.0 Result? -0.0 Result? -14.0 Result? 3.6 Result? nan Result? nan Result? -inf Result? -inf Result? 0.0 Result? -0.6931471805599453
- Related Articles
- Change the sign of Numpy array values to that of a scalar element-wise
- A positive sign in the value of magnification indicates that the image is ___________.
- Return an element-wise indication of the sign of a number in Numpy
- Python Program to get indices of sign change in a list
- Return the truth value of an array equal to another element-wise in Numpy
- Return the next floating-point value after a value towards another value in Numpy
- Return the truth value of an array not equal to another element-wise in Numpy
- Compute the truth value of an array AND to another array element-wise in Numpy
- Compute the truth value of an array OR to another array element-wise in Numpy
- How to change the sign of even number rows in column of a data.table object in R?
- Return an element-wise indication of the sign of complex types in Numpy
- Return the truth value of an array less than equal to another element-wise in Numpy
- Return the truth value of an array greater than equal to another element-wise in Numpy
- Return the truth value of an array less than another element-wise in Numpy
- Compute the truth value of an array XOR another array element-wise in Numpy
