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

Introduction to NumPy

Learn what NumPy is, why it is important in Python, and how its multidimensional arrays help you perform numerical calculations efficiently.

1. Learning Objectives

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

  • Define NumPy and explain its purpose.
  • Understand the importance of numerical computing in Python.
  • Recognize the difference between Python lists and NumPy arrays.
  • Understand the basic structure of a NumPy array.
  • Identify common applications of NumPy.
  • Write a simple Python program using NumPy.

2. What Is NumPy?

NumPy stands for Numerical Python. It is a widely used Python library designed for numerical computing. It provides powerful multidimensional arrays and functions for working with numerical data.

NumPy makes it easier to perform mathematical calculations, process large collections of numbers, manipulate array shapes, calculate statistical values, and perform linear algebra operations.

A Practical Example

Imagine that a teacher wants to calculate the total marks of five students. The marks are 75, 82, 91, 68, and 84. NumPy can store these values in an array and calculate their total and average using built-in functions.

import numpy as np

marks = np.array([75, 82, 91, 68, 84])

total = np.sum(marks)
average = np.mean(marks)

print("Marks:", marks)
print("Total:", total)
print("Average:", average)

Output:

Marks: [75 82 91 68 84]
Total: 400
Average: 80.0
Tip: The statement import numpy as np imports NumPy and gives it the commonly used short name np.

3. Why Do We Need NumPy?

Python lists are useful for storing collections of values. However, numerical tasks often require repeated mathematical operations on many values. NumPy provides specialized arrays and operations that make these tasks convenient and efficient.

Python List 10 20 30 40 General-purpose collection Arithmetic often uses loops Flexible element types NumPy Array 10 20 30 40 Numerical computing Vectorized arithmetic Homogeneous element dtype
Figure 1: Both can store sequences of numbers, but NumPy arrays provide specialized numerical operations.
Feature Python List NumPy Array
Primary purpose General-purpose data storage. Numerical and array-based computing.
Element types Can contain different data types. Usually stores elements using one common data type.
Arithmetic Element-wise arithmetic usually requires explicit logic. Supports vectorized element-wise arithmetic.
Dimensions Nested lists can represent multidimensional structures. Provides explicit multidimensional array support.
Typical use General collections and application logic. Numerical analysis, matrices, and scientific computing.
Important: NumPy is not automatically faster for every task. Its main advantages are specialized array operations, convenient mathematical functions, and efficient numerical processing for suitable workloads.

4. Understanding NumPy Arrays

The central data structure in NumPy is the ndarray, which stands for N-dimensional array. It stores elements in a structured arrangement and supports operations across one or more dimensions.

One-Dimensional Array

import numpy as np

numbers = np.array([10, 20, 30, 40])

print(numbers)
print("Dimensions:", numbers.ndim)
print("Shape:", numbers.shape)
print("Size:", numbers.size)

Output:

[10 20 30 40]
Dimensions: 1
Shape: (4,)
Size: 4

This array has one dimension, contains four elements, and has a shape of (4,).

Two-Dimensional Array

import numpy as np

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(matrix)
print("Dimensions:", matrix.ndim)
print("Shape:", matrix.shape)
print("Size:", matrix.size)

Output:

[[1 2 3]
 [4 5 6]]
Dimensions: 2
Shape: (2, 3)
Size: 6

This array has two dimensions, two rows, three columns, and six elements in total.

Remember: The ndim attribute reports the number of dimensions, shape describes the size along each dimension, and size reports the total number of elements.

5. Important Features of NumPy

1. Multidimensional Arrays

NumPy supports one-dimensional arrays, two-dimensional matrices, and arrays with additional dimensions.

2. Vectorized Operations

Many operations can be applied to an entire array without writing an explicit Python loop.

import numpy as np

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

print(values * 2)

Output:

[2 4 6 8]

3. Mathematical Functions

NumPy provides functions for sums, averages, square roots, trigonometric calculations, and many other numerical tasks.

4. Statistical Operations

You can calculate values such as the mean, median, standard deviation, minimum, and maximum.

5. Linear Algebra

NumPy includes operations for matrix multiplication, solving linear systems, and computing eigenvalues.

6. Where Is NumPy Used?

Field Example Application
Data Analysis Calculating averages, totals, and summary statistics.
Machine Learning Representing numerical features and performing array calculations.
Scientific Computing Processing experimental measurements and numerical models.
Finance Analyzing numerical time series and financial calculations.
Image Processing Representing image pixels as multidimensional arrays.
Statistics Calculating distributions and descriptive statistics.
Practical insight: NumPy is also a foundation for many Python data science libraries, including pandas and scikit-learn.

7. Your First NumPy Program

The following program creates an array of product prices, increases every price by 10%, and calculates the average price.

import numpy as np

prices = np.array([100, 200, 300, 400])

updated_prices = prices * 1.10
average_price = np.mean(prices)

print("Original prices:", prices)
print("Updated prices:", updated_prices)
print("Average price:", average_price)

Output:

Original prices: [100 200 300 400]
Updated prices: [110. 220. 330. 440.]
Average price: 250.0

How the Program Works

  1. import numpy as np imports the NumPy library.
  2. np.array() creates an array from the supplied values.
  3. prices * 1.10 increases each price by 10%.
  4. np.mean(prices) calculates the average original price.
  5. print() displays the results.
Before running the program: NumPy must be installed in the Python environment selected by your IDE. Installation and environment setup are covered in the next lessons.

8. Common Beginner Mistakes

Mistake 1: Using NumPy Without Importing It

Calling np.array() before importing NumPy with the alias np causes a name-related error.

Mistake 2: Confusing Lists and Arrays

Multiplying a Python list by an integer repeats the list, while multiplying a numerical NumPy array by a number multiplies each element.

python_list = [1, 2, 3]
print(python_list * 2)

import numpy as np

numpy_array = np.array([1, 2, 3])
print(numpy_array * 2)

Output:

[1, 2, 3, 1, 2, 3]
[2 4 6]

Mistake 3: Assuming Every Array Has the Same Shape

Array operations involving different shapes must satisfy NumPy's broadcasting rules. Not every pair of shapes can be combined directly.

9. Classroom Practical Activity

Activity: Analyze Student Marks

Create a NumPy array containing the marks of five students. Calculate the total, average, highest mark, and lowest mark.

  1. Create an array containing 72, 85, 91, 64, and 78.
  2. Display the complete array.
  3. Calculate the total marks using np.sum().
  4. Calculate the average using np.mean().
  5. Find the highest and lowest marks.
  6. Display each result with a meaningful label.

Starter Code

import numpy as np

marks = np.array([72, 85, 91, 64, 78])

# Calculate the total
total = np.sum(marks)

# Calculate the average
average = np.mean(marks)

# Find the highest and lowest marks
highest = np.max(marks)
lowest = np.min(marks)

print("Marks:", marks)
print("Total:", total)
print("Average:", average)
print("Highest:", highest)
print("Lowest:", lowest)

Practice Questions

  1. What does NumPy stand for?
  2. What is the purpose of a NumPy array?
  3. What does import numpy as np mean?
  4. How does NumPy array multiplication differ from Python list multiplication?
  5. What is the difference between an array's shape and size?
  6. Name three areas where NumPy is useful.
  7. Which NumPy function calculates the average of numerical values?

Summary

NumPy is a Python library for numerical computing. Its main data structure is the multidimensional ndarray, which supports array-based mathematical operations, statistics, and linear algebra.

You learned how to import NumPy, create arrays, perform element-wise calculations, and calculate basic statistical values. You also explored the differences between Python lists and NumPy arrays.