在 Python 中計算矩陣堆疊的 Moore-Penrose 偽逆


要計算矩陣堆疊的(Moore-Penrose)偽逆,請在 Python 中使用 numpy.linalg.pinv() 方法。使用矩陣的奇異值分解 (SVD) 幷包含所有大的奇異值來計算廣義逆。

第一個引數 a 是要進行偽逆的矩陣或矩陣堆疊。第二個引數 rcodn 是小奇異值的截止值。小於或等於 rcond * 最大奇異值的奇異值將設定為零。針對矩陣堆疊進行廣播。第三個引數 hermitian,如果為 True,則假定 a 為厄米特矩陣,從而能夠使用更有效的方法來查詢奇異值。預設為 False。

步驟

首先,匯入所需的庫。

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)偽逆,請在 Python 中使用 numpy.linalg.pinv() 方法。

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 ]]]

更新於: 2022-2-25

309 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.