Linear Algebra with NumPy
Linear algebra is the mathematical foundation behind machine learning,
data science, scientific computing, and engineering simulations.
NumPy provides a powerful linalg module to work with
linear equations, vectors, and matrices efficiently.
In this lesson, you will learn how to solve linear equations, work with vectors, and perform core linear algebra operations.
Why Linear Algebra Matters
Linear algebra is used in:
- Machine learning models
- Optimization problems
- Data transformations
- Computer vision and graphics
Understanding these operations helps you work confidently with numerical data.
Creating a Linear System
Let us define a system of linear equations using matrices.
import numpy as np
A = np.array([[3, 1],
[1, 2]])
b = np.array([9, 8])
print(A)
print(b)
Here, matrix A represents coefficients, and vector b represents constants.
Solving Linear Equations
NumPy provides np.linalg.solve() to solve equations of the form:
A · x = b
solution = np.linalg.solve(A, b)
print(solution)
Output:
[2. 3.]
This means:
- x = 2
- y = 3
Dot Product of Vectors
The dot product measures the similarity between two vectors.
v1 = np.array([2, 4, 6])
v2 = np.array([1, 3, 5])
dot_product = np.dot(v1, v2)
print(dot_product)
Output:
44
The dot product is calculated as:
(2×1) + (4×3) + (6×5) = 44
Vector Magnitude (Norm)
The magnitude of a vector represents its length.
vector = np.array([3, 4])
magnitude = np.linalg.norm(vector)
print(magnitude)
Output:
5.0
This follows the Pythagorean theorem:
√(3² + 4²) = 5
Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors describe how a matrix transforms space. They are heavily used in machine learning and dimensionality reduction.
matrix = np.array([[4, 2],
[1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print(eigenvalues)
print(eigenvectors)
Output:
[5. 2.]
[[ 0.89442719 -0.4472136 ]
[ 0.4472136 0.89442719]]
Eigenvalues show scaling factors, and eigenvectors show directions.
Rank of a Matrix
The rank represents the number of linearly independent rows or columns.
rank = np.linalg.matrix_rank(A)
print(rank)
Output:
2
A full-rank matrix contains maximum independent information.
Real-World Applications
- Solving regression problems
- Dimensionality reduction
- Signal processing
- Physics simulations
Linear algebra is at the heart of numerical computing.
Practice Exercise
Task
- Create a 2×2 matrix and vector
- Solve the linear equation
- Compute dot product of two vectors
- Find eigenvalues
What’s Next?
In the next lesson, you will learn how to read and write data using NumPy, including saving arrays to files.