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)文章
python爬蟲爬取股票的北上資金持倉數(shù)據(jù)
這篇文章主要介紹了python爬蟲爬取股票的北上資金持倉數(shù)據(jù),文章基于python的相關(guān)資料展開爬取數(shù)據(jù)的詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
Python unittest單元測試框架實(shí)現(xiàn)參數(shù)化
這篇文章主要介紹了Python unittest單元測試框架實(shí)現(xiàn)參數(shù)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
pytorch加載自己的圖像數(shù)據(jù)集實(shí)例
這篇文章主要介紹了pytorch加載自己的圖像數(shù)據(jù)集實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python?+?Selenium?實(shí)現(xiàn)模擬登錄jd實(shí)例分享
這篇文章主要介紹了Python?+?Selenium?實(shí)現(xiàn)模擬登錄jd實(shí)例分享的相關(guān)資料,需要的朋友可以參考下2023-06-06
深入理解Python中pywin32庫實(shí)現(xiàn)Windows自動(dòng)化與系統(tǒng)交互
pywin32是一個(gè)讓?Python?能夠直接調(diào)用?Windows?API?和?COM?對象的擴(kuò)展庫,本文將帶大家從入門到精通,系統(tǒng)性地理解?pywin32?的核心功能、內(nèi)部機(jī)制、典型應(yīng)用場景和高級用法2025-10-10
談一談數(shù)組拼接tf.concat()和np.concatenate()的區(qū)別
今天小編就為大家分享一篇談?wù)剶?shù)組拼接tf.concat()和np.concatenate()的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Numpy數(shù)組轉(zhuǎn)置的兩種實(shí)現(xiàn)方法
下面小編就為大家分享一篇Numpy數(shù)組轉(zhuǎn)置的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
詳解OpenCV中直方圖,掩膜和直方圖均衡化的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了OpenCV中直方圖、掩膜、直方圖均衡化詳細(xì)介紹及代碼的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-11-11

