最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python之broadcast和numpy.sum()函數(shù)用法及說明

 更新時(shí)間:2023年06月14日 09:43:23   作者:ImposterSyndrome  
這篇文章主要介紹了python之broadcast和numpy.sum()函數(shù)用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python broadcast和numpy.sum()函數(shù)

import numpy as np
a = np.random.random_sample((3,1,3))
b  = np.random.random_sample((2,3))
c = a-b
c = np.square(c)
c = np.sum(c,axis=2)
c= np.sqrt(c)
a1 = a[1,:,:]
b1 = b[1,:]
print(a1,'5')
print(b1,'6')
print(np.square(a1-b1).shape)
print(np.sum(np.square(a1-b1),axis=1),'7')
print(np.sqrt(np.sum(np.square(a1-b1),axis=1)))

python 的broadcast機(jī)制,適用于當(dāng)兩個(gè)array的形狀不一樣時(shí),可以通過broadcast進(jìn)行自動(dòng)的補(bǔ)齊,從而可以減少使用循環(huán)所帶來的代碼量以及提高效率。

它的補(bǔ)齊規(guī)則如下:

1.如果兩個(gè)數(shù)組數(shù)據(jù)維度相同,如(3,1,2)與(1,2,2),且其中某個(gè)維度的rank是1,那么會(huì)將rank低的數(shù)據(jù)進(jìn)行復(fù)制,直到兩個(gè)數(shù)組的維度以及rank均相同

2.如果兩個(gè)數(shù)組的維度不同,如(3,1,2)與(2,2),那么維度低的數(shù)組會(huì)加一,直到其維度與高維度的相匹配,加一的條件在于(1,2)與(2,2)可以進(jìn)行broadcast,與情況一相同

numpy.sum()

  • sum()函數(shù)參數(shù)為numpy.sum(a, axis = )
  • axis代表相加的軸,初始從0開始
  • axis = i,則代表從維度i進(jìn)行累加,其他維度不變

a.shape = (1,2,3,4)

numpy.sum(a,axis = 0).shape = (2,3,4)
numpy .sum(a, axis =1).shape = (1,3,4)

numpy-numpy.sum()中‘keepdims‘參數(shù)的作用

在numpy的許多函數(shù)中,會(huì)出現(xiàn)'keepdims'參數(shù),以numpy.sum()為例:

官方文檔中給出的解釋:

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
'''
keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class' method does not implement keepdims any exceptions will be raised.
'''

看的一臉懵,還是跑個(gè)代碼來得實(shí)在:

a = np.array([[0, 0, 0],
       [0, 1, 0],
       [0, 2, 0],
       [1, 0, 0],
       [1, 1, 0]])
print(a)
'''
輸出:
[[0 0 0]
 [0 1 0]
 [0 2 0]
 [1 0 0]
 [1 1 0]]
'''
a_sum_true = np.sum(a, keepdims=True)
print(a_sum_true)
print(a_sum_true.shape)
a_sum_false = np.sum(a, keepdims=False)
print(a_sum_false)
print(a_sum_false.shape)
'''
輸出:
[[6]]
(1, 1)
6
()
'''
a_sum_axis1_true = np.sum(a, axis=1, keepdims=True)
print(a_sum_axis1_true)
print(a_sum_axis1_true.shape)
a_sum_axis1_false = np.sum(a, axis=1, keepdims=False)
print(a_sum_axis1_false)
print(a_sum_axis1_false.shape)
'''
輸出:
[[0]
 [1]
 [2]
 [1]
 [2]]
(5, 1)
[0 1 2 1 2]
(5,)
'''
a_sum_axis0_true = np.sum(a, axis=0, keepdims=True)
print(a_sum_axis0_true)
print(a_sum_axis0_true.shape)
a_sum_axis0_false = np.sum(a, axis=0, keepdims=False)
print(a_sum_axis0_false)
print(a_sum_axis0_false.shape)
'''
輸出:
[[2 4 0]]
(1, 3)
[2 4 0]
(3,)
'''

如果并不指定'axis'參數(shù),輸出的結(jié)果是相同的,區(qū)別在于當(dāng)' keepdims = True'時(shí),輸出的是2D結(jié)果。

如果指定'axis'參數(shù),輸出的結(jié)果也是相同的,區(qū)別在于'keepdims = True'時(shí),輸出的是2D結(jié)果。

可以理解為'keepdims = True'參數(shù)是為了保持結(jié)果的維度與原始array相同。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

响水县| 衡山县| 台州市| 武夷山市| 舞阳县| 云南省| 报价| 古丈县| 古丈县| 阿拉善右旗| 寿光市| 富裕县| 卫辉市| 通化县| 巨鹿县| 韶山市| 天津市| 阳东县| 寿宁县| 即墨市| 城口县| 石河子市| 西贡区| 兴隆县| 铁岭县| 武宁县| 雅江县| 郁南县| 淄博市| 微博| 两当县| 建始县| 腾冲县| 关岭| 若羌县| 墨脱县| 昆明市| 塔城市| 松溪县| 彭阳县| 枝江市|