python numpy ufunc.reduce(self, a, axis=0, dtype=None, out=None, keepdims=False)函数.(连续执行原始运算对值聚合)
生活随笔
收集整理的這篇文章主要介紹了
python numpy ufunc.reduce(self, a, axis=0, dtype=None, out=None, keepdims=False)函数.(连续执行原始运算对值聚合)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
def reduce(self, a, axis=0, dtype=None, out=None, keepdims=False): # real signature unknown; restored from __doc__"""reduce(a, axis=0, dtype=None, out=None, keepdims=False)Reduces `a`'s dimension by one, by applying ufunc along one axis.通過沿一個(gè)軸應(yīng)用ufunc將`a`的尺寸減小一倍。Let :math:`a.shape = (N_0, ..., N_i, ..., N_{M-1})`. Then:math:`ufunc.reduce(a, axis=i)[k_0, ..,k_{i-1}, k_{i+1}, .., k_{M-1}]` =the result of iterating `j` over :math:`range(N_i)`, cumulatively applyingufunc to each :math:`a[k_0, ..,k_{i-1}, j, k_{i+1}, .., k_{M-1}]`.For a one-dimensional array, reduce produces results equivalent to:::令a.shape =(N_0,...,N_i,...,N_ {M-1})`。 然后:ufunc.reduce(a,axis = i)[k_0,..,k_ {i-1},k_ {i + 1},..,k_ {M-1}]`=的結(jié)果在:math:`range(N_i)`上迭代`j`,將ufunc累積應(yīng)用于每個(gè):math:`a [k_0,..,k_ {i-1},j,k_ {i + 1},.., k_ {M-1}]`。 對于一維數(shù)組,reduce產(chǎn)生的結(jié)果等于:::r = op.identity # op = ufuncfor i in range(len(A)):r = op(r, A[i])return rFor example, add.reduce() is equivalent to sum().例如,add.reduce()等同于sum()。Parameters----------a : array_likeThe array to act on.要作用的數(shù)組。axis : None or int or tuple of ints, optionalAxis or axes along which a reduction is performed.The default (`axis` = 0) is perform a reduction over the firstdimension of the input array. `axis` may be negative, inwhich case it counts from the last to the first axis.進(jìn)行縮小的一個(gè)或多個(gè)軸。 默認(rèn)值(“ axis” = 0)是對輸入數(shù)組的第一維進(jìn)行縮減。 “軸”可能為負(fù),在這種情況下,它從最后一個(gè)軸開始計(jì)數(shù)。.. versionadded:: 1.7.0If this is `None`, a reduction is performed over all the axes.If this is a tuple of ints, a reduction is performed on multipleaxes, instead of a single axis or all the axes as before.如果為“無”,則對所有軸進(jìn)行歸約。 如果這是一個(gè)整數(shù)元組,則在多個(gè)軸上執(zhí)行歸約,而不是像以前那樣在單個(gè)軸或所有軸上執(zhí)行歸約。For operations which are either not commutative or not associative,doing a reduction over multiple axes is not well-defined. Theufuncs do not currently raise an exception in this case, but willlikely do so in the future.對于非交換或非關(guān)聯(lián)運(yùn)算,在多個(gè)軸上進(jìn)行歸約的定義不明確。 在這種情況下,ufunc當(dāng)前不會引發(fā)異常,但將來可能會引發(fā)異常。dtype : data-type code, optionalThe type used to represent the intermediate results. Defaultsto the data-type of the output array if this is provided, orthe data-type of the input array if no output array is provided.用于表示中間結(jié)果的類型。 如果提供此參數(shù),則默認(rèn)為輸出數(shù)組的數(shù)據(jù)類型;如果不提供輸出數(shù)組,則默認(rèn)為輸入數(shù)組的數(shù)據(jù)類型。out : ndarray, None, or tuple of ndarray and None, optionalA location into which the result is stored. If not provided or `None`,a freshly-allocated array is returned. For consistency with:ref:`ufunc.__call__`, if given as a keyword, this may be wrapped in a1-element tuple.結(jié)果存儲的位置。 如果未提供或沒有,則返回一個(gè)新分配的數(shù)組。 為了與ufunc .__ call__保持一致,如果作為關(guān)鍵字給出,則可以將其包裝在1元素元組中。.. versionchanged:: 1.13.0Tuples are allowed for keyword argument.keepdims : bool, optionalIf this is set to True, the axes which are reduced are leftin the result as dimensions with size one. With this option,the result will broadcast correctly against the original `arr`.如果將其設(shè)置為True,則縮小的軸將保留為尺寸1的尺寸。 使用此選項(xiàng),結(jié)果將針對原始`arr`正確廣播。.. versionadded:: 1.7.0Returns-------r : ndarrayThe reduced array. If `out` was supplied, `r` is a reference to it.精簡數(shù)組。 如果提供了`out`,則`r`是對它的引用。Examples-------->>> np.multiply.reduce([2,3,5])30A multi-dimensional array example:>>> X = np.arange(8).reshape((2,2,2))>>> Xarray([[[0, 1],[2, 3]],[[4, 5],[6, 7]]])>>> np.add.reduce(X, 0) # 次外層相加array([[ 4, 6],[ 8, 10]])>>> np.add.reduce(X) # confirm: default axis value is 0 如果不指定軸,則默認(rèn)為0(次外層)array([[ 4, 6],[ 8, 10]])>>> np.add.reduce(X, 1)array([[ 2, 4],[10, 12]])>>> np.add.reduce(X, 2)array([[ 1, 5],[ 9, 13]])"""pass
參考文章:numpy高級應(yīng)用—ufunc
總結(jié)
以上是生活随笔為你收集整理的python numpy ufunc.reduce(self, a, axis=0, dtype=None, out=None, keepdims=False)函数.(连续执行原始运算对值聚合)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python numpy Broadca
- 下一篇: python numpy np.argm