numpy.negative () in Python

numpy.negative() is used when we want to calculate the negation of array elements. Returns an element-wise negative array value or negative scalar value.

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.
 – & gt ; If provided, it must have a shape that the inputs broadcast to.
 – & gt; 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: Work

# 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) 

Output:

 Input number: 10 negative of input number: -10  

Code # 2:

# 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) 

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]]