當我們要計算數組元素的負數時,使用numpy.negative()函數。它返回數組的按元素的負值或標量的負值。
用法: numpy.negative(arr, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘negative’)
參數:
arr :[數組或標量]輸入數組。
dtype :返回數組的類型。默認情況下,使用arr的dtype。 out:[ndarray,可選]將結果存儲到的位置。 ->如果提供,則必須具有廣播輸入的形狀。 ->如果未提供或沒有,則返回新分配的數組。其中:[數組,可選]值為True表示要在該位置計算ufunc,值為False表示將值保留在輸出中。 ** kwargs:允許將參數的關鍵字可變長度參數傳遞給函數。當我們要處理函數中的命名參數時使用。
Return :[ndarray或標量]返回的數組或標量=-(輸入arr或標量)
代碼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]]
相關用法
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy.negative() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。