NumPy Essentials
NumPy is the numerical engine of the Python data stack โ fast arrays, vectorized operations, and the foundation Pandas is built on.
Learning Objectives
- Explain what NumPy is and why it's the numerical engine of Python.
- Create arrays and inspect their attributes (
shape,dtype,ndim,size). - Use indexing and slicing to access elements.
- Understand vectorization and why it's faster than loops.
- Explain broadcasting between arrays of different shapes.
- Apply common functions:
mean,median,std,reshape.
1. What NumPy Is & Why It Matters
NumPy (Numerical Python) provides fast, multi-dimensional arrays and a large library of mathematical functions. It's the foundation that Pandas, Matplotlib, and most of the data stack are built on.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
2. Creating Arrays
import numpy as np
# From a Python list
arr = np.array([1, 2, 3, 4, 5])
# Arrays of zeros or ones
zeros = np.zeros(5) # [0. 0. 0. 0. 0.]
ones = np.ones((2, 3)) # 2x3 array of 1s
# A range (like Python's range, but returns an array)
r = np.arange(0, 10, 2) # [0 2 4 6 8]
# 2-D array (matrix-like)
m = np.array([[1, 2, 3], [4, 5, 6]])
3. Array Attributes
Every array has attributes describing its structure โ a common interview question.
m = np.array([[1, 2, 3], [4, 5, 6]])
m.shape # (2, 3) โ 2 rows, 3 columns
m.ndim # 2 โ number of dimensions
m.size # 6 โ total elements
m.dtype # dtype('int64') โ data type of elements
| Attribute | Meaning |
|---|---|
shape | dimensions (rows, columns, โฆ) |
ndim | number of dimensions |
size | total number of elements |
dtype | data type of the elements |
4. Indexing and Slicing
Indexing gets a single element; slicing gets a range of elements.
arr = np.array([10, 20, 30, 40, 50])
arr[0] # 10 (indexing โ first element)
arr[-1] # 50 (last element)
arr[1:4] # [20 30 40] (slicing โ elements 1 to 3)
# 2-D indexing
m = np.array([[1, 2, 3], [4, 5, 6]])
m[1, 2] # 6 (row 1, column 2)
arr[1:4] includes index 1, 2, 3 but not 4.
5. Vectorization โ the Whole Point
Vectorization applies an operation to an entire array at once, instead of looping over each element. This is why NumPy is dramatically faster than pure Python loops.
arr = np.array([1, 2, 3, 4, 5])
arr * 2 # [2 4 6 8 10] โ no loop needed
arr + 10 # [11 12 13 14 15]
arr ** 2 # [1 4 9 16 25]
# Two arrays, element-wise
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
a + b # [11 22 33]
sales * 1.18. With plain Python you'd write a loop over thousands of values.
6. Broadcasting
Broadcasting lets NumPy operate on arrays of different shapes by stretching the smaller one โ the simplest case is adding a scalar to every element.
arr = np.array([1, 2, 3])
arr + 100 # [101 102 103] โ the scalar 100 "broadcasts" to every element
# Column (3x1) + row (1x3) โ broadcasts to a 3x3
col = np.array([[1], [2], [3]])
row = np.array([[10, 20, 30]])
col + row
# [[11 21 31]
# [12 22 32]
# [13 23 33]]
7. Common Functions
arr = np.array([1, 2, 3, 4, 5])
np.mean(arr) # 3.0
np.median(arr) # 3.0
np.std(arr) # standard deviation
np.min(arr) # 1
np.max(arr) # 5
np.sum(arr) # 15
# Reshape
m = np.arange(6).reshape(2, 3) # [[0 1 2], [3 4 5]]
8. NumPy Array vs Python List
| Python list | NumPy array | |
|---|---|---|
| Element types | mixed allowed | single fixed dtype |
| Speed | slower (loops) | fast (vectorized) |
| Memory | higher | compact |
| Math ops | element-by-element loops | whole-array at once |
9. Interview Questions (with Model Answers)
The NumPy questions interviewers ask. Self-test before revealing.
IQ1. What is NumPy, and why is it used in data analysis?
Model answer: "NumPy is a library for numerical computing. It provides fast, multi-dimensional arrays and mathematical functions, and it's the foundation that Pandas and Matplotlib build on."
IQ2. What is an ndarray, and what are its main attributes?
Model answer: "An ndarray is NumPy's n-dimensional array. Its key attributes are shape (dimensions), ndim (number of dimensions), size (total elements), and dtype (element data type)."
IQ3. What's the difference between indexing and slicing?
Model answer: "Indexing returns a single element by position; slicing returns a range of elements. Slices are half-open โ arr[1:4] gives elements 1, 2, and 3."
IQ4. What is vectorization, and why is it faster than loops?
Model answer: "Vectorization applies an operation to a whole array at once using optimized C code under the hood, instead of looping element-by-element in Python. That's why it's dramatically faster."
IQ5. What is broadcasting in NumPy?
Model answer: "Broadcasting lets NumPy operate on arrays of different shapes by stretching the smaller one. The simplest example is adding a scalar to every element of an array."
IQ6. What's the difference between a NumPy array and a Python list?
Model answer: "A list can hold mixed types and is slower; a NumPy array holds a single fixed dtype, uses less memory, and supports fast vectorized operations โ which is what data work needs."
IQ7. How do you reshape an array?
Model answer: "With .reshape() โ for example,
np.arange(6).reshape(2, 3) turns a 1-D array of 6 into a 2ร3 matrix, as long as the total size matches."
IQ8. What statistical functions does NumPy provide?
Model answer: "The common ones โ mean, median, std, min, max, sum, and percentile โ all operate on the whole array at once, which is much faster than manual calculation."
IQ9. Why is NumPy faster than pure Python for numerical work?
Model answer: "Because NumPy is vectorized โ operations run in optimized compiled C over contiguous memory, avoiding Python's per-element interpreter overhead and loops."
Hands-On Project: NumPy in Action
Write NumPy code for each task. Run it and check the output.
Steps
- Create a NumPy array of daily sales:
[120, 150, 90, 200, 180]. - Print its shape, size, and dtype.
- Compute the total, mean, and max sales using vectorized functions.
- Add 18% tax to every value (vectorized, no loop).
- Select the first three days using slicing.
- Reshape a 1-D array of 6 values into a 2ร3 matrix.
View Solution / Walkthrough
import numpy as np
# 1. Daily sales
sales = np.array([120, 150, 90, 200, 180])
# 2. Attributes
print(sales.shape) # (5,)
print(sales.size) # 5
print(sales.dtype) # int64
# 3. Aggregate functions
print(sales.sum()) # 740
print(sales.mean()) # 148.0
print(sales.max()) # 200
# 4. Add 18% tax (vectorized)
with_tax = sales * 1.18 # no loop
# 5. First three days
first3 = sales[0:3] # [120 150 90]
# 6. Reshape 6 values into 2x3
m = np.arange(6).reshape(2, 3) # [[0 1 2], [3 4 5]]
Key Takeaways
NumPy is the fast array engine that Pandas is built on.
shape, ndim, size, and dtype describe an array.
Vectorization = whole-array operations, no loops โ that's the speed.
Broadcasting stretches smaller arrays to match larger ones.
Indexing = one element; slicing = a range (half-open).
Objective Questions โ Test Your Understanding
Q1. What is the primary data structure in NumPy?
Q2. What does "vectorization" mean in NumPy?
Q3. Which array attribute gives its dimensions (rows, columns)?
Q4. What is "broadcasting" in NumPy?
Q5. Which NumPy function creates an array filled with zeros?