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機(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)文章
局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送
這篇文章主要介紹了局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04urllib和BeautifulSoup爬取維基百科的詞條簡單實(shí)例
這篇文章主要介紹了urllib和BeautifulSoup爬取維基百科的詞條簡單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python使用unicodedata實(shí)現(xiàn)字符串標(biāo)準(zhǔn)化
這篇文章主要來和大家聊一聊 Python 的一個(gè)內(nèi)置模塊:unicodedata,它是專門用來處理 unicode 字符串的,下面就一起來看看它的用法吧2023-06-06Django零基礎(chǔ)入門之運(yùn)行Django版的hello world
這篇文章主要介紹了Django零基礎(chǔ)入門之運(yùn)行Django版的hello world,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09一文帶你精通Python中exec函數(shù)的高級(jí)技巧
在?Python?中,exec?是一個(gè)內(nèi)置函數(shù),允許在運(yùn)行時(shí)動(dòng)態(tài)執(zhí)行?Python?代碼,本文將詳細(xì)介紹?Python?exec?函數(shù)的高級(jí)用法,包括動(dòng)態(tài)代碼生成、執(zhí)行外部文件等內(nèi)容,希望對(duì)大家有所幫助2023-11-11python內(nèi)置模塊OS?實(shí)現(xiàn)SHELL端文件處理器
這篇文章主要介紹了python內(nèi)置模塊OS實(shí)現(xiàn)SHELL端文件處理器,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09