เพื่อคำนวณ pseudo-inverse แบบ Moore-Penrose ของสแต็กเมทริกซ์ (stack of matrices) ให้ใช้ numpy.linalg.pinv() คำนวณ generalized inverse ผ่าน singular-value decomposition (SVD) รวมค่า singular values ที่ใหญ่ๆ
พารามิเตอร์สำคัญ
- a: เมทริกซ์หรือสแต็กเมทริกซ์ที่ต้องการ pseudo-invert
- rcond: cutoff สำหรับ singular values เล็ก ค่าที่ ≤ rcond * largest_singular_value จะเป็นศูนย์
- hermitian: ถ้า True จะถือว่า a เป็น Hermitian เพื่อวิธีคำนวณที่มีประสิทธิภาพมากขึ้น
ขั้นตอนการใช้งาน
import numpy as np
สร้างสแต็กเมทริกซ์ 3 ตัว 2x2
arr = np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]])
print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)
print("\nShape of our Array object...\n", arr.shape)
คำนวณ pseudo-inverse
print("\nResult...\n", np.linalg.pinv(arr))
โค้ดตัวอย่างสมบูรณ์
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]])
print("Our Array...\n", arr)
print("\nDimensions of our Array...\n", arr.ndim)
print("\nDatatype of our Array object...\n", arr.dtype)
print("\nShape of our Array object...\n", arr.shape)
print("\nResult...\n", np.linalg.pinv(arr))
ผลลัพธ์ที่คาดหวัง
Our Array... [[[1 2] [3 4]] [[1 2] [2 1]] [[1 3] [3 1]]] Dimensions of our Array... 3 Datatype of our Array object... int64 Shape of our Array object... (3, 2, 2) Result... [[[-2. 1. ] [ 1.5 -0.5 ]] [[-0.33333333 0.66666667] [ 0.66666667 -0.33333333]] [[-0.125 0.375 ] [ 0.375 -0.125 ]]]