日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python a and b_python-尽管Numpy建议a.b,为什么a.dot(b)比a @ b更...

發(fā)布時(shí)間:2023/12/2 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python a and b_python-尽管Numpy建议a.b,为什么a.dot(b)比a @ b更... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

根據(jù)question的答案,也根據(jù)numpy的答案,與a.dot(b)相比,二維數(shù)組的矩陣乘法最好通過a b或numpy.matmul(a,b)完成.

If both a and b are 2-D arrays, it is matrix multiplication, but using

matmul or a @ b is preferred.

我做了以下基準(zhǔn)測(cè)試,發(fā)現(xiàn)相反的結(jié)果.

問題:我的基準(zhǔn)測(cè)試有問題嗎?如果不是,當(dāng)Numpy比a @ b或numpy.matmul(a,b)快時(shí),為什么Numpy不推薦a.dot(b)?

基準(zhǔn)使用python 3.5 numpy 1.15.0.

$pip3 list | grep numpy

numpy 1.15.0

$python3 --version

Python 3.5.2

基準(zhǔn)代碼:

import timeit

setup = '''

import numpy as np

a = np.arange(16).reshape(4,4)

b = np.arange(16).reshape(4,4)

'''

test = '''

for i in range(1000):

a @ b

'''

test1 = '''

for i in range(1000):

np.matmul(a,b)

'''

test2 = '''

for i in range(1000):

a.dot(b)

'''

print( timeit.timeit(test, setup, number=100) )

print( timeit.timeit(test1, setup, number=100) )

print( timeit.timeit(test2, setup, number=100) )

結(jié)果:

test : 0.11132473500038031

test1 : 0.10812476599676302

test2 : 0.06115105600474635

附加結(jié)果:

>>> a = np.arange(16).reshape(4,4)

>>> b = np.arange(16).reshape(4,4)

>>> a@b

array([[ 56, 62, 68, 74],

[152, 174, 196, 218],

[248, 286, 324, 362],

[344, 398, 452, 506]])

>>> np.matmul(a,b)

array([[ 56, 62, 68, 74],

[152, 174, 196, 218],

[248, 286, 324, 362],

[344, 398, 452, 506]])

>>> a.dot(b)

array([[ 56, 62, 68, 74],

[152, 174, 196, 218],

[248, 286, 324, 362],

[344, 398, 452, 506]])

總結(jié)

以上是生活随笔為你收集整理的python a and b_python-尽管Numpy建议a.b,为什么a.dot(b)比a @ b更...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。