How to compute numerical negative value for all elements in a given NumPy array?
Last Updated :
07 Aug, 2021
In this article, we will see how to compute the negative value for all elements in a given NumPy array. So, The negative value is actually the number which when added to any number becomes 0.
Example:
If we take a number as 4 then -4 is its negative number because when we add -4 to 4 we get sum as 0. Now let us take another example, Suppose we take a number -6 Now when we add +6 to it then the sum becomes zero. hence +6 is the negative value of -6. Now suppose we have an array of numbers:
A = [1,2,3,-1,-2,-3,0]
So, the negative value of A is
A'=[-1,-2,-3,1,2,3,0].
So, for finding the numerical negative value of an element we have to use numpy.negative() function of NumPy library.
Syntax: numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’)
Return: [ndarray or scalar] Returned array or scalar = -(input arr or scalar )
Now, let’s see the examples:
Example 1:
Python3
import numpy as np
x = np.array([ - 1 , - 2 , - 3 ,
1 , 2 , 3 , 0 ])
print ( "Printing the Original array:" ,
x)
r1 = np.negative(x)
print ( "Printing the negative value of the given array:" ,
r1)
|
Output:
Printing the Original array: [-1 -2 -3 1 2 3 0]
Printing the negative value of the given array: [ 1 2 3 -1 -2 -3 0]
Example 2:
Python3
import numpy as np
x = np.array([[ 1 , 2 ],
[ 2 , 3 ]])
print ( "Printing the Original array Content:\n" ,
x)
r1 = np.negative(x)
print ( "Printing the negative value of the given array:\n" ,
r1)
|
Output:
Printing the Original array Content:
[[1 2]
[2 3]]
Printing the negative value of the given array:
[[-1 -2]
[-2 -3]]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...