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

Eigenvalues and Linear Equations

Learn how to calculate eigenvalues and eigenvectors using NumPy, solve systems of linear equations, and understand the role of matrix operations in scientific computing. Explore np.linalg.eig(), np.linalg.solve(), and practical examples with step-by-step explanations.

1. Learning Objectives

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

  • Understand eigenvalues and eigenvectors.
  • Calculate eigenvalues and eigenvectors with NumPy.
  • Interpret the output of np.linalg.eig().
  • Represent linear equations using matrices and vectors.
  • Solve linear systems using np.linalg.solve().
  • Check whether a calculated solution satisfies a system.
  • Understand what happens when a system has no unique solution.
  • Apply these operations to practical numerical problems.

2. Introduction to Eigenvalues and Eigenvectors

Eigenvalues and eigenvectors describe important properties of a square matrix. For a matrix A, an eigenvector is a non-zero vector whose direction remains unchanged when A multiplies it, although its magnitude may change.

The relationship is represented by the equation:

Eigenvalue Equation

A @ v = lambda * v

Where:

  • A: The square matrix.
  • v: An eigenvector.
  • lambda: The corresponding eigenvalue.

The eigenvalue describes how the eigenvector is scaled by the matrix transformation. Eigenvalues and eigenvectors are used in scientific computing, data analysis, physics, and machine learning.

v A Transform lambda v The eigenvector direction stays unchanged when scaled by its eigenvalue.

An eigenvector is scaled by the matrix transformation without changing its direction.

3. Calculating Eigenvalues and Eigenvectors

NumPy provides np.linalg.eig() to calculate the eigenvalues and corresponding eigenvectors of a square matrix.

import numpy as np A = np.array([ [4, 0], [0, 2] ], dtype=float) eigenvalues, eigenvectors = np.linalg.eig(A) print("Eigenvalues:") print(eigenvalues) print("Eigenvectors:") print(eigenvectors)

Output:

Eigenvalues: [4. 2.] Eigenvectors: [[1. 0.] [0. 1.]]

The eigenvalues are 4 and 2. Each column in the eigenvector matrix corresponds to the eigenvalue at the same position in the eigenvalue array.

Remember: Eigenvectors are returned as columns of the eigenvector matrix, not as rows.

4. Understanding the Eigenvalue Equation

For an eigenvalue lambda and its corresponding eigenvector v, multiplying A by v should produce the same result as multiplying v by lambda.

import numpy as np A = np.array([ [4, 0], [0, 2] ], dtype=float) eigenvalues, eigenvectors = np.linalg.eig(A) for i in range(len(eigenvalues)): value = eigenvalues[i] vector = eigenvectors[:, i] left_side = A @ vector right_side = value * vector print("Eigenvalue:", value) print("A @ v:", left_side) print("lambda * v:", right_side) print()

Output:

Eigenvalue: 4.0 A @ v: [4. 0.] lambda * v: [4. 0.] Eigenvalue: 2.0 A @ v: [0. 2.] lambda * v: [0. 2.]

Both sides match for each eigenvalue and eigenvector pair. This verifies the eigenvalue relationship for the example.

5. Eigenvalues of a General Matrix

Eigenvalues are not always integers, and eigenvectors may contain several non-zero components. Consider the following matrix:

import numpy as np A = np.array([ [2, 1], [1, 2] ], dtype=float) eigenvalues, eigenvectors = np.linalg.eig(A) print("Eigenvalues:") print(eigenvalues) print("Eigenvectors:") print(eigenvectors)

Output:

Eigenvalues: [3. 1.] Eigenvectors: [[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]]

The eigenvalues are 3 and 1. The eigenvector directions correspond to the two columns returned by NumPy.

Important: Eigenvectors are not unique in magnitude. Multiplying an eigenvector by any non-zero scalar produces another valid eigenvector for the same eigenvalue.

6. Introduction to Linear Equations

A linear equation contains variables raised only to the first power. A system of linear equations contains two or more equations that must be satisfied simultaneously.

Example System

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

We can represent this system using a coefficient matrix A, an unknown vector x, and a result vector b.

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

Representing equations as matrices makes them easier to solve using numerical linear algebra functions.

7. Solving Linear Equations with np.linalg.solve()

NumPy provides np.linalg.solve(A, b) to solve a square linear system Ax = b when the coefficient matrix has a unique solution.

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:") print(solution) print("x =", solution[0]) print("y =", solution[1])

Output:

Solution: [1.8 1.4] x = 1.8 y = 1.4

The solution is x = 1.8 and y = 1.4. These values satisfy both original equations.

Best practice: Prefer np.linalg.solve(A, b) over explicitly calculating an inverse when solving a linear system. It is generally more efficient and numerically appropriate.

8. Verifying a Linear Equation Solution

After calculating a solution, multiply the coefficient matrix by the solution vector. The result should be approximately equal to the original right-hand-side vector.

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) calculated_b = A @ solution print("Solution:", solution) print("Calculated right-hand side:", calculated_b) print("Original right-hand side:", b) print("Verified:", np.allclose(calculated_b, b))

Output:

Solution: [1.8 1.4] Calculated right-hand side: [5. 6.] Original right-hand side: [5. 6.] Verified: True

np.allclose() checks whether the calculated values are sufficiently close to the expected values, accounting for floating-point precision.

9. Systems with No Unique Solution

A square linear system may fail to have a unique solution when its coefficient matrix is singular. The equations may be inconsistent or may have infinitely many solutions.

Example: Dependent Equations

x + y = 2 2x + 2y = 4

The second equation is exactly twice the first equation. Both equations describe the same line, so there are infinitely many solutions rather than one unique solution.

import numpy as np A = np.array([ [1, 1], [2, 2] ], dtype=float) b = np.array([2, 4], dtype=float) try: solution = np.linalg.solve(A, b) print(solution) except np.linalg.LinAlgError: print("The system does not have a unique solution.")

Output:

The system does not have a unique solution.
Important: A singular coefficient matrix cannot be solved using np.linalg.solve() for a unique solution. Depending on the system, it may have no solutions or infinitely many solutions.

10. Solving a Three-Variable System

NumPy can also solve systems containing three or more unknowns, provided the coefficient matrix and right-hand-side vector have compatible dimensions and the system has a unique solution.

import numpy as np # Equations: # x + y + z = 6 # 2x + y + z = 8 # x + 2y + z = 9 A = np.array([ [1, 1, 1], [2, 1, 1], [1, 2, 1] ], dtype=float) b = np.array([6, 8, 9], dtype=float) solution = np.linalg.solve(A, b) print("x, y, z:") print(solution) print("Verification:") print(A @ solution)

Output:

x, y, z: [2. 3. 1.] Verification: [6. 8. 9.]

The solution is x = 2, y = 3, and z = 1. Multiplying the coefficient matrix by the solution reproduces the right-hand-side values.

11. Practical Example: Student Performance Analysis

Activity Description

At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha are reviewing a numerical model that combines three subject scores. The team can represent the subject relationships using a matrix and use NumPy to solve for unknown values.

In this example, suppose a simple model is described by the equations:

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

Sample Solution

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) names = ["Samadhan", "Pradnya", "Samiksha"] print("Model solution:", solution) for name in names: print(name, "can review the model result.")

Output:

Model solution: [1.8 1.4] Samadhan can review the model result. Pradnya can review the model result. Samiksha can review the model result.

The calculated vector contains the unknown values of the mathematical model. The names are included to demonstrate how a team might review a numerical result.

12. Eigenvalues vs Linear Equation Solving

Feature Eigenvalues and Eigenvectors Linear Equation Solving
Purpose Understand matrix transformations and their special directions. Find unknown values that satisfy a system of equations.
NumPy function np.linalg.eig() np.linalg.solve()
Input A square matrix A coefficient matrix and a right-hand-side vector
Output Eigenvalues and corresponding eigenvectors A solution vector
Common application Scientific computing, transformations, and data analysis Solving mathematical and engineering systems

13. Common Mistakes

Mistake 1: Using a Non-Square Matrix for Eigenvalues

The standard eigenvalue decomposition in np.linalg.eig() requires a square matrix.

Mistake 2: Misreading Eigenvector Columns

NumPy returns eigenvectors as columns. The i-th eigenvector corresponds to the i-th eigenvalue.

Mistake 3: Mixing Up A and b

In Ax = b, A is the coefficient matrix and b is the right-hand-side vector. Their dimensions must be compatible.

Mistake 4: Assuming Every System Has a Unique Solution

A singular coefficient matrix may lead to no solution or infinitely many solutions.

Mistake 5: Comparing Floating-Point Results Exactly

Use np.allclose() when verifying computed numerical results instead of relying on exact floating-point equality.

14. Practice Exercises

Exercise 1: Calculate Eigenvalues

Create the matrix [[3, 0], [0, 5]] and calculate its eigenvalues and eigenvectors using np.linalg.eig().

Exercise 2: Verify an Eigenvector

Select an eigenvalue and its corresponding eigenvector. Check whether A @ v is approximately equal to lambda * v.

Exercise 3: Solve Two Linear Equations

Solve the system 3x + y = 7 and x + 2y = 5 using np.linalg.solve().

Exercise 4: Verify the Solution

Multiply the coefficient matrix by your solution vector. Use np.allclose() to compare the result with b.

Exercise 5: Identify a Singular System

Try solving x + y = 2 and 2x + 2y = 5. Explain why the system has no solution.

Exercise 6: Three Unknowns

Create a 3 × 3 coefficient matrix and a three-element right-hand-side vector. Solve the system and verify the result.

Summary

In this lesson, you learned how to calculate eigenvalues and eigenvectors using np.linalg.eig(). You explored how eigenvectors relate to matrix transformations through the equation A @ v = lambda * v.

You also learned to represent systems of linear equations using matrices and vectors, solve them with np.linalg.solve(), and verify numerical solutions using np.allclose().