Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Matrix Operations in Python | Section
Python Math Module Essentials: Trigonometry, Logarithms, and Constants - 1769704232288

Matrix Operations in Python

Свайпніть щоб показати меню

1. Addition and Subtraction

Two matrices AA and BB of the same shape can be added:

123456789
import numpy as np A = np.array([[1, 2], [5, 6]]) B = np.array([[3, 4], [7, 8]]) C = A + B print(f'C:\n{C}') # C = [[4, 6], [12, 14]]

2. Multiplication Rules

Matrix multiplication is not element-wise.

Rule: if AA has shape (n,m)(n, m) and BB has shape (m,l)(m, l), then the result has shape (n,l)(n, l).

1234567891011121314151617181920
import numpy as np # Example random matrix 3x2 A = np.array([[1, 2], [3, 4], [5, 6]]) print(f'A:\n{A}') # Example random matrix 2x4 B = np.array([[11, 12, 13, 14], [15, 16, 17, 18]]) print(f'B:\n{B}') # product shape (3, 4) product = np.dot(A, B) print(f'np.dot(A, B):\n{product}') # or equivalently product = A @ B print(f'A @ B:\n{product}')

3. Transpose

Transpose flips rows and columns.

General rule: if AA is (n×m)(n \times m), then ATA^T is (m×n)(m \times n).

1234567
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) A_T = A.T # Transpose of A print(f'A_T:\n{A_T}')

4. Inverse of a Matrix

A matrix AA has an inverse A1A^{-1} if:

AA1=IA \cdot A^{-1} = I

Where II is the identity matrix.

Not all matrices have inverses. A matrix must be square and full-rank.

12345678910
import numpy as np A = np.array([[1, 2], [3, 4]]) A_inv = np.linalg.inv(A) # Inverse of A print(f'A_inv:\n{A_inv}') I = np.eye(2) # Identity matrix 2x2 print(f'A x A_inv = I:\n{np.allclose(A @ A_inv, I)}') # Check if product equals identity
question mark

What is the output of this Python code?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 31

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 31
some-alt