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.
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:
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]
Printing the Original array Content: [[1 2] [2 3]] Printing the negative value of the given array: [[-1 -2] [-2 -3]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.