Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Determinants and Matrix Inverse

Learn how to calculate determinants and matrix inverses using NumPy. Understand the mathematical meaning of determinants, identify singular matrices, calculate inverse matrices with np.linalg.inv(), and verify matrix inverses through practical numerical examples.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand the meaning of a matrix determinant.
  • Calculate determinants using np.linalg.det().
  • Understand the determinant formula for a 2 × 2 matrix.
  • Understand what an inverse matrix represents.
  • Calculate matrix inverses using np.linalg.inv().
  • Identify singular and non-singular matrices.
  • Verify an inverse using matrix multiplication.
  • Apply determinants and inverses to numerical problems.

2. Introduction to Determinants

A determinant is a single numerical value calculated from a square matrix. It provides useful information about the matrix, including whether the matrix is invertible.

NumPy provides the function np.linalg.det() to calculate the determinant of a square matrix.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) determinant = np.linalg.det(A) print("Matrix:") print(A) print("Determinant:", determinant)

Output:

Matrix: [[1 2] [3 4]] Determinant: -2.0000000000000004

The mathematical determinant is exactly -2. Floating-point calculations can display a value very close to the exact answer rather than its precise mathematical representation.

Remember: A determinant is defined for square matrices. A matrix with different numbers of rows and columns does not have a determinant.

3. Determinant of a 2 × 2 Matrix

For a 2 × 2 matrix, the determinant can be calculated using a simple formula.

Mathematical Formula

Consider the matrix:

A = [[a, b], [c, d]]

The determinant is calculated as:

det(A) = (a × d) - (b × c)

Example

A = [[1, 2], [3, 4]] det(A) = (1 × 4) - (2 × 3) = 4 - 6 = -2

Implementing the Formula in Python

import numpy as np A = np.array([ [1, 2], [3, 4] ]) a = A[0, 0] b = A[0, 1] c = A[1, 0] d = A[1, 1] manual_det = (a * d) - (b * c) numpy_det = np.linalg.det(A) print("Manual determinant:", manual_det) print("NumPy determinant:", round(numpy_det, 2))

Output:

Manual determinant: -2 NumPy determinant: -2.0

Both approaches produce the same mathematical determinant. The manual formula is helpful for learning, while NumPy is convenient for larger matrices.

4. Determinant of a 3 × 3 Matrix

A 3 × 3 determinant is more involved than a 2 × 2 determinant. It can be calculated by expanding along a row or column. For practical work, NumPy can calculate it directly.

import numpy as np A = np.array([ [1, 2, 3], [0, 1, 4], [5, 6, 0] ]) determinant = np.linalg.det(A) print("Matrix:") print(A) print("Determinant:", round(determinant))

Output:

Matrix: [[1 2 3] [0 1 4] [5 6 0]] Determinant: 1

The determinant of this matrix is 1. Since the determinant is non-zero, the matrix is invertible.

Tip: Use np.linalg.det(A) for square matrices instead of manually expanding large determinants unless you are specifically practising the mathematical method.

5. Understanding Singular and Non-Singular Matrices

A square matrix is called singular when its determinant is zero. A singular matrix does not have an ordinary matrix inverse. If its determinant is non-zero, the matrix is non-singular and has an inverse.

Matrix Type Determinant Has an Inverse?
Non-singular matrix Non-zero Yes
Singular matrix Zero No

Example of a Non-Singular Matrix

import numpy as np A = np.array([ [1, 2], [3, 4] ]) det_A = np.linalg.det(A) print("Determinant:", round(det_A, 2))

Output:

Determinant: -2.0

Example of a Singular Matrix

import numpy as np B = np.array([ [1, 2], [2, 4] ]) det_B = np.linalg.det(B) print("Determinant:", round(det_B, 2))

Output:

Determinant: 0.0

The second row is twice the first row. The rows are linearly dependent, so the determinant is zero and the matrix has no inverse.

Important: In floating-point calculations, a determinant that is extremely close to zero requires care. Do not rely on an exact equality check for every numerical problem.

6. Introduction to Matrix Inverse

The inverse of a square matrix A is written as A⁻¹. When the inverse exists, multiplying A by its inverse produces the identity matrix.

Inverse Property

A @ A_inverse = I A_inverse @ A = I

Here, I represents the identity matrix. Its diagonal elements are 1, while its other elements are 0.

NumPy provides np.linalg.inv() to calculate the inverse of an invertible square matrix.

import numpy as np A = np.array([ [1, 2], [3, 4] ], dtype=float) inverse_A = np.linalg.inv(A) print("Original matrix:") print(A) print("Inverse matrix:") print(inverse_A)

Output:

Original matrix: [[1. 2.] [3. 4.]] Inverse matrix: [[-2. 1. ] [ 1.5 -0.5]]

The inverse matrix contains the values needed to reverse the linear transformation represented by A.

7. Formula for the Inverse of a 2 × 2 Matrix

For a 2 × 2 matrix, the inverse can be expressed using its determinant. This formula is valid only when the determinant is non-zero.

Mathematical Formula

A = [[a, b], [c, d]] det(A) = ad - bc A_inverse = (1 / det(A)) * [[ d, -b], [-c, a]]

Example

A = [[1, 2], [3, 4]] det(A) = (1 × 4) - (2 × 3) = -2 A_inverse = (1 / -2) * [[ 4, -2], [-3, 1]] A_inverse = [[-2.0, 1.0], [ 1.5, -0.5]]

Implementing the Formula Manually

import numpy as np A = np.array([ [1, 2], [3, 4] ], dtype=float) a = A[0, 0] b = A[0, 1] c = A[1, 0] d = A[1, 1] det_A = (a * d) - (b * c) if det_A != 0: inverse_manual = (1 / det_A) * np.array([ [d, -b], [-c, a] ]) print(inverse_manual) else: print("Matrix has no inverse.")

Output:

[[-2. 1. ] [ 1.5 -0.5]]
Remember: The determinant appears in the denominator of the inverse formula. When the determinant is zero, the inverse is undefined.

8. Verifying the Matrix Inverse

A useful way to verify an inverse is to multiply the original matrix by its inverse. The result should be approximately the identity matrix, allowing for small floating-point errors.

import numpy as np A = np.array([ [1, 2], [3, 4] ], dtype=float) inverse_A = np.linalg.inv(A) result = A @ inverse_A print("A @ inverse_A:") print(result) print("Is identity:", np.allclose(result, np.eye(2)))

Output:

A @ inverse_A: [[1.00000000e+00 0.00000000e+00] [0.00000000e+00 1.00000000e+00]] Is identity: True

The displayed values may include tiny rounding differences. np.allclose() checks whether the computed values are sufficiently close to the expected identity matrix.

Best practice: Use np.allclose() instead of checking floating-point matrix values with exact equality.

9. Creating and Understanding the Identity Matrix

An identity matrix is a square matrix containing ones on its main diagonal and zeros everywhere else. It acts as the multiplicative identity for compatible matrices.

import numpy as np I = np.eye(3) print(I)

Output:

[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]

Verify Matrix Multiplication with Identity

import numpy as np A = np.array([ [2, 3], [4, 5] ]) I = np.eye(2) print(A @ I)

Output:

[[2. 3.] [4. 5.]]

Multiplying a matrix by a compatible identity matrix leaves the matrix unchanged.

10. Handling Singular Matrices

Calling np.linalg.inv() on a singular matrix normally raises a LinAlgError because the matrix has no inverse.

import numpy as np A = np.array([ [1, 2], [2, 4] ], dtype=float) try: inverse_A = np.linalg.inv(A) print(inverse_A) except np.linalg.LinAlgError: print("The matrix is singular and has no inverse.")

Output:

The matrix is singular and has no inverse.

The exception can be handled using try and except so that the program can display a useful message rather than stopping unexpectedly.

Important: A determinant check can help explain why a matrix is singular, but floating-point computations need numerical care. For important numerical applications, use appropriate linear algebra methods rather than relying only on a raw determinant threshold.

11. Practical Example: Solving Linear Equations

A system of linear equations can be represented in matrix form as Ax = b. If A is invertible, the solution can be written mathematically as x = A⁻¹b.

Example Equations

2x + y = 5 x + 3y = 6

These equations can be represented as:

A = [[2, 1], [1, 3]] x = [x, y] b = [5, 6]

Solution Using the Inverse

import numpy as np A = np.array([ [2, 1], [1, 3] ], dtype=float) b = np.array([5, 6], dtype=float) inverse_A = np.linalg.inv(A) solution = inverse_A @ b print("Solution:", solution)

Output:

Solution: [1.8 1.4]

The solution is x = 1.8 and y = 1.4. Substituting these values into the original equations satisfies both equations.

Practical note: For solving a system Ax = b in NumPy, np.linalg.solve(A, b) is generally preferred over explicitly calculating the inverse. It is usually more efficient and numerically appropriate.

Recommended Method: np.linalg.solve()

import numpy as np A = np.array([ [2, 1], [1, 3] ], dtype=float) b = np.array([5, 6], dtype=float) solution = np.linalg.solve(A, b) print("Solution:", solution)

Output:

Solution: [1.8 1.4]

12. Comparing Determinants and Inverses

Feature Determinant Matrix Inverse
Meaning A scalar value calculated from a square matrix. A matrix that reverses the transformation of an invertible matrix.
NumPy function np.linalg.det() np.linalg.inv()
Output A single numerical value. A matrix with compatible dimensions.
Singular matrix Determinant equals zero. Inverse does not exist.
Common use Checking invertibility and matrix properties. Matrix calculations and mathematical transformations.

13. Common Mistakes

Mistake 1: Using a Non-Square Matrix

Determinants and ordinary matrix inverses are defined for square matrices. Check the matrix shape before calling the corresponding function.

Mistake 2: Inverting a Singular Matrix

A matrix whose determinant is zero does not have an ordinary inverse. NumPy may raise a LinAlgError.

Mistake 3: Expecting Exact Floating-Point Values

Numerical calculations may produce tiny rounding errors. Use np.allclose() when comparing computed matrices with expected numerical results.

Mistake 4: Using the Inverse to Solve Every System

Although the inverse formula is mathematically useful, np.linalg.solve(A, b) is generally the recommended method for solving a square linear system.

14. Practice Exercises

Exercise 1: Calculate a Determinant

Create a 2 × 2 matrix [[4, 2], [3, 5]]. Calculate its determinant using np.linalg.det().

Exercise 2: Calculate a 3 × 3 Determinant

Create a 3 × 3 square matrix and calculate its determinant. Verify that the matrix has the correct shape.

Exercise 3: Calculate an Inverse

Create the matrix [[2, 1], [5, 3]]. Calculate its inverse using np.linalg.inv().

Exercise 4: Verify an Inverse

Multiply a matrix by its inverse and use np.allclose() to check whether the result is the identity matrix.

Exercise 5: Identify a Singular Matrix

Create the matrix [[1, 2], [2, 4]]. Calculate its determinant and explain why the inverse does not exist.

Exercise 6: Solve Linear Equations

Use NumPy to solve the system 3x + y = 7 and x + 2y = 5. Compare np.linalg.solve() with the inverse-based mathematical method.

Summary

  • A determinant is a scalar value associated with a square matrix.
  • Use np.linalg.det() to calculate a determinant.
  • A 2 × 2 determinant is calculated as ad - bc.
  • A non-zero determinant indicates that a square matrix is invertible.
  • A singular matrix has determinant zero and no ordinary inverse.
  • Use np.linalg.inv() to calculate an inverse matrix.
  • Multiplying a matrix by its inverse produces the identity matrix, up to numerical precision.
  • Use np.allclose() to verify approximate numerical equality.
  • Use np.linalg.solve(A, b) as the preferred approach for solving many square linear systems.

You are now ready to calculate determinants, find matrix inverses, identify singular matrices, and solve linear equations with NumPy.