Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คำนวณ pseudoinverse ของ Moore-Penrose ของ stack ของเมทริกซ์ใน Python


ในการคำนวณการผกผันเทียม (Moore-Penrose) ของสแต็กของเมทริกซ์ ให้ใช้เมธอด numpy.linalg.pinv() ใน Python คำนวณค่าผกผันทั่วไปของเมทริกซ์โดยใช้การสลายตัวของค่าเอกพจน์ (SVD) และรวมค่าเอกพจน์ขนาดใหญ่ทั้งหมดด้วย

พารามิเตอร์ตัวที่ 1 a คือเมทริกซ์หรือสแต็กของเมทริกซ์ที่จะกลับค่าเทียม พารามิเตอร์ตัวที่ 2 rcodn เป็นตัวตัดสำหรับค่าเอกพจน์ขนาดเล็ก ค่าเอกพจน์ที่น้อยกว่าหรือเท่ากับ rcond * maximum_singular_value ถูกตั้งค่าเป็นศูนย์ ออกอากาศเทียบกับสแต็กของเมทริกซ์ พารามิเตอร์ตัวที่ 3 คือ Hermitian หากเป็น True จะถือว่า a เป็น Hermitian ซึ่งช่วยให้สามารถค้นหาค่าเอกพจน์ได้อย่างมีประสิทธิภาพมากขึ้น ค่าเริ่มต้นเป็นเท็จ

ขั้นตอน

ขั้นแรก ให้นำเข้าไลบรารีที่จำเป็น

import numpy as np

สร้างอาร์เรย์โดยใช้ array()

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)

ในการคำนวณการผกผันเทียม (Moore-Penrose) ของสแต็กของเมทริกซ์ ให้ใช้เมธอด numpy.linalg.pinv() ใน Python

print("\nResult...\n",np.linalg.pinv(arr))

ตัวอย่าง

import numpy as np

# Create an array using array()
arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# To Compute the (Moore-Penrose) pseudo-inverse of a stack of matrices, use the numpy.linalg.pinv() method in Python.
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 ]]]