เพื่อคำนวณ pseudo-inverse แบบ Moore-Penrose ของเมทริกซ์ ให้ใช้ numpy.linalg.pinv() คำนวณ generalized inverse ผ่าน SVD รวม singular values ที่ใหญ่ พารามิเตอร์ rcond ควบคุม cutoff ของ singular values เล็ก
ขั้นตอนการใช้งาน
import numpy as np
สร้างเมทริกซ์สุ่ม 9x6 และตรวจสอบคุณสมบัติ
arr = np.random.randn(9, 6)
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.random.randn(9, 6)
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... [[ 2.14644893 -0.14757929 ... ] ... [ 0.99636776 0.83682256 ... ]] Dimensions of our Array... 2 Datatype of our Array object... float64 Shape of our Array object... (9, 6) Result... [[ 0.19229685 -0.03266066 ... ] ... [-0.13235983 0.21188986 ... ]]