Python中的 numpy.negative()
当我们想要计算数组元素的负数时,使用numpy.negative()
函数。它返回数组的元素负值或标量的负值。
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 )
代码#1:工作
# 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)
输出 :
Input number : 10
negative of input number : -10
代码#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)
输出 :
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]]