Checkpoint: Array Operations
A checkpoint is a review lesson that consolidates a whole section — here you revisit splitting, axis, fancy indexing, NaN handling, counting, set operations, rounding, and cumulative functions in one place.
Learn Checkpoint: Array Operations in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll recap every tool, tackle a multi-step build challenge with a full worked solution, and test yourself with a short checkpoint quiz.
Every tool from this section, with the one-liner that does the job:
Before the big challenge, fire off one example of each tool to refresh your memory.
You are handed a small 2D table of sensor readings (rows = sensors, columns = hours). It has a missing value and an outlier. Build a short pipeline that:
Try it in the editor first. The starter sets up the data and leaves the steps for you. The full solution is below if you get stuck.
It collapses the rows, giving a per-column result.
✅ Fix: the named axis disappears — sum(axis=0) is one total per column.
✅ Fix: use np.isnan(arr) ; reduce with np.nanmean .
diff returns one fewer element than its input.
✅ Fix: use prepend= or slice the original to match.
From a list of order categories, find the distinct categories with their counts, then the most common one — using only tools from this section.
Answer each in your head, then expand to check.
np.array_split(arr, 3) — it allows uneven pieces, making the earlier ones one element longer. np.split would raise a ValueError.
A length-3 array — one total per row. axis=1 collapses the 4 columns, so the result has shape (3,) .
By the IEEE-754 standard, NaN is defined to never equal anything, including itself. That is why you must test with np.isnan() instead of == .
A copy. Modifying the result of arr[[0, 2]] does not change the original. Basic slicing like arr[0:2] returns a view that does share memory.
values, counts = np.unique(arr, return_counts=True) . The values come back sorted, and counts lines up with them position by position.
They are inverses. cumsum builds running totals; diff recovers the step-by-step changes. Note diff returns one fewer element unless you use prepend= .
Checkpoint complete — array operations consolidated!
You can split and join, reason about axis , index fancily, clean NaN and outliers, count and compare sets, round and clip, and accumulate or difference — and combine them into a real pipeline.
🚀 Up next: meshgrid & Coordinate Grids — build 2D coordinate grids to evaluate functions across a plane.
Practice quiz
Which function splits a length-7 array into 3 parts without raising an error?
- np.split(arr, 3)
- np.array_split(arr, 3)
- np.hsplit(arr, 3)
- np.vsplit(arr, 3)
Answer: np.array_split(arr, 3). np.array_split allows uneven pieces, making the earlier ones one element longer. np.split requires the array to divide evenly and would raise a ValueError.
For a (3, 4) array, what shape does arr.sum(axis=1) return?
- (4,)
- (3,)
- (3, 4)
- a single scalar
Answer: (3,). The named axis collapses. axis=1 removes the 4 columns, leaving one total per row, so the result has shape (3,).
What does np.clip(arr, 0, 100) do to a value of 999?
- Removes it from the array
- Leaves it as 999
- Caps it to 100
- Replaces it with NaN
Answer: Caps it to 100. clip caps any value above the upper bound to that bound, so 999 becomes 100. Values below 0 would be raised to 0.
Why does the test arr[arr == np.nan] always return an empty array?
- NaN never equals anything, even itself, so the comparison is always False
- == is not defined for arrays
- np.nan is actually zero
- You must use a list instead of an array
Answer: NaN never equals anything, even itself, so the comparison is always False. By the IEEE-754 standard NaN compares unequal to everything, including itself, so == finds nothing. Use np.isnan(arr) instead.
How do you get the distinct values of an array together with how often each appears?
- np.bincount(arr)
- np.sort(arr)
- np.unique(arr, return_counts=True)
- np.intersect1d(arr, arr)
Answer: np.unique(arr, return_counts=True). np.unique(arr, return_counts=True) returns the sorted distinct values and a counts array lined up position by position.
Does fancy indexing like arr[[0, 2]] return a copy or a view?
- A view that shares memory
- A copy
- It depends on the dtype
- It raises an error
Answer: A copy. Fancy (integer-array) indexing always returns a copy. Basic slicing such as arr[0:2] returns a view that shares memory with the original.
Which call keeps only the values that appear in BOTH arrays a and b?
- np.union1d(a, b)
- np.setdiff1d(a, b)
- np.intersect1d(a, b)
- np.isin(a, b)
Answer: np.intersect1d(a, b). np.intersect1d returns the sorted, unique values common to both arrays. union1d combines them, setdiff1d subtracts, and isin returns a boolean mask.
What is the relationship between np.cumsum and np.diff?
- They are unrelated
- They are inverses of each other
- Both return one fewer element than the input
- Both require an axis argument
Answer: They are inverses of each other. cumsum builds running totals while diff recovers the step-by-step changes, so they undo each other. Note diff returns one fewer element unless you use prepend=.
Which NumPy function computes the mean while skipping NaN values?
- np.mean
- np.nanmean
- np.average
- np.nan_to_num
Answer: np.nanmean. np.nanmean ignores NaN entries when averaging. A plain np.mean would be poisoned and return NaN if any element is NaN.
After np.diff on a length-5 array, how many elements does the result have?
- 6
- 5
- 4
- 1
Answer: 4. diff returns the differences between consecutive elements, which is one fewer than the input, so a length-5 array yields a length-4 result.