在 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]]
广告