Module 2 of 6 · Applied Maths with NumPy · Beginner

Linear Algebra with NumPy

Duration: 20 min

Try it in Google Colab: Open in Colab

Visual: Matrix Operations in NumPy

Matrix A (2x3)        Matrix B (3x2)
[[1, 2, 3],           [[1, 2],
 [4, 5, 6]]            [3, 4],
                       [5, 6]]

Result: A @ B (2x2)
[[22, 28],
 [49, 64]]

Key Concepts Table

Operation NumPy Function Purpose
Dot Product np.dot() Vector similarity
Matrix Mult @ or np.matmul() Layer transformation
Transpose .T or np.transpose() Flip dimensions
Inverse np.linalg.inv() Solve equations
Eigenvalues np.linalg.eig() Matrix properties
Determinant np.linalg.det() Matrix invertibility
Rank np.linalg.matrix_rank() Linear independence

Vector Operations

import numpy as np

# Create vectors
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Vector addition
c = a + b  # [5, 7, 9]

# Scalar multiplication
d = 2 * a  # [2, 4, 6]

# Dot product (inner product)
dot_product = np.dot(a, b)  # 1*4 + 2*5 + 3*6 = 32

# Vector magnitude (norm)
magnitude = np.linalg.norm(a)  # sqrt(1² + 2² + 3²) ≈ 3.74

Matrix Operations

# Create matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix addition
C = A + B

# Matrix multiplication
D = np.dot(A, B)
# or
D = A @ B

# Matrix transpose
A_T = A.T

# Matrix inverse
A_inv = np.linalg.inv(A)

# Determinant
det_A = np.linalg.det(A)

Eigenvalues and Eigenvectors

A = np.array([[1, 2], [2, 1]])

# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)

print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)

Output:

Eigenvalues: [-0.37228132  5.37228132]
Eigenvectors: [[-0.82456485 -0.85090352]
               [ 0.56568542 -0.52573111]]

Solving Linear Systems

# Solve Ax = b
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])

# Solution
x = np.linalg.solve(A, b)
print("x =", x)

Matrix Decomposition

# Singular Value Decomposition (SVD)
A = np.array([[1, 2], [3, 4], [5, 6]])
U, S, V = np.linalg.svd(A)

# QR Decomposition
Q, R = np.linalg.qr(A)

❓ What does np.dot(a, b) compute for vectors?

Practice Quizzes

Quiz 1: What does np.dot() compute?

Quiz 2: How do you transpose a matrix in NumPy?

Quiz 3: What is the determinant used for?

← Previous Continue interactively → Next →

Related Courses