Learn / NumPy
Advanced

Linear Algebra & Advanced Patterns

Matrix operations, sorting/searching, and the tricks that come up in real code.

FunctionWhat it findsExample

np.sort(a)

sorted copy

np.sort([3,1,2])array([1, 2, 3])

a.argsort()

indices that would sort the array

array([3,1,2]).argsort()array([1, 2, 0])

np.unique(a, return_counts=True)

distinct values + how often each appears

np.unique([1,1,2])(array([1, 2]), array([2, 1]))

np.where(cond) / np.argwhere(cond)

coordinates where a condition holds

np.where([1,-1,2] > 0)(array([0, 2]),)

np.searchsorted(a, v)

insertion point to keep a sorted array sorted

np.searchsorted([1,3,5], 4)2

⚠️ Watch out: np.reshape and matrix transpose are NOT the same operation: a frequent source of silent bugs when porting math notation into code. Reshape relabels the flat buffer; transpose actually reorders it.

Now practice it

Exercises that use what this lesson just covered.

Random Numbers & File I/OBack to overview →