Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

NumPy is a Python library used to store and process large sets of numbers efficiently. This cheat sheet covers the array operations students need most often, including creating arrays, selecting values, reshaping data, and applying calculations. It is useful for data science, simulations, image processing, and scientific computing because arrays make numerical code faster and cleaner.

Students in grades 10-12 can use it as a quick reference while writing Python programs.

Key Facts

  • Create a NumPy array with np.array([1, 2, 3]) after importing the library with import numpy as np.
  • Use arr.shape to get the size of each dimension, such as (3, 4) for an array with 3 rows and 4 columns.
  • Index a 2D array with arr[row, column], so arr[1, 2] selects the value in row index 1 and column index 2.
  • Slice arrays with start:stop:step, such as arr[0:5:2], which selects indices 0, 2, and 4.
  • Vectorized operations apply to every element, so arr * 2 doubles each value without writing a loop.
  • Broadcasting lets NumPy combine compatible shapes, such as adding a shape (3,) array to each row of a shape (2, 3) array.
  • Aggregation functions summarize data, such as np.sum(arr), np.mean(arr), np.max(arr), and np.min(arr).
  • Boolean masks filter arrays, so arr[arr > 10] returns only the values greater than 10.

Vocabulary

NumPy
NumPy is a Python library for fast numerical computing with arrays.
Array
An array is a structured collection of values stored in one or more dimensions.
Shape
The shape of an array tells how many elements are in each dimension.
Index
An index is the position number used to access an element in an array, starting at 0 in Python.
Broadcasting
Broadcasting is NumPy's rule system for applying operations to arrays with compatible but different shapes.
Boolean mask
A Boolean mask is an array of True and False values used to select matching elements from another array.

Common Mistakes to Avoid

  • Using arr[row][column] instead of arr[row, column], which can work but is less direct and can be slower or confusing for multidimensional arrays.
  • Forgetting that slicing stops before the stop index, so arr[1:4] includes indices 1, 2, and 3, not index 4.
  • Trying to combine arrays with incompatible shapes, which causes a broadcasting error because NumPy cannot match the dimensions.
  • Using Python loops for simple element-by-element math, which is slower and less readable than vectorized operations like arr + 5 or arr * arr.
  • Changing an array slice and expecting the original to stay unchanged, which is wrong because many NumPy slices are views of the original array.

Practice Questions

  1. 1 Given arr = np.array([4, 8, 12, 16]), what is the result of arr / 4?
  2. 2 Given a = np.array([[1, 2, 3], [4, 5, 6]]), what value is selected by a[1, 2]?
  3. 3 Write a NumPy expression that returns only the values greater than 50 from an array named scores.
  4. 4 Explain why arr * 3 is usually better than writing a for loop to multiply every element of arr by 3.

Understanding NumPy Array Operations Reference

A NumPy array has more structure than an ordinary Python list. Its values usually share one data type, such as integers or decimal numbers, and they are stored in a compact block of memory. This layout helps the computer process many values at once.

Before changing an array, inspect its shape, number of dimensions, and data type. A result that looks wrong often comes from a shape mismatch or from using integer data when decimal values are needed. Integer arrays can discard the decimal part during some assignments, which can silently change a calculation.

Dimensions give meaning to data. A one dimensional array might represent temperatures measured each hour. A two dimensional array might store students in rows and test scores in columns.

A three dimensional array can represent a color image, with height, width, and color channels. When using a summary function, pay close attention to the axis setting. Summarising across rows produces one result for each column.

Summarising across columns produces one result for each row. This matters when finding each student's average score instead of the average score on each test.

Broadcasting works by lining up dimensions from the right and extending a dimension of size one when needed. This is useful for tasks such as subtracting the average of each column from every value in that column. It avoids repeated loops and makes the intended calculation easier to see.

However, compatible shapes can still produce an unintended result. A one dimensional array may be treated as a row when a column was intended.

Reshaping it into a single column can fix that situation. Students should write down the shape of each intermediate result while learning, especially after selecting part of an array or reducing a dimension.

Indexing and slicing need care because Python starts counting at zero, and the stopping position of a slice is excluded. Another important detail is that many slices are views of the original data rather than separate copies. Changing a view can change the original array.

Use an explicit copy when the selected data must be edited independently. Boolean masks are especially useful in real data work. They can remove missing measurements, select pixels above a brightness threshold, or find scores outside an expected range.

Combine conditions carefully, using parentheses, because each comparison produces an array of true and false values. These skills make array code easier to check, faster to run, and less likely to hide data errors.