當我們要計算兩個數組的乘法時,使用numpy.multiply()函數。它按元素返回arr1和arr2的乘積。
用法: numpy.multiply(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘multiply’)
參數:
arr1 :[數組或標量] 1st輸入數組。
arr2 :[數組或標量] 2nd輸入數組。
dtype :返回數組的類型。默認情況下,使用arr的dtype。 out:[ndarray,可選]將結果存儲到的位置。 ->如果提供,則必須具有廣播輸入的形狀。 ->如果未提供或沒有,則返回新分配的數組。其中:[數組,可選]值為True表示要在該位置計算ufunc,值為False表示將值保留在輸出中。 ** kwargs:允許將參數的關鍵字可變長度參數傳遞給函數。當我們要處理函數中的命名參數時使用。
Return :[ndarray或標量] arr1和arr2的乘積,元素方式。
代碼1:
# Python program explaining
# numpy.multiply() function
import numpy as geek
in_num1 = 4
in_num2 = 6
print ("1st Input number:", in_num1)
print ("2nd Input number:", in_num2)
out_num = geek.multiply(in_num1, in_num2)
print ("output number:", out_num)
輸out :
1st Input number: 4 2nd Input number: 6 output number: 24
代碼2:
# Python program explaining
# numpy.multiply() function
import numpy as geek
in_arr1 = geek.array([[2, -7, 5], [-6, 2, 0]])
in_arr2 = geek.array([[0, -7, 8], [5, -2, 9]])
print ("1st Input array:", in_arr1)
print ("2nd Input array:", in_arr2)
out_arr = geek.multiply(in_arr1, in_arr2)
print ("Resultant output array:", out_arr)
輸out :
1st Input array: [[ 2 -7 5] [-6 2 0]] 2nd Input array: [[ 0 -7 8] [ 5 -2 9]] Resultant output array: [[ 0 49 40] [-30 -4 0]]
相關用法
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy.multiply() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。