numpy の基本
- numpy の関数
- np.arange(bgn, end, step): [bgn, end) 間, step 刻みの値を返す
- np.linspace(bgn, end, nPt): [bgn, end] 間, nPt個の等間隔な値を返す
- np.ones(row, col): row行, col列の、各要素が 1 である行列を返す
- np.eye(n): n行, n列の単位行列を返す
#!/usr/bin/env python import numpy as np def work(): a = np.arange(10) b = np.arange(1.0, 2.5, 0.5) c = np.linspace(0, 1, 5) d = np.linspace(0, 1, 5, endpoint=False) print a # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print b # array([1.0, 1.5, 2.0]) print c # array([0.0, 0.25, 0.5, 0.75, 1.0]) print d # array([0.0, 0.2, 0.4, 0.6, 0.8]) e = np.ones((2, 4)) f = np.ones(5, dtype=np.int) g = np.eye(3) print e # array([[1, 1, 1, 1], # [1, 1, 1, 1]]) print e.dtype # float64 print f # array([1, 1, 1, 1, 1]) print f.dtype # int32 print g # array([[1, 0, 0], # [0, 1, 0], # [0, 0, 1]]) print g.dtype # float64 if __name__ == "__main__": work()
numpyの基本 その2
- numpy.ndarray 型の演算子、メソッド
- a *= 4 (ただし、a は numpy.ndarray型) で、a の各要素を4倍
- a **= 2 (ただし、a は numpy.ndarray型) で、a の各要素を2乗
- np.diag([0,1,2,3]): 対角要素が 0, 1, 2, 3 の 4行4列の行列を返す
- a.reshape(3,4): a を 3行4列 の行列に変える
- a[1:3,2:4] という表現が可能。ただし、この記述の返り値は参照であり、コピーでない点に注意。
#!/usr/bin/env python #coding:utf-8 import numpy as np def work(): a = np.arange(6) b = a*4 + 1 c = a**2 print a # array([0, 1, 2, 3, 4, 5]) print b # array([1, 5, 9, 13, 17, 21]) print c # array([0, 1, 4, 9, 16, 25]) d = np.diag([1,4,2,5,2]) e = np.arange(12).reshape(3,4) f = e[1:3,2:4] print d # array([[1,0,0,0,0], # [0,4,0,0,0], # [0,0,2,0,0], # [0,0,0,4,0], # [0,0,0,0,5]]) print e # array([[0, 1, 2, 3], # [4, 5, 6, 7], # [8, 9,10,11]]) print f # array([[6, 7], # [9,10]]) f[0][0] = 0 print e # f を 変えたのに、e も変わっている点に注意! # # array([[0, 1, 2, 3], # [4, 5, 0, 7], # [8, 9,10,11]]) if __name__ == "__main__": work()
numpy の基本 その3
- work1():
- numpy.ndarray のメソッド
- a.shape: 行列のサイズ(行,列)をタプルで返す
- a.size: 行列の全要素数を返す
- numpy.ndarray のメソッド
- work2():
- 参考URLに記載されていた練習問題の解答
#!/usr/bin/env python #coding:utf-8 import numpy as np def work1(): a = np.arange(12).reshape(3,4) print a.shape # (3,4) print a.size # 12 def work2(): """ 練習問題 :以下の配列を最も単純な解答で作成せよ. [[ 1. 1. 1. 1.] [ 1. 1. 1. 1.] [ 1. 1. 1. 2.] [ 1. 6. 1. 1.]] [[0 0 0 0 0] [2 0 0 0 0] [0 3 0 0 0] [0 0 4 0 0] [0 0 0 5 0] [0 0 0 0 6]] """ a = np.ones((4,4)) a[2][3] = 2 a[3][1] = 6 b = np.zeros((6,5)) b[1:6] = np.diag([2,3,4,5,6]) print a print b if __name__ == "__main__": work1() work2()
numpyの基本 その4
test1.png:
test2.png:
mydraw.png:
- pylab.imsave(filename, image): 画像ファイルに出力する
- pylab.imread(filename):
- 画像ファイルを読み込み、行列データとして返す
- (row行, col列, RGB3要素) の行列データとして返ってくる。(row, col は、画像ファイルの縦横のサイズ)
#!/usr/bin/env python import pylab import numpy as np def work(): image = np.random.rand(30, 30) pylab.imsave("test1.png", image) pylab.imsave("test2.png", image, cmap=pylab.cm.gray) myimage = pylab.imread("mydraw.png") for r in range(myimage.shape[0]): for c in range(myimage.shape[1]): print (int)(myimage[r,c,0]), print "" print "" # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 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1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 # 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 if __name__ == "__main__": work()
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()
numpy の基本 その6
- b = (a % 3 == 0):
- 3で割った余りが0かどうか、各要素に対して判定し、結果を配列で返す
- a[b]:
- 3で割った余りが0である要素のみを返す
- ここで返す値は、*コピー* であり、参照ではない点に注意
#!/usr/bin/env python #coding:utf-8 import numpy as np def work(): a = np.arange(10) b = (a % 3 == 0) c = a[b] print a # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print b # [True, False, False, True, False, False, True, False, False, True] print c # [0, 3, 6, 9] c[::] = -1 print a # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a[b] = -1 print a # [-1, 1, 2, -1, 4, 5, -1, 7, 8, -1] if __name__ == "__main__": work()
numpy の基本その7
- a[::2] += 3: a の行列に対し、2要素置きに3を加算する
- a[ [2,5,1,8] ]: a の行列のうち、 2, 5, 1, 8 番目の要素を返す
- a[ [0,1,1,2], [2,1,3,3] ]
- → (a[0][2], a[1][1], a[1][3], a[2][3])
- a[ [ [0,1], [1,2] ], [ [2,1], [3,3] ] ]
- → ( (a[0][2], a[1][1]), (a[1][3], a[2][3]) )
#!/usr/bin/env python #coding:utf-8 import numpy as np def work(): a = np.arange(10) a[::2] += 3 print a # [3, 1, 5, 3, 7, 5, 9, 7, 11, 9] print a[[2,5,1,8]] # [5, 5, 1, 11] b = np.arange(12).reshape(3,4) print b # [[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9,10,11], # [11,12,13,14]] print b[[0,1,1,2],[2,1,3,3]] # [2, 5, 7, 11] # # -> (b[0][2], b[1][1], b[1][3], b[2][3]) print b[[[0,1],[1,2]],[[2,1],[3,3]]] # [[2, 5], # [7,11]] # # -> ((b[0][2], b[1][1]), # (b[1][3], b[2][3])) if __name__ == "__main__": work()
numpy の基本その8
- サイズの異なる配列同士を足すと、大きいほうの配列にサイズを変換の上、演算を行う
- これをブロードキャストという
#!/usr/bin/env python #coding:utf-8 import numpy as np def work(): a = np.array([[ 0, 1, 2],[ 0, 1, 2],[ 0, 1, 2],[ 0, 1, 2]]) b = np.array([[ 0, 0, 0],[10,10,10],[20,20,20],[30,30,30]]) print a + b # [[ 0 1 2] # [10 11 12] # [20 21 22] # [30 31 32]] a = np.array([ 0, 1, 2]) b = np.array([[ 0, 0, 0],[10,10,10],[20,20,20],[30,30,30]]) print a + b # [[ 0 1 2] # [10 11 12] # [20 21 22] # [30 31 32]] a = np.array([ 0, 1, 2]) b = np.array([[0],[10],[20],[30]]) print a + b # [[ 0 1 2] # [10 11 12] # [20 21 22] # [30 31 32]] # 5 * 5 の格子点の原点からの距離を計算 x = np.arange(5) y = np.arange(5).reshape(5,1) print np.sqrt(x**2 + y**2) # [[ 0. 1. 2. 3. 4. ] # [ 1. 1.41421356 2.23606798 3.16227766 4.12310563] # [ 2. 2.23606798 2.82842712 3.60555128 4.47213595] # [ 3. 3.16227766 3.60555128 4.24264069 5. ] # [ 4. 4.12310563 4.47213595 5. 5.65685425]] # 中心(25, 12)、半径 10 の円の範囲を0で塗りつぶす mat = np.ones((30, 30), dtype=np.int) x = np.arange(30) y = np.arange(30).reshape(30,1) mask = np.sqrt((x-25)**2 + (y-12)**2) < 10 mat[mask] = 0 print mat # [[1 1 1 1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