Mastering NumPy in Python: The Backbone of Scientific Computing

8.57K 0 0 0 0

Chapter 5: Linear Algebra and Matrix Computations in NumPy

🔹 1. Introduction

Linear algebra forms the core of many modern applications — from machine learning and computer graphics to engineering simulations and data transformations. NumPy provides an efficient and intuitive set of tools to perform linear algebra operations using the numpy.linalg module.

In this chapter, we’ll dive into:

  • Dot products and matrix multiplication
  • Transposes and inverses
  • Determinants and rank
  • Solving systems of linear equations
  • Eigenvalues and eigenvectors

These techniques are essential for data scientists, analysts, engineers, and developers working on predictive models or simulations.


🔹 2. Matrix Representation in NumPy

import numpy as np

 

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

B = np.array([[5, 6], [7, 8]])

Here, both A and B are 2×2 matrices.


🔹 3. Matrix Multiplication & Dot Product

Matrix Multiplication

Use @ or np.dot() for matrix multiplication:

result = A @ B

# or

result = np.dot(A, B)

Output:

[[19 22]

 [43 50]]

Element-wise Multiplication (not matrix multiplication)

A * B

# [[ 5 12]

#  [21 32]]


🔹 4. Transpose of a Matrix

Flip rows to columns:

A.T

# [[1 3]

#  [2 4]]


🔹 5. Inverse of a Matrix

To find the inverse of a square matrix A, use:

inv_A = np.linalg.inv(A)

Only works if the matrix is non-singular (det ≠ 0).


🔹 6. Identity Matrix

I = np.eye(3)

Output:

lua

Copy

[[1. 0. 0.]

 [0. 1. 0.]

 [0. 0. 1.]]

Useful for checking if:
A @ np.linalg.inv(A) ≈ I


🔹 7. Determinant of a Matrix

np.linalg.det(A)

# Output: -2.0 (means A is invertible)


🔹 8. Rank of a Matrix

Use:

np.linalg.matrix_rank(A)


🔹 9. Solving Systems of Equations

2x + 3y = 8

4x + 9y = 20

Represent as AX = B

A = np.array([[2, 3], [4, 9]])

B = np.array([8, 20])

X = np.linalg.solve(A, B)

print(X)  # Solution: [2. 1.]


🔹 10. Eigenvalues and Eigenvectors

Eigen decomposition helps in PCA, dynamics, and dimensionality reduction.

vals, vecs = np.linalg.eig(A)

print("Eigenvalues:", vals)

print("Eigenvectors:\n", vecs)


🔹 11. Norms and Condition Numbers

Norm (magnitude of vector)

v = np.array([3, 4])

np.linalg.norm(v)  # Output: 5.0

Condition Number (matrix stability)

np.linalg.cond(A)

A high condition number means the matrix is close to singular → numerical instability.


🔹 12. Pseudoinverse

Used when the matrix isn’t square or isn’t invertible.

np.linalg.pinv(A)


🔹 13. Summary Table

Operation

Function/Method

Description

Matrix multiplication

np.dot() or @

Matrix product

Transpose

.T

Swap rows and columns

Inverse

np.linalg.inv()

Matrix inverse

Determinant

np.linalg.det()

Scalar value from square matrix

Rank

np.linalg.matrix_rank()

Dimension of the column space

Solve equations

np.linalg.solve()

Solve AX = B

Eigenvalues/vectors

np.linalg.eig()

For PCA, dynamics

Norm

np.linalg.norm()

Vector magnitude

Pseudoinverse

np.linalg.pinv()

Generalized inverse for non-square A


🔹 14. Real-World Example: PCA (Simplified)

X = np.array([[2.5, 2.4],

              [0.5, 0.7],

              [2.2, 2.9]])

 

# Step 1: Mean Center

X_centered = X - np.mean(X, axis=0)

 

# Step 2: Covariance matrix

cov = np.cov(X_centered.T)

 

# Step 3: Eigen decomposition

vals, vecs = np.linalg.eig(cov)

Principal components = Eigenvectors
Importance = Eigenvalues



Back

FAQs


1. What is NumPy used for?

NumPy is used for numerical computations, array operations, linear algebra, and data processing in Python.

2. How is NumPy different from regular Python lists?

NumPy arrays are faster, use less memory, and support vectorized operations, unlike Python lists which are slower and less flexible for numerical tasks

3. What is an ndarray in NumPy?

It’s the core data structure in NumPy — an N-dimensional array that allows element-wise operations and advanced indexing.

4. Is NumPy part of the standard Python library?

No, it needs to be installed separately using pip install numpy.

5. What are broadcasting rules in NumPy?

Broadcasting allows NumPy to perform operations on arrays of different shapes by automatically expanding them to be compatible.

6. Can NumPy be used for linear algebra and matrix operations?

Yes, it provides comprehensive support for matrix multiplication, eigenvalues, singular value decomposition, and more.

7. Is NumPy suitable for big data or deep learning?

While NumPy is essential for preprocessing and fast array computations, deep learning libraries like TensorFlow or PyTorch build on top of it for more advanced tasks.

8. Can I use NumPy with Pandas and Matplotlib?

Absolutely Pandas is built on NumPy arrays, and Matplotlib supports NumPy for plotting.

9. Does NumPy support random number generation?

Yes the numpy.random module offers distributions like normal, binomial, uniform, etc.

10. Is NumPy faster than Python loops?

Significantly. NumPys vectorized operations are typically 10x to 100x faster than traditional for-loops in Python.