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

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

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

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機制,適用于當兩個array的形狀不一樣時,可以通過broadcast進行自動的補齊,從而可以減少使用循環(huán)所帶來的代碼量以及提高效率。

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

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

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

numpy.sum()

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

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ù)中,會出現(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.
'''

看的一臉懵,還是跑個代碼來得實在:

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ū)別在于當' keepdims = True'時,輸出的是2D結(jié)果。

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

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

總結(jié)

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

相關文章

最新評論

盈江县| 巴塘县| 高阳县| 梁平县| 馆陶县| 凤阳县| 丰都县| 永济市| 丰城市| 临朐县| 义马市| 栖霞市| 临高县| 兰溪市| 长子县| 东光县| 鱼台县| 运城市| 河曲县| 湘西| 叶城县| 田东县| 怀柔区| 乌兰县| 曲松县| 石台县| 察隅县| 德安县| 孟连| 政和县| 南木林县| 鄯善县| 仁寿县| 远安县| 鲜城| 谷城县| 西昌市| 广河县| 五河县| 包头市| 积石山|