오늘/오늘의 함수

[numpy] np.ufunc.accumulate

hwijin97 2021. 12. 8. 19:40

https://numpy.org/doc/stable/reference/ufuncs.html#:~:text=Math-,operations,-%C2%B6

 

Universal functions (ufunc) — NumPy v1.21 Manual

Methods All ufuncs have four methods. However, these methods only make sense on scalar ufuncs that take two input arguments and return one output argument. Attempting to call these methods on other ufuncs will cause a ValueError. The reduce-like methods al

numpy.org

Params :

array : array_like

axis : int, optional , default=0

dtype : data-type , optional

out : ndarray, optional

 

Return :

r : ndarray

 

설명 :

배열을 축기준으로 순회하면서 ufunc을 수행한다. dtype을 지정하거나 out 으로 저장할 배열을 지정가능하다.

아래와 같이 배열을 순회하면서 ufunc을 수행한다. 인자가 2개있는 함수만 가능.

r = np.empty(len(A))
t = op.identity        # op = the ufunc being applied to A's  elements
for i in range(len(A)):
    t = op(t, A[i])
    r[i] = t
return r

ex ) maximum, add

A = np.arange(5)
np.random.shuffle(A)
#array([1, 3, 0, 2, 4])
np.maximum.accumulate(A)
array([1, 3, 3, 3, 4])
np.add.accumulate(A)
#array([ 1,  4,  4,  6, 10]) == np.cumsum