numpy の基本 その5

  • np.random.random_integers(lo,hi,size):
    • [lo-hi]間のランダムな整数で初期化した、size(任意の次元数の行列を指定可)の行列を返す
  • np.cumsum(a, axis=None): 行列aの (0, 0) からの累積和を、各要素ごとに計算
    • axis: 0で縦方向に累積、1で縦方向に累積
  • np.mean(a, axis=None): 行列aの全要素の平均値を計算
    • axis: 0で縦方向に平均、1で縦方向に平均
#!/usr/bin/env python
#coding:utf-8

import numpy as np


def work():
    a = np.random.random_integers(-1, 1, (3, 5)) 

    print a
    # [[ 1 -1  0 -1  1]
    #  [-1 -1  0  1  0]
    #  [-1 -1  0  1  0]]
    
    print np.cumsum(a)
    # [ 1  0  0 -1  0 -1 -2 -2 -1 -1 -2 -3 -3 -2 -2]
    
    print np.cumsum(a, axis=0)
    # [[ 1 -1  0 -1  1]
    #  [ 0 -2  0  0  1]
    #  [-1 -3  0  1  1]]
   
    print np.cumsum(a, axis=1)
    # [[ 1  0  0 -1  0]
    #  [-1 -2 -2 -1 -1]
    #  [-1 -2 -2 -1 -1]]

    print np.mean(a)
    # -0.133333333333
    
    print np.mean(a, axis=0)
    # [-0.33333333 -1.          0.          0.33333333  0.33333333]
    
    print np.mean(a, axis=1)
    # [ 0.  -0.2 -0.2]


if __name__ == "__main__":
    work()