20
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

numpy cumsum()

Posted at

numpyのcumsum()関数について

##処理内容
要素を足し合わせたものを、配列として出力する。

###サンプルコード

a = np.array([1,2,3,4,5,6])
#下記どちらの書き方でもOK
np.cumsum(a)
a.cumsum()

###実行結果

>>>array([ 1,  3,  6, 10, 15, 21])
>>>array([ 1,  3,  6, 10, 15, 21])

実行結果は同じ。
ちなみにaxisで加算方向を変更できる。

a = np.array([1,2,3,4,5,6]).reshape(2,3)
print(a)
>>>array([[1, 2, 3],
          [4, 5, 6]])

np.cumsum(a, axis=0)
>>>array([[1, 2, 3],
          [5, 7, 9]])

np.cumsum(a, axis=1)
>>>array([[ 1,  3,  6],
          [ 4,  9, 15]])

#ちなみに多次元のarrayに対してaxis指定せずにcumsum()実行すると、、、、
np.cumsum(a)
>>>array([ 1,  3,  6, 10, 15, 21])

[参考]
SciPy.orgドキュメント

20
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?