numpy.negative() in Python

numpy.negative() function is used when we want to compute the negative of array elements. It returns element-wise negative value of an array or negative value of a scalar.

Syntax : numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’)

Parameters :
arr : [array_like or scalar] Input array.
dtype : The type of the returned array. By default, the dtype of arr is used.
out : [ndarray, optional] 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.
where : [array_like, optional] Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
**kwargs : Allows to pass keyword variable length of argument to a function. Used when we want to handle named argument in a function.



Return : [ndarray or scalar] Returned array or scalar = -(input arr or scalar )

Code #1 : Working

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.negative() function
  
import numpy as geek
in_num = 10
  
print ("Input  number : ", in_num)
    
out_num = geek.negative(in_num) 
print ("negative of input number : ", out_num) 

chevron_right


Output :

Input  number :  10
negative of input number :  -10

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.negative function
  
import numpy as geek
  
in_arr = geek.array([[2, -7, 5], [-6, 2, 0]])
   
print ("Input array : ", in_arr) 
    
out_arr = geek.negative(in_arr) 
print ("negative of array elements: ", out_arr) 

chevron_right


Output :

Input array :  [[  2.   2.   2.]
 [  2.   2.  nan]]
product of array elements:  32.0Input array :  [[ 2 -7  5]
 [-6  2  0]]
negative of array elements:  [[-2  7 -5]
 [ 6 -2  0]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.