Python Numpy.linalg.inv() - 逆矩阵
Jinku Hu
2023年1月30日
2020年6月17日
-
numpy.linalg.inv()
语法 -
示例代码:
numpy.linalg.inv()
方法 -
示例代码:
numpy.linalg.inv()
方法与输入矩阵 -
示例代码:
numpy.square()
与矩阵数组
Numpy.linalg.inv()
计算给定矩阵的逆矩阵。
numpy.linalg.inv()
语法
numpy.linalg.inverse(arr)
参数
arr |
输入数组 |
返回值
返回给定矩阵的逆矩阵。
如果给定的矩阵不是正方形或者求逆矩阵失败,它会引发错误。
示例代码:numpy.linalg.inv()
方法
import numpy as np
arr = np.array([[1, 3], [5, 7]])
arr_inv = np.linalg.inv(arr)
print(arr_inv)
输出:
[[-0.875 0.375]
[ 0.625 -0.125]]
示例代码:numpy.linalg.inv()
方法与输入矩阵
如果给定的输入是一个 numpy 矩阵,那么 inv()
也返回一个矩阵。
import numpy as np
arr = np.matrix([[1, 3], [5, 7]])
arr_inv = np.linalg.inv(arr)
print(arr_inv, type(arr_inv))
输出:
[[-0.875 0.375]
[ 0.625 -0.125]] <class 'numpy.matrix'>
示例代码:numpy.square()
与矩阵数组
import numpy as np
arr = np.array([
[[1, 3],
[5, 7]],
[[2, 5],
[4, 6]]])
arr_inv = np.linalg.inv(arr)
print(arr_inv)
输出:
[[[-0.875 0.375]
[ 0.625 -0.125]]
[[-0.75 0.625]
[ 0.5 -0.25 ]]]
如果输入数组由多个矩阵组成,numpy linalg.inv()
方法一次计算它们的逆矩阵。
Author: Jinku Hu
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn