Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Pythonによる行列変換の実装 | 線形代数の基礎
Pythonによるデータサイエンスのための数学

bookPythonによる行列変換の実装

メニューを表示するにはスワイプしてください

線形方程式系の解法

次の連立方程式を定義する:

2x+y=5xy=12x + y = 5 \\ x - y = 1

これは次のように書き換えられる:

123456789
import numpy as np A = np.array([[2, 1], # Coefficient matrix [1, -1]]) b = np.array([5, 1]) # Constants (RHS) x_solution = np.linalg.solve(A, b) print(x_solution)
copy

これは、両方の方程式を満たす xxyy の値を求める。

重要性:連立方程式の解法はデータサイエンスの基礎であり、線形モデルのフィッティングや最適化制約の解決などに利用される。

線形変換の適用

ベクトルを定義します:

v = np.array([[2], [3]])

次に2つの変換を適用します:

スケーリング

xx を2倍に拡大し、yy を0.5倍に圧縮します:

S = np.array([[2, 0],
              [0, 0.5]])

scaled_v = S @ v

この操作は次のようになります:

Sv=[2000.5][23]=[41.5]S \cdot v = \begin{bmatrix} 2 & 0 \\ 0 & 0.5 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} 4 \\ 1.5 \end{bmatrix}

回転

回転行列を用いてベクトルを90°90°反時計回りに回転させます:

theta = np.pi / 2
R = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta),  np.cos(theta)]])

rotated_v = R @ v

この結果は次の通りです:

Rv=[0110][23]=[32]R \cdot v = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} -3 \\ 2 \end{bmatrix}

変換の可視化

matplotlib を使用して、各ベクトルを原点から描画し、その座標にラベルを付けます:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
import numpy as np import matplotlib.pyplot as plt # Define original vector v = np.array([[2], [3]]) # Scaling S = np.array([[2, 0], [0, 0.5]]) scaled_v = S @ v # Rotation theta = np.pi / 2 R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) rotated_v = R @ v # Vizualization origin = np.zeros(2) fig, ax = plt.subplots(figsize=(6, 6)) # Plot original ax.quiver(*origin, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='blue', label='Original') # Plot scaled ax.quiver(*origin, scaled_v[0], scaled_v[1], angles='xy', scale_units='xy', scale=1, color='green', label='Scaled') # Plot rotated ax.quiver(*origin, rotated_v[0], rotated_v[1], angles='xy', scale_units='xy', scale=1, color='red', label='Rotated') # Label vector tips ax.text(v[0][0]+0.2, v[1][0]+0.2, "(2, 3)", color='blue') ax.text(scaled_v[0][0]+0.2, scaled_v[1][0]+0.2, "(4, 1.5)", color='green') ax.text(rotated_v[0][0]-0.6, rotated_v[1][0]+0.2, "(-3, 2)", color='red') # Draw coordinate axes ax.annotate("", xy=(5, 0), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.annotate("", xy=(0, 5), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.text(5.2, 0, "X", fontsize=12) ax.text(0, 5.2, "Y", fontsize=12) ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_aspect('equal') ax.grid(True) ax.legend() plt.show()
copy

重要性: データサイエンスのワークフローでは、しばしば以下のような変換が含まれます。

  • 主成分分析 - データの回転
  • 特徴量の正規化 - 軸のスケーリング
  • 次元削減 - 射影

ベクトルとその変換を可視化することで、行列がどのようにデータを空間内で移動・変形させるかを理解できます。

question mark

この演算の出力は何ですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  6

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  6
some-alt