Skip to content
C

NumPy

Complete learning notes


1. Introduction

Almost every machine learning library — including Pandas, Scikit-learn, TensorFlow, and PyTorch — is built on top of NumPy. Before we can work with real datasets, we need to understand NumPy, the library that lets Python handle numerical data quickly and efficiently using a special data structure called an array.


2. What is NumPy?

Simple definition: NumPy (Numerical Python) is a Python library that provides fast, memory-efficient arrays and mathematical functions for working with numbers.

Technical explanation: NumPy introduces the ndarray (n-dimensional array) object, along with a large collection of optimized mathematical operations that work directly on entire arrays at once, instead of looping through elements one by one in plain Python.


3. Why is it Important?

  • Datasets in ML are fundamentally large collections of numbers — NumPy handles this efficiently.
  • Operations on NumPy arrays are significantly faster than equivalent operations on plain Python lists, because NumPy performs calculations in optimized, low-level code.
  • Nearly every ML and data science library depends on NumPy arrays internally.

4. Prerequisites

You should be comfortable with Python basics, data types, loops, and functions (Topics 1–3). NumPy is usually installed separately using pip install numpy and imported using import numpy as np.


5. Core Concepts

  1. Creating NumPy arrays
  2. Array shape and dimensions
  3. Indexing and slicing
  4. Vectorized operations (math on whole arrays at once)
  5. Broadcasting
  6. Useful array-generation functions (zeros, ones, arange, linspace)
  7. Basic statistical functions (mean, sum, max, min, std)
  8. Reshaping arrays

6. Detailed Explanation

a) Creating Arrays

A NumPy array is created using np.array(), typically from a Python list. Unlike a list, all elements in a NumPy array are expected to be the same data type, which is part of what makes it so fast.

b) Shape and Dimensions

Every array has a "shape" — describing how many elements exist along each dimension. A 1D array is a simple sequence of numbers; a 2D array is like a table (rows and columns).

c) Indexing and Slicing

You can access individual elements or ranges of elements using square brackets, very similar to Python lists, but extended to work across multiple dimensions.

d) Vectorized Operations

Instead of writing a loop to add 1 to every element, NumPy lets you write array + 1 directly, and it applies the operation to every element internally — this is called "vectorization," and it's both faster and more readable.

e) Broadcasting

Broadcasting is NumPy's ability to perform operations between arrays of different (but compatible) shapes, automatically "stretching" the smaller one to match, without actually copying data.

f) Array-Generation Functions

  • np.zeros(shape) creates an array filled with zeros.
  • np.ones(shape) creates an array filled with ones.
  • np.arange(start, stop, step) creates an array of evenly spaced values, similar to Python's range().
  • np.linspace(start, stop, num) creates a given number of evenly spaced values between two numbers.

g) Statistical Functions

NumPy provides built-in functions like np.mean(), np.sum(), np.max(), np.min(), and np.std() (standard deviation) that work directly on arrays.

h) Reshaping

array.reshape(rows, cols) changes an array's shape without changing its data — useful for converting between 1D and 2D forms.


7. How It Works

  1. NumPy stores array data in a single, contiguous block of memory (unlike Python lists, which store references scattered in memory).
  2. When you perform an operation like array * 2, NumPy applies it directly across that memory block using optimized low-level code (written in C).
  3. This avoids the overhead of Python's normal loop mechanism, making operations on large arrays dramatically faster.

8. Real-World Example

Imagine you have exam marks for 1,000 students and want to add 5 bonus marks to everyone. With a plain Python list, you'd loop through all 1,000 values one at a time. With a NumPy array, you simply write marks + 5, and NumPy applies it to all 1,000 values instantly and efficiently — like a factory machine stamping all products at once instead of one worker doing it by hand, item by item.


9. Technical Example

python
import numpy as np marks = np.array([70, 85, 90, 60]) bonus_marks = marks + 5 print(bonus_marks)

Here, marks + 5 adds 5 to every element in one vectorized operation — no loop required.


10. Python Example

python
import numpy as np # Creating a 1D array marks = np.array([70, 85, 90, 60, 75]) print("Marks array:", marks) # Creating a 2D array (like a table) matrix = np.array([[1, 2, 3], [4, 5, 6]]) print("Matrix:\n", matrix) print("Shape of matrix:", matrix.shape) # Indexing and slicing print("First mark:", marks[0]) print("First three marks:", marks[0:3]) # Vectorized operation - add 5 bonus marks to everyone bonus_marks = marks + 5 print("Marks with bonus:", bonus_marks) # Statistical functions print("Average marks:", np.mean(marks)) print("Highest mark:", np.max(marks)) print("Lowest mark:", np.min(marks)) print("Standard deviation:", np.std(marks)) # Array generation functions zeros_array = np.zeros(4) ones_array = np.ones(4) range_array = np.arange(0, 10, 2) print("Zeros:", zeros_array) print("Ones:", ones_array) print("Range array:", range_array) # Reshaping an array numbers = np.arange(1, 7) reshaped = numbers.reshape(2, 3) print("Original:", numbers) print("Reshaped:\n", reshaped)

Expected Output:

text
Marks array: [70 85 90 60 75] Matrix: [[1 2 3] [4 5 6]] Shape of matrix: (2, 3) First mark: 70 First three marks: [70 85 90] Marks with bonus: [75 90 95 65 80] Average marks: 76.0 Highest mark: 90 Lowest mark: 60 Standard deviation: 10.319883720275544 Zeros: [0. 0. 0. 0.] Ones: [1. 1. 1. 1.] Range array: [0 2 4 6 8] Original: [1 2 3 4 5 6] Reshaped: [[1 2 3] [4 5 6]]

11. Code Explanation

  • np.array([70, 85, 90, 60, 75]) converts a normal Python list into a fast NumPy array.
  • matrix.shape returns (2, 3), meaning the array has 2 rows and 3 columns.
  • marks[0:3] slices the array to get the first three elements, using the same slicing style as Python lists.
  • marks + 5 demonstrates vectorization — the addition is applied to every element without writing a loop.
  • np.mean(), np.max(), np.min(), and np.std() compute statistics directly across the whole array in one call.
  • np.arange(0, 10, 2) generates numbers from 0 up to (not including) 10, stepping by 2 each time.
  • numbers.reshape(2, 3) takes the same 6 values and rearranges them into a 2-row, 3-column layout, without changing the underlying data.

12. Advantages

  • Much faster than plain Python lists for numerical operations, especially on large datasets.
  • Supports powerful vectorized operations, avoiding manual loops.
  • Provides many built-in mathematical and statistical functions.
  • Forms the numerical foundation for nearly all ML/data science libraries.

13. Limitations

  • All elements in a NumPy array must be the same data type, unlike Python lists which can mix types.
  • Slightly steeper learning curve for beginners compared to plain lists.
  • Not ideal for non-numeric or highly irregular data structures.

14. Common Mistakes

  • Forgetting to import NumPy (import numpy as np) before using it.
  • Confusing a NumPy array's shape order — (rows, columns), not (columns, rows).
  • Trying to reshape an array into a shape that doesn't match its total number of elements.
  • Mixing data types unintentionally, which can silently convert numbers into an unexpected type.

15. Best Practices

  • Use np.array() for numeric data instead of plain Python lists whenever performance matters.
  • Prefer vectorized operations over manual loops for speed and cleaner code.
  • Always check array.shape when working with multi-dimensional data to avoid confusion.
  • Use meaningful variable names for arrays representing real data (e.g., student_marks, not arr1).

16. Real-World Applications

  • Storing and processing image data, where pixels are represented as multi-dimensional NumPy arrays.
  • Performing fast mathematical calculations on large datasets before feeding them into ML models.
  • Powering internal calculations of libraries like Pandas, Scikit-learn, and TensorFlow.

17. Interview-Oriented Points

  • Be ready to explain why NumPy arrays are faster than Python lists.
  • Understand what vectorization means and why it matters for performance.
  • Know the difference between a 1D array and a 2D array, and how to check an array's shape.
  • Be able to explain broadcasting in simple terms.

18. Exam-Oriented Points

  • NumPy provides the ndarray object for fast numerical computation.
  • Vectorized operations apply a calculation to an entire array at once, without explicit loops.
  • shape describes an array's dimensions; reshape() changes its layout without changing its data.
  • Common statistical functions: mean(), sum(), max(), min(), std().

19. Comparison Table — NumPy Array vs Python List

AspectNumPy ArrayPython List
Speed for numeric operationsMuch fasterSlower
Data typeMust be the same type throughoutCan mix different types
Built-in math operationsYes (vectorized)No (requires manual loops)
Memory efficiencyMore efficient for large numeric dataLess efficient
Multi-dimensional supportYes (native)Requires nested lists (less convenient)

20. Quick Revision

  • NumPy provides fast, memory-efficient arrays (ndarray) for numerical computing.
  • Vectorized operations let you apply math to an entire array at once, without loops.
  • shape tells you an array's dimensions; reshape() rearranges data into a new shape.
  • Common functions: np.array(), np.zeros(), np.ones(), np.arange(), np.mean(), np.max(), np.min(), np.std().
  • NumPy arrays require a single data type, unlike Python lists.

Mock Test

  • NumPy — Quick Test

    A 10-question multiple-choice check on NumPy.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems

  • Problem 1: Array Statistics Calculator
    Easy · python
    Solve Problem
  • Problem 2: Add Bonus Marks Using Vectorization
    Easy · python
    Solve Problem
  • Problem 3: Reshape a 1D Array into a 2D Grid
    Easy · python
    Solve Problem
  • Problem 4: Compare Two Arrays Element-Wise
    Easy · python
    Solve Problem