記事

Numpy & Scipy - 1.3 行列の基本操作 (1)

Numpy & Scipy - 1.3 行列の基本操作 (1)
Visitors


copy (deep copy)

  • np.copy:deep copy、新しいメモリ空間を参照して割り当て
1
2
3
4
5
6
7
8
9
a = np.array([[1, 2.5, 3], [-1, -2, -1.5], [4, 5.5, 6]], dtype=np.float64)

b = a # swallow copy、同じメモリ空間を参照する。

a[0,0] = 0.0 # こうなると a[0,0], b[0,0] の値がすべて0に変わる。

b = np.copy(a) # deep copy、値が同じ行列またはベクトルが深くコピーされる、完全に別のメモリ空間

prt(a, fmt="%0.2f")



reshape (swallow copy)

  • np.reshape(matrix, number または shape):入力した matrix を希望する 1D vector, 2D matrix 形式に変えて swallow copy を行う。
  • ここで shape は tuple 形式のデータ型である。 (2,3) なら 2x3 matrix
  • number つまり数字一つだけ入力すれば (3,) 1d vector である
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a = np.array([[1, 2.5, 3], [-1, -2, -1.5], [4, 5.5, 6]], dtype=np.float64) 
# 2x3 なのですべての entry が 6個である。

b = np.reshape(a, 6) # 1D array (vector) になる。

print(b)

b = np.reshape(a, (3,2)) # 2D array (matrix) になる。ここで 3x2 = 6 なので entry 個数が同じ。
# もし entry 個数が異なればエラーが発生する。

print(b)

# swallow copy をしたくないなら
# copy を使用して deep copy すればよい。
b = np.copy(np.reshape(a, (3,2))) 
1
2
3
4
5
[1, 2.5, 3, -1, -2, -1.5]

[[1, 2.5]
[3, -1]
[-2, -1.5]]



tril / triu

  • np.tril(matrix, band_id):band_id を含んで lower 部分を deep copy する。lower triangular matrix
  • ここで band_id は default parameter で省略可能
1
2
3
4
5
a = np.array([[1,2,3], [4,5,6], [7,8,9]], dtype=np.float64)

b = np.tril(a)

prt(b, "%0.2f", delimiter=" , ")
1
2
3
 1.00 ,  0.00 ,  0.00
 4.00 ,  5.00 ,  0.00
 7.00 ,  8.00 ,  9.00


  • np.triu(matrix, band_id):band_id を含んで upper 部分を deep copy。upper triangular matrix
1
2
3
4
5
a = np.array([[1,2,3], [4,5,6], [7,8,9]], dtype=np.float64)

b = np.triu(a)

prt(b, "%0.2f", delimiter=" , ")
1
2
3
 1.00 ,  2.00 ,  3.00
 0.00 ,  5.00 ,  6.00
 0.00 ,  0.00 ,  9.00



diag

  • np.diag(matrix, k=band_id):特定の band を抜き出して 1D array (vector) にして swallow copy する関数
1
2
3
4
5
6
7
8
a = np.array([[1,2,3], [4,5,6], [7,8,9]], dtype=np.float64)

b = np.diag(a, k=1)

prt(b, "%0.2f", delimiter=" , ")

b[0] = 0.0 # readonly なのでエラー発生

1
 2.00 ,  6.00

Desktop View


入力によって機能が変わる np.diag

  • np.diag(matrix, k=band_id) で matrix に 1D array を入力すると square matrix を deep copy して返却する。
1
2
3
4
5
6
7
c = np.array([1,2,3,4] , dtype=np.float64) # 1次元配列を入力した時

d = np.diag(c, k=-1) # deep copy である。

c[0] = 0.0

prt(d, "%0.2f", delimiter=" , ")
1
2
3
4
5
 0.00 ,  0.00 ,  0.00 ,  0.00 ,  0.00
 1.00 ,  0.00 ,  0.00 ,  0.00 ,  0.00
 0.00 ,  2.00 ,  0.00 ,  0.00 ,  0.00
 0.00 ,  0.00 ,  3.00 ,  0.00 ,  0.00
 0.00 ,  0.00 ,  0.00 ,  4.00 ,  0.00

Desktop View



diagflat

  • np.diagflat(M, k=band_id):常に square matrix を deep copy して作ってくれる。
  • 注意すべき点は、ここで入力される M を 1D array 化させた後に square matrix を生成する。
  • 1d array -> np.diag と似ている、2d array -> 1d 化した後に square matrix 生成
1
2
3
4
5
M = np.array([[1,3],[2,4]], dtype=np.float64)

e = np.diagflat(M, k = 0)

prt(e, "%0.2f", delimiter=" , ")
1
2
3
4
 1.00 ,  0.00 ,  0.00 ,  0.00
 0.00 ,  3.00 ,  0.00 ,  0.00
 0.00 ,  0.00 ,  2.00 ,  0.00
 0.00 ,  0.00 ,  0.00 ,  4.00

Desktop View Desktop View



trace

  • np.trace:diagonal entry または band entry などを足した値を返す。
1
2
3
4
5
6
7
8
9
10
T = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float64)

val = np.trace(T)

print(val)

val = np.trace(T, offset=-1)  # k ではなく offset

print(val)
1
2
3
15

12

Desktop View



flatten method / ravel

  • flatten:行列 A を 1D array 化して deep copy して返す。
1
2
3
4
5
T = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float64)

flat = T.flatten()

prt(flat, "%0.2f", delimiter=" , ")
1
1.00 ,  2.00 ,  3.00 ,  4.00 ,  5.00 ,  6.00 ,  7.00 ,  8.00 ,  9.00


  • np.ravel:行列 A を 1D array 化して swallow copy して返す
1
2
3
4
5
6
7
T = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float64)

rav = np.ravel(T)

T[0,0] = 0.0

prt(rav, "%0.2f", delimiter=" , ")
1
 0.00 ,  2.00 ,  3.00 ,  4.00 ,  5.00 ,  6.00 ,  7.00 ,  8.00 ,  9.00
この記事は著者の CC BY 4.0 ライセンスの下で提供されています。