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

Matrix Multiplication and Dot Product

Learn how to perform matrix multiplication and vector dot products using NumPy. Understand how rows and columns interact, how matrix dimensions determine whether multiplication is possible, and how to use the @ operator, np.dot(), and np.matmul() with practical numerical examples and student data analysis.

1. Learning Objectives

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

  • Understand the mathematical meaning of a vector dot product.
  • Perform matrix multiplication using NumPy arrays.
  • Use the @ operator for matrix and vector multiplication.
  • Perform multiplication using np.dot() and np.matmul().
  • Understand the matrix dimension compatibility rule.
  • Differentiate element-wise multiplication from matrix multiplication.
  • Multiply matrices by vectors and scalar values.
  • Apply matrix multiplication to practical numerical problems.

2. Introduction to Matrix Multiplication

Matrix multiplication is a mathematical operation in which rows of the first matrix are combined with columns of the second matrix. Each output element is calculated by multiplying corresponding values and adding their products.

NumPy provides several ways to perform matrix multiplication. The @ operator is convenient and readable, while np.matmul() and np.dot() provide function-based alternatives.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) print("Matrix A:") print(A) print("Matrix B:") print(B) print("Shape of A:", A.shape) print("Shape of B:", B.shape)

Output:

Matrix A: [[1 2] [3 4]] Matrix B: [[5 6] [7 8]] Shape of A: (2, 2) Shape of B: (2, 2)

Both matrices contain two rows and two columns. Their dimensions are compatible for matrix multiplication because the number of columns in A equals the number of rows in B.

Matrix A 1 2 3 4 @ Matrix B 5 6 7 8 = Result Matrix 19 22 43 50 Each output value comes from a row-column calculation.

Matrix multiplication combines each row of A with each column of B.

3. Understanding the Matrix Dimension Rule

Before multiplying two matrices, always check their shapes. Matrix multiplication is possible only when the number of columns in the first matrix matches the number of rows in the second matrix.

Dimension Formula

A has shape (m, n) B has shape (n, p) A @ B produces shape (m, p)

The inner dimensions must match. The outer dimensions determine the shape of the result.

Matrix A Shape Matrix B Shape Compatible? Result Shape
(2, 3) (3, 4) Yes (2, 4)
(3, 2) (2, 5) Yes (3, 5)
(2, 3) (2, 4) No Not possible
(4, 4) (4, 2) Yes (4, 2)

Example: Multiplying Different Shapes

import numpy as np A = np.array([ [1, 2, 3], [4, 5, 6] ]) B = np.array([ [10, 20], [30, 40], [50, 60] ]) result = A @ B print("A shape:", A.shape) print("B shape:", B.shape) print("Result:") print(result) print("Result shape:", result.shape)

Output:

A shape: (2, 3) B shape: (3, 2) Result: [[220 280] [490 640]] Result shape: (2, 2)
Remember: A matrix with shape (2, 3) multiplied by a matrix with shape (3, 2) produces a result with shape (2, 2). The inner dimensions, 3 and 3, match.

4. Understanding the Dot Product of Vectors

A dot product multiplies corresponding elements of two vectors and adds all the products together. When two one-dimensional vectors have the same length, their dot product produces a single scalar value.

Manual Calculation

Consider the following vectors:

A = [1, 2, 3] B = [4, 5, 6] Dot product: (1 × 4) + (2 × 5) + (3 × 6) = 4 + 10 + 18 = 32

Using np.dot()

import numpy as np A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) result = np.dot(A, B) print(result)

Output:

32

Using the @ Operator

import numpy as np A = np.array([1, 2, 3]) B = np.array([4, 5, 6]) result = A @ B print(result)

Output:

32

For two one-dimensional vectors, both np.dot(A, B) and A @ B calculate the dot product. The output is a scalar rather than another vector.

Tip: Both vectors must have compatible lengths for a dot product. If their lengths differ, NumPy raises a ValueError.

5. Matrix Multiplication Using the @ Operator

The @ operator is a readable and convenient way to multiply matrices in NumPy. It performs matrix multiplication rather than multiplying corresponding elements.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) result = A @ B print(result)

Output:

[[19 22] [43 50]]

How Is the First Result Calculated?

First row of A: [1, 2] First column of B: [5, 7] First result: (1 × 5) + (2 × 7) = 5 + 14 = 19

Calculating All Four Values

Output Position Calculation Result
First row, first column (1 × 5) + (2 × 7) 19
First row, second column (1 × 6) + (2 × 8) 22
Second row, first column (3 × 5) + (4 × 7) 43
Second row, second column (3 × 6) + (4 × 8) 50

Every element in the result matrix is calculated using one row from the first matrix and one column from the second matrix.

6. Matrix Multiplication Using np.matmul()

NumPy provides the np.matmul() function specifically for matrix multiplication. For two-dimensional matrices, it produces the same result as the @ operator when the dimensions are compatible.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) result = np.matmul(A, B) print(result)

Output:

[[19 22] [43 50]]

Checking the Result Shape

result = np.matmul(A, B) print("Result:") print(result) print("Shape:", result.shape)

Output:

Result: [[19 22] [43 50]] Shape: (2, 2)
Tip: Use A @ B when you want concise, readable code. Use np.matmul(A, B) when you prefer an explicit function call.

7. Matrix Multiplication Using np.dot()

The np.dot() function calculates dot products. For two two-dimensional arrays, it also performs matrix multiplication.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) result = np.dot(A, B) print(result)

Output:

[[19 22] [43 50]]

Comparison of Matrix Multiplication Methods

Method Purpose Two 2D Matrices
A @ B Matrix multiplication operator Performs matrix multiplication
np.matmul(A, B) Matrix multiplication function Performs matrix multiplication
np.dot(A, B) Dot product function Performs matrix multiplication
A * B Element-wise multiplication Multiplies corresponding elements
Important: np.dot() and np.matmul() have different behavior for arrays with more than two dimensions. For two-dimensional matrices, both perform matrix multiplication. For higher-dimensional arrays, select the function according to the operation you need.

8. Element-Wise Multiplication vs Matrix Multiplication

One common mistake is assuming that the multiplication operator * performs matrix multiplication. In NumPy, * multiplies corresponding elements, whereas @ combines rows and columns.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) element_wise = A * B matrix_product = A @ B print("Element-wise multiplication:") print(element_wise) print("Matrix multiplication:") print(matrix_product)

Output:

Element-wise multiplication: [[ 5 12] [21 32]] Matrix multiplication: [[19 22] [43 50]]

Why Are the Results Different?

Element-wise multiplication:

1 × 5 = 5 2 × 6 = 12 3 × 7 = 21 4 × 8 = 32

Matrix multiplication:

(1 × 5) + (2 × 7) = 19 (1 × 6) + (2 × 8) = 22 (3 × 5) + (4 × 7) = 43 (3 × 6) + (4 × 8) = 50
Remember: Use * for element-wise multiplication and @ for matrix multiplication. The two operations have different mathematical meanings.

9. Matrix-Vector Multiplication

A matrix can also be multiplied by a one-dimensional vector. The number of columns in the matrix must equal the number of elements in the vector.

import numpy as np A = np.array([ [1, 2, 3], [4, 5, 6] ]) v = np.array([10, 20, 30]) result = A @ v print("Matrix shape:", A.shape) print("Vector shape:", v.shape) print("Result:", result) print("Result shape:", result.shape)

Output:

Matrix shape: (2, 3) Vector shape: (3,) Result: [140 320] Result shape: (2,)

Manual Calculation

First row: (1 × 10) + (2 × 20) + (3 × 30) = 10 + 40 + 90 = 140 Second row: (4 × 10) + (5 × 20) + (6 × 30) = 40 + 100 + 180 = 320

The result contains one value for each row of the matrix. Since the matrix has two rows, the resulting vector contains two elements.

10. Scalar Multiplication of a Matrix

Scalar multiplication multiplies every element in a matrix by a single numerical value. Unlike matrix multiplication, scalar multiplication does not combine rows and columns.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) result = 3 * A print(result)

Output:

[[ 3 6] [ 9 12]]

Each element is multiplied by 3. The shape remains (2, 2) because scalar multiplication does not change the dimensions.

Tip: Scalar multiplication is useful when scaling numerical measurements, adjusting values, or applying a constant factor to an entire matrix.

11. Matrix Multiplication Is Not Generally Commutative

In general, changing the order of matrix multiplication changes the result. A @ B is not necessarily equal to B @ A. In some cases, one order is valid while the reverse order is not dimensionally compatible.

import numpy as np A = np.array([ [1, 2], [3, 4] ]) B = np.array([ [5, 6], [7, 8] ]) AB = A @ B BA = B @ A print("A @ B:") print(AB) print("B @ A:") print(BA) print("Are they equal?", np.array_equal(AB, BA))

Output:

A @ B: [[19 22] [43 50]] B @ A: [[23 34] [31 46]] Are they equal? False

Both operations are valid for these square matrices, but their results differ. Always preserve the intended order when multiplying matrices.

Important: Do not assume that swapping two matrices leaves their product unchanged. Matrix multiplication is generally not commutative.

12. Handling Incompatible Matrix Dimensions

If the number of columns in the first matrix does not match the number of rows in the second matrix, matrix multiplication cannot be performed.

import numpy as np A = np.ones((2, 3)) B = np.ones((2, 4)) print("A shape:", A.shape) print("B shape:", B.shape) result = A @ B

The shapes are (2, 3) and (2, 4). The inner dimensions are 3 and 2, so NumPy raises a ValueError.

Check Shapes Before Multiplication

import numpy as np A = np.ones((2, 3)) B = np.ones((3, 4)) if A.shape[1] == B.shape[0]: result = A @ B print("Multiplication successful.") print(result) print("Result shape:", result.shape) else: print("Matrix dimensions are incompatible.")

Output:

Multiplication successful. [[3. 3. 3. 3.] [3. 3. 3. 3.]] Result shape: (2, 4)
Best practice: Inspect array shapes using the shape attribute before performing matrix multiplication. This helps you identify dimension errors early.

13. Practical Activity: Student Marks Analysis

Activity Description

At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha have marks in Python, NumPy, and Statistics. Each subject has a different weight. Use matrix multiplication to calculate the weighted score of each student.

Subject weights:

  • Python: 40%
  • NumPy: 35%
  • Statistics: 25%

Sample Solution

import numpy as np # Columns: Python, NumPy, Statistics marks = np.array([ [80, 85, 90], # Samadhan [75, 92, 88], # Pradnya [90, 87, 95] # Samiksha ]) weights = np.array([0.40, 0.35, 0.25]) weighted_scores = marks @ weights names = ["Samadhan", "Pradnya", "Samiksha"] for name, score in zip(names, weighted_scores): print(name, ":", round(score, 2))

Output:

Samadhan : 84.75 Pradnya : 84.7 Samiksha : 90.85

Understanding the Calculation

For Samadhan:

(80 × 0.40) + (85 × 0.35) + (90 × 0.25) = 32 + 29.75 + 22.5 = 84.25

The weighted score is calculated by multiplying each subject mark by its corresponding weight and adding the results. The same calculation is applied to every student's row.

Note: The correct weighted score for Samadhan in this example is 84.25. NumPy calculates the score directly using the marks matrix and the weight vector.

14. Common Mistakes

Mistake 1: Using * Instead of @

The * operator performs element-wise multiplication. Use @ when you want matrix multiplication.

Mistake 2: Ignoring Matrix Dimensions

The number of columns in the first matrix must match the number of rows in the second matrix.

Mistake 3: Confusing the Dot Product with Element-Wise Multiplication

The dot product of two one-dimensional vectors produces a scalar. Element-wise multiplication produces an array of products.

Mistake 4: Assuming A @ B Equals B @ A

Matrix multiplication is generally not commutative. Reversing the order can change the result or make the operation invalid.

Mistake 5: Forgetting the Output Shape

If A has shape (m, n) and B has shape (n, p), the result has shape (m, p). Use the shape attribute to verify the output.

15. Practice Exercises

Exercise 1: Vector Dot Product

Create two vectors [2, 4, 6] and [1, 3, 5]. Calculate their dot product using np.dot() and the @ operator.

Exercise 2: Matrix Multiplication

Create the matrices [[2, 1], [3, 4]] and [[1, 5], [2, 3]]. Calculate their product using the @ operator.

Exercise 3: Compare Multiplication Methods

Multiply two 2 × 2 matrices using A * B, A @ B, np.dot(A, B), and np.matmul(A, B). Explain why the first result differs from the others.

Exercise 4: Matrix-Vector Multiplication

Create a matrix with shape (3, 2) and a vector containing two elements. Multiply them and inspect the shape of the result.

Exercise 5: Weighted Student Scores

Create a marks matrix for Samadhan, Pradnya, and Samiksha. Multiply it by a weight vector to calculate each student's weighted score.

Exercise 6: Dimension Compatibility

Create matrices with shapes (2, 3) and (3, 4). Perform multiplication and identify the output shape. Then try multiplying shapes (2, 3) and (2, 4). Explain why the second operation fails.

Summary

  • The dot product multiplies corresponding vector elements and adds the products.
  • The @ operator performs matrix multiplication.
  • np.matmul() performs matrix multiplication.
  • np.dot() calculates dot products and performs multiplication for two-dimensional matrices.
  • The * operator performs element-wise multiplication.
  • The inner dimensions of two matrices must match.
  • A matrix with shape (m, n) multiplied by one with shape (n, p) produces shape (m, p).
  • Matrix-vector multiplication produces one result for each matrix row when the dimensions are compatible.
  • Matrix multiplication is generally not commutative.
  • Matrix multiplication can be applied to weighted scores, numerical analysis, and scientific computing.

You are now ready to use NumPy for vector dot products, matrix multiplication, and matrix-based calculations. In the next lesson, you will learn about determinants and matrix inverses.