NumPy Basics

Updated · 4 min read

NumPy is a Python library for working with arrays: grids of numbers stored efficiently in memory. It is used for fast math on large amounts of data, and it is the foundation of almost every AI and data library, including pandas and the major deep learning frameworks. Images, embeddings and model weights are all arrays of numbers.

What Is a Vectorized Operation

A vectorized operation is a single operation applied to every value in an array at once, without writing a loop. It is used because it is much shorter to write and much faster to run.

One operation, every value

Common Operations

OperationDescription
np.array([...])It is used to create an array from a list
a * 1.05It is used to multiply every value (any math operator works)
a.sum(), a.mean(), a.max()It is used to summarize the values
a[a > 40]It is used to keep only values that meet a condition
a.shapeIt is used to see the array's dimensions
a.sum(axis=1)It is used to sum across each row of a 2-D array

Example

Python
import numpy as np
 
fees = np.array([45, 30, 15, 120, 250])     # one fee per item
print("Array:", fees)
print("Plus 5%:", fees * 1.05)               # one operation on every value
print("Total:", fees.sum(), "Mean:", fees.mean(), "Max:", fees.max())
print("Above $40:", fees[fees > 40])          # filter with a condition
 
students = np.array([[45, 30], [50, 0], [45, 15]])   # 3 students x 2 fees
print("Shape:", students.shape)
print("Total per student:", students.sum(axis=1))
Output
Array: [ 45  30  15 120 250]
Plus 5%: [ 47.25  31.5   15.75 126.   262.5 ]
Total: 460 Mean: 92.0 Max: 250
Above $40: [ 45 120 250]
Shape: (3, 2)
Total per student: [75 50 60]

Explanation: fees * 1.05 multiplies all five values in one step. fees > 40 creates a true or false value for each fee, and fees[...] keeps only the true ones. The 2-D array has 3 rows (students) and 2 columns (fees), so its shape is (3, 2); axis=1 adds across each row.

Common Mistakes

Looping over arrays element by element. Vectorized operations are simpler and much faster.

Mixing up axis 0 and axis 1. Check the shape of the result.

Using and/or in conditions. Use & and | with parentheses: (a > 10) & (a < 100).

Interview Corner

Why is NumPy faster than plain Python lists for math?

NumPy stores numbers in compact arrays and runs vectorized operations in optimized compiled code, instead of looping in Python one value at a time.

What does axis=1 mean in a.sum(axis=1)?

Sum across each row, producing one value per row. axis=0 sums down each column.

Practice

Exercise: student fees. Add a fourth student [50, 30] to the students array. Print the total per student, the total per fee type (axis=0), and the students whose total is above $60. Check each result by hand.

Check your understanding:

  1. What is a vectorized operation?
  2. What does fees[fees > 40] return?
  3. What does axis=1 do?

Quick check: NumPy basics

4 questions
1What does fees * 1.05 do?
2What does fees[fees > 40] return?
3What is the shape of a 3 × 2 array?
4What does students.sum(axis=1) give?

Key Takeaways

Quick reference

CodeResult
a * 2Every value doubled
a[a > 0]Only positive values
a.mean()The average
a.sum(axis=1)Row totals

Was this tutorial helpful?

Lesson 78 of 186