Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Matrix Operations in Python | Linear Algebra Foundations
Mathematics for Data Science

bookMatrix Operations in Python

1. Matrix Operations: 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 = {C}') # C = [[4, 6], [12, 14]]
copy

2. Matrix 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).

123456789101112131415
# Example random matrix 3x2 A = np.random.rand(3, 2) print(f'A = {A}') # Example random matrix 2x4 B = np.random.rand(2, 4) print(f'B = {B}') # product shape (3, 4) product = np.dot(A, B) print(f'np.dot(A, B) = {product}') # or equivalently product = A @ B print(f'A @ B = {product}')
copy

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).

12345
A = np.array([[1, 2, 3], [4, 5, 6]]) A_T = A.T # Transpose of A print(A_T)
copy

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.

Quiz



  1. What does np.linalg.inv(A) do in Python when solving the system $Ax = b$?

A) Rotates vector $x$ B) Eliminates matrix $A$ C) Reverses the linear transformation applied by $A$ D) None of the above

Correct Answer: C




Correct Answer: B


12345678
A = np.array([[1, 2], [3, 4]]) A_inv = np.linalg.inv(A) # Inverse of A print(A_inv) I = np.eye(2) # Identity matrix 2x2 print(assert np.allclose(A @ A_inv, I)) # Check if product equals identity
copy

1. What does the following Python code print?

2. In Python's numpy, matrix multiplication can be performed between non-square matrices as long as their inner dimensions align.

3. What is the output of this Python code?

question mark

What does the following Python code print?

Select the correct answer

question mark

In Python's numpy, matrix multiplication can be performed between non-square matrices as long as their inner dimensions align.

Select the correct answer

question mark

What is the output of this Python code?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 1.89

bookMatrix Operations in Python

Deslize para mostrar o menu

1. Matrix Operations: 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 = {C}') # C = [[4, 6], [12, 14]]
copy

2. Matrix 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).

123456789101112131415
# Example random matrix 3x2 A = np.random.rand(3, 2) print(f'A = {A}') # Example random matrix 2x4 B = np.random.rand(2, 4) print(f'B = {B}') # product shape (3, 4) product = np.dot(A, B) print(f'np.dot(A, B) = {product}') # or equivalently product = A @ B print(f'A @ B = {product}')
copy

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).

12345
A = np.array([[1, 2, 3], [4, 5, 6]]) A_T = A.T # Transpose of A print(A_T)
copy

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.

Quiz



  1. What does np.linalg.inv(A) do in Python when solving the system $Ax = b$?

A) Rotates vector $x$ B) Eliminates matrix $A$ C) Reverses the linear transformation applied by $A$ D) None of the above

Correct Answer: C




Correct Answer: B


12345678
A = np.array([[1, 2], [3, 4]]) A_inv = np.linalg.inv(A) # Inverse of A print(A_inv) I = np.eye(2) # Identity matrix 2x2 print(assert np.allclose(A @ A_inv, I)) # Check if product equals identity
copy

1. What does the following Python code print?

2. In Python's numpy, matrix multiplication can be performed between non-square matrices as long as their inner dimensions align.

3. What is the output of this Python code?

question mark

What does the following Python code print?

Select the correct answer

question mark

In Python's numpy, matrix multiplication can be performed between non-square matrices as long as their inner dimensions align.

Select the correct answer

question mark

What is the output of this Python code?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4
some-alt