温馨提示×

Python Numpy的矩阵计算技巧

小樊
83
2024-07-15 18:39:37
栏目: 编程语言

  1. 创建矩阵:
import numpy as np

# 创建一个2x3的矩阵
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
  1. 矩阵加法和减法:
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# 矩阵加法
result_add = matrix1 + matrix2
print(result_add)

# 矩阵减法
result_sub = matrix1 - matrix2
print(result_sub)
  1. 矩阵乘法:
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# 矩阵乘法
result_mul = np.dot(matrix1, matrix2)
print(result_mul)
  1. 矩阵转置:
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# 矩阵转置
result_transpose = matrix.T
print(result_transpose)
  1. 矩阵求逆:
matrix = np.array([[1, 2], [3, 4]])

# 求矩阵的逆
result_inv = np.linalg.inv(matrix)
print(result_inv)

这些是一些常用的矩阵计算技巧,希望对你有帮助!

0