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


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

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

步驟

首先,匯入所需的庫 -

import numpy as np

建立陣列並使用 randn() 填充隨機值 -

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)

要計算矩陣的(Moore-Penrose)偽逆,請使用 numpy.linalg.pinv() 方法 -

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

示例

import numpy as np

# Create an array and fill with random values using randn()
arr = np.random.randn(9, 6)

# 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 matrix, use the numpy.linalg.pinv() method in Python.
print("\nResult...\n",np.linalg.pinv(arr))

輸出

Our Array...
[[ 2.14644893 -0.14757929 0.14252834 0.54433625 -0.21374741 0.08804508]
[-0.05644831 -0.75323572 -1.95304923 0.17167461 -0.64155798 -1.38576017]
[-1.40043868 -0.62073383 -0.13501655 0.79788858 -1.47284176 1.03076414]
[ 0.52384943 -0.51581571 -0.35674166 1.32374059 -0.31340491 0.26292693]
[-0.28434997 0.07384262 1.62577397 -0.54059147 -1.02090985 2.36613533]
[-0.22025823 -1.07203572 1.30598633 0.39122889 2.05180917 1.59262088]
[-2.53455261 0.79274529 0.1822599 1.11345144 0.54343454 0.27523291]
[-1.11915817 1.21435385 0.87345865 0.85541497 1.90349169 -0.05778244]
[ 0.99636776 0.83682256 -0.03753307 -0.11389184 1.14089214 0.11317533]]

Dimensions of our Array...
2

Datatype of our Array object...
float64

Shape of our Array object...
(9, 6)

Result...
[[ 0.19229685 -0.03266066 -0.05913054 0.0990068 0.01377734 -0.02829296
-0.11340774 -0.02715551 0.13106032]
[ 0.01242764 -0.03612164 -0.0019295 0.00090135 0.15372234 -0.31686534
0.16305901 0.09059529 0.45836714]
[ 0.23344397 -0.46295399 -0.17382325 -0.0801975 -0.10227208 -0.04366331
-0.14434698 0.2615106 -0.84357154]
[ 0.28299012 -0.06772757 0.11355691 0.31272279 -0.11283442 -0.0361218
0.12165585 0.17999476 -0.14682526]
[-0.11148768 0.11063486 -0.07823299 -0.03096356 -0.07104466 0.24122668
0.02395283 0.01890529 0.26797921]
[-0.13235983 0.21188986 0.20340676 0.09081754 0.31058622 0.13372814
0.11417357 -0.20740154 0.71096452]]

更新於: 2022-02-25

2K+ 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告