久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解

瀏覽:115日期:2022-06-24 13:30:12

為了區(qū)分三種乘法運(yùn)算的規(guī)則,具體分析如下:

import numpy as np1. np.multiply()函數(shù)

函數(shù)作用

數(shù)組和矩陣對(duì)應(yīng)位置相乘,輸出與相乘數(shù)組/矩陣的大小一致

1.1數(shù)組場景

A = np.arange(1,5).reshape(2,2)A

array([[1, 2], [3, 4]])

B = np.arange(0,4).reshape(2,2)B

array([[0, 1], [2, 3]])

np.multiply(A,B) #數(shù)組對(duì)應(yīng)元素位置相乘

array([[ 0, 2], [ 6, 12]])

1.2 矩陣場景

np.multiply(np.mat(A),np.mat(B)) #矩陣對(duì)應(yīng)元素位置相乘,利用np.mat()將數(shù)組轉(zhuǎn)換為矩陣

matrix([[ 0, 2],[ 6, 12]])

np.sum(np.multiply(np.mat(A),np.mat(B))) #輸出為標(biāo)量

20

2. np.dot()函數(shù)

函數(shù)作用

對(duì)于秩為1的數(shù)組,執(zhí)行對(duì)應(yīng)位置相乘,然后再相加;

對(duì)于秩不為1的二維數(shù)組,執(zhí)行矩陣乘法運(yùn)算;超過二維的可以參考numpy庫介紹。

2.1 數(shù)組場景

2.1.1 數(shù)組秩不為1的場景

A = np.arange(1,5).reshape(2,2)A

array([[1, 2], [3, 4]])

B = np.arange(0,4).reshape(2,2)B

array([[0, 1], [2, 3]])

np.dot(A,B) #對(duì)數(shù)組執(zhí)行矩陣相乘運(yùn)算

array([[ 4, 7], [ 8, 15]])

2.1.2 數(shù)組秩為1的場景

C = np.arange(1,4)C

array([1, 2, 3])

D = np.arange(0,3)D

array([0, 1, 2])

np.dot(C,D) #對(duì)應(yīng)位置相乘,再求和

8

2.2 矩陣場景

np.dot(np.mat(A),np.mat(B)) #執(zhí)行矩陣乘法運(yùn)算

matrix([[ 4, 7],[ 8, 15]])

3. 星號(hào)(*)乘法運(yùn)算

作用

對(duì)數(shù)組執(zhí)行對(duì)應(yīng)位置相乘

對(duì)矩陣執(zhí)行矩陣乘法運(yùn)算

3.1 數(shù)組場景

A = np.arange(1,5).reshape(2,2)A

array([[1, 2], [3, 4]])

B = np.arange(0,4).reshape(2,2)B

array([[0, 1], [2, 3]])

A*B #對(duì)應(yīng)位置點(diǎn)乘

array([[ 0, 2], [ 6, 12]])

3.2矩陣場景

(np.mat(A))*(np.mat(B)) #執(zhí)行矩陣運(yùn)算

matrix([[ 4, 7],[ 8, 15]])

到此這篇關(guān)于python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解的文章就介紹到這了,更多相關(guān)python np.multiply()、np.dot()和星號(hào)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 司法| 博湖县| 原阳县| 上饶市| 湘西| 昔阳县| 扬州市| 江陵县| 上饶县| 孝感市| 淳安县| 星子县| 瑞安市| 彝良县| 鹤山市| 辽阳县| 凤翔县| 阿鲁科尔沁旗| 南城县| 花莲县| 通许县| 大英县| 西盟| 普兰店市| 宾川县| 沙田区| 鄂尔多斯市| 板桥市| 航空| 晋城| 晋中市| 大化| 罗源县| 木兰县| 宣城市| 合阳县| 眉山市| 友谊县| 邵阳县| 苏州市| 景洪市|