Python
numpy

numpyの基本

配列の基本

配列の基本属性

# -*- coding: UTF-8 -*-

import numpy as np
a = np.array([[1,2,3]
             ,[4,5,6]])
print(a)
print(a.shape) # 構造:shape ⇒ (2,3)
print(a.ndim) # 次元数:ndim ⇒ 2
print(a.size) # サイズ:size ⇒ 6
print(a.dtype) # データ型:dtype ⇒ int64

実行結果

[[1 2 3]
 [4 5 6]]
(2, 3)
2
6
int64

配列の新規

基本

# -*- coding: UTF-8 -*-
import numpy as np
a = np.array([[1,2,3]
             ,[4,5,6]])
print(a)

実行結果

[[1 2 3]
 [4 5 6]]

arange(),reshape()

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(6)
print(a)

print('***************************')
b = np.arange(12).reshape(3,4)
print(b)

print('***************************')
c = np.arange(24).reshape(2,3,4)
print(c)

実行結果

[0 1 2 3 4 5]
***************************
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
***************************
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

データ型の指定

# -*- coding: UTF-8 -*-
import numpy as np
a = np.array([[1,2,3]
             ,[4,5,6]], dtype=complex) # typeの指定
print(a)

実行結果

[[ 1.+0.j  2.+0.j  3.+0.j]
 [ 4.+0.j  5.+0.j  6.+0.j]]

要素の初期値の指定(0,1,random)

# -*- coding: UTF-8 -*-
import numpy as np

# 要素が0の配列の新規
array_zeros = np.zeros( (3,4) )
print(array_zeros)

print('********')
# 要素が1の配列の新規
array_ones = np.ones( (2,3,4) ) 
print(array_ones)

print('********')
# 要素がrandomの配列の新規
array_random = np.empty( (2,3) )
print(array_random)

実行結果

[[ 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.]]]
********
[[  6.92641146e-310   6.92641146e-310   6.32233921e-307]
 [  1.15710115e-306   4.18403666e+149  -4.17465261e+149]]

配列の計算

基本

# -*- coding: UTF-8 -*-
import numpy as np

a = np.array( [10,20,30,40] )
b = np.array( [0, 1, 2, 3] )

c = a - b
print(c) # ⇒ [10 19 28 37]

print(b**2) # ⇒ [0 1 4 9]

print(np.sin(a)) # ⇒ [-0.54402111  0.91294525 -0.98803162  0.74511316]

print(a < 25) # ⇒ [ True  True False False]

print(a * b) # ⇒ [  0  20  60 120]

実行結果

[10 19 28 37]
[0 1 4 9]
[-0.54402111  0.91294525 -0.98803162  0.74511316]
[ True  True False False]
[  0  20  60 120]

軸(axis)
二次元例

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(12).reshape(3,4)

print(a)
# 0 1 2 3
# 4 5 6 7
# 8 9 10 11

print('*********************')
print(a.sum(axis=0)) # 軸0 ⇒ shapeの3で


print('*********************')
print(a.sum(axis=1)) # 軸1 ⇒  shapeの4で

実行結果

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
*********************
[12 15 18 21]
*********************
[ 6 22 38]

三次元例

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(24).reshape(2,3,4)

print(a)

print('*********************')
print(a.sum(axis=0)) # 軸0 ⇒ shapeの2で

print('*********************')
print(a.sum(axis=1)) # 軸1 ⇒  shapeの3で

print('*********************')
print(a.sum(axis=2)) # 軸2 ⇒  shapeの4で

実行結果

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
*********************
[[12 14 16 18]
 [20 22 24 26]
 [28 30 32 34]]
*********************
[[12 15 18 21]
 [48 51 54 57]]
*********************
[[ 6 22 38]
 [54 70 86]]

配列の選択例

一次元

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(10)**2
print(a)
# ⇒ [ 0  1  4  9 16 25 36 49 64 81]

print(a[3])
# ⇒ 9

print(a[3:7])
# ⇒ [ 9 16 25 36]

print(a[::-1])
# 逆順にする ⇒ [81 64 49 36 25 16  9  4  1  0]

print(a**(1/2))
# ⇒ [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

実行結果

[ 0  1  4  9 16 25 36 49 64 81]
9
[ 9 16 25 36]
[81 64 49 36 25 16  9  4  1  0]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

二次元

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(12).reshape(3,4)
print(a)
# ⇒ [[[ 0  1  2  3]
# ⇒ [[ 4  5  6  7]
# ⇒ [ [ 8  9 10 11]]

print(a[1,2])
# ⇒ 6

print(a[0:2,3])
# ⇒ [3 7]

print(a[:,3])
# ⇒ [ 3  7 11]

実行結果

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
6
[3 7]
[ 3  7 11]

配列の繰り返す

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(6).reshape(2,3)
print(a)

print('***********************')

for raw in a:
  print(raw) # forで ⇒ 軸0で繰り返す

print('***********************')

for element in a.flat:
  print(element) # flatで ⇒ 全要素を繰り返す

実行結果

[[0 1 2]
 [3 4 5]]
***********************
[0 1 2]
[3 4 5]
***********************
0
1
2
3
4
5

配列のreshape()とresize()

# -*- coding: UTF-8 -*-
import numpy as np

a = np.arange(12).reshape(3,4)
print(a)

c = a.reshape(1,12)
print(c) # 1*12
print(a) # 3*4 ⇒ reshapeでaが変わらない

a.resize(2,6)
print(a) # 2*6 ⇒ resizeでaが変更

実行結果

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3  4  5  6  7  8  9 10 11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]

配列の組み合わせ

# -*- coding: UTF-8 -*-
import numpy as np

a = np.floor(10*np.random.random((3,3)))
print(a)

b = np.floor(10*np.random.random((3,3)))
print(b)

# vstack
print(np.vstack((a,b)))

# hstack
print(np.hstack((a,b)))

実行結果

[[ 8.  0.  4.]
 [ 6.  9.  9.]
 [ 6.  0.  1.]]
[[ 8.  2.  2.]
 [ 2.  3.  3.]
 [ 4.  5.  8.]]
[[ 8.  0.  4.]
 [ 6.  9.  9.]
 [ 6.  0.  1.]
 [ 8.  2.  2.]
 [ 2.  3.  3.]
 [ 4.  5.  8.]]
[[ 8.  0.  4.  8.  2.  2.]
 [ 6.  9.  9.  2.  3.  3.]
 [ 6.  0.  1.  4.  5.  8.]]

参考公式:

https://docs.scipy.org/doc/numpy/reference/routines.html