亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

2.如果兩個數(shù)組的維度不同,如(3,1,2)與(2,2),那么維度低的數(shù)組會加一,直到其維度與高維度的相匹配,加一的條件在于(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ù)中,會出現(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.
'''

看的一臉懵,還是跑個代碼來得實(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é)

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

相關(guān)文章

最新評論