NumPy Assignments — Vectorize. Compute. Optimize.
Level up from Python loops to high-performance array computing. Each topic-wise set includes 20 Basic, 20 Intermediate, and 10 Advanced questions so learners can practice intentionally and master real-world NumPy skills.
Why practice with these assignments?
- Go beyond theory—create arrays, index/slice, reshape, and broadcast against realistic tasks.
- Build numerical intuition with statistics, linear algebra, and ufuncs while comparing vectorized vs looped solutions.
- Learn performance habits: views vs copies, memory layout, and broadcasting without surprises.
How it works
- Open an assignment and solve in order: Basic → Intermediate → Advanced.
- Run code in a REPL or notebook; print shapes/dtypes and verify results with small examples.
- Prefer vectorized solutions first; then profile vs pure-Python loops for insight.
- Keep a short “what I learned” note—syntax + reasoning + gotchas—after each problem.
What you’ll achieve
- Fluency with array creation, attributes, random generation, and copy vs view semantics.
- Confident indexing, slicing, boolean masks, and fancy indexing across 1D/2D/3D arrays.
- Practical array math & broadcasting, chained ufuncs, and vectorization patterns.
- Solid stats & math toolbox: reductions, moments, trig, rounding, cumulative ops.
- Mastery of reshape/transpose/stack/split, axis transforms, and memory layout.
- Working knowledge of linear algebra (dot/matmul, det/inv/eig, norms, solve, SVD).
- concepts: structured/record/masked arrays, set ops, ordering, and performance.
Browse the Assignments
- NumPy Basics — creation (arange, linspace, zeros/ones/full/eye), random, dtype/shape/size, copy vs view.
- Array Indexing & Slicing — positional/negative slices, boolean/fancy indexing, 2D/3D picks, stride tricks, checkerboards/diagonals.
- Array Operations & Broadcasting — ufuncs, reductions, scalar/row/col broadcast, vectorization, timing vs loops.
- Statistical & Mathematical Functions — mean/median/std/var, argmin/argmax, trig, cumulative ops, column/row stats.
- Reshaping & Manipulating Arrays — reshape/transpose/swapaxis/moveaxis, stack/split, view vs copy (ravel vs flatten).
- Linear Algebra & Advanced Operations — dot/matmul, det/inv/eig, solve, norms, orthogonality, condition numbers, PCA-style tasks.
- Advanced NumPy Concepts — structured/record/masked arrays, set ops, sort/argsort/searchsorted, memory flags & performance.
Tips for success
- Print arr.shape/arr.dtype early; most bugs are shape/dtype mismatches.
- Prefer vectorization + broadcasting; benchmark against loops to see the gain.
- Know your copy vs view rules: ravel() often views; flatten() always copies.
- Control randomness with np.random.seed() when comparing solutions.
- Use np.linalg for stable, fast linear algebra—avoid rolling your own inverses/solvers.
Keep arrays contiguous when possible; transposes affect performance paths.
Ready to build real confidence in NumPy? Pick a set below and start solving!
FAQs
Q1. Which Python/NumPy versions should I use?
Any recent Python 3 with a current NumPy version (1.24 or higher) works. These tasks use mainstream APIs such as np.linalg
, ufuncs, and broadcasting that are consistent across modern versions.
Q2. How do I know if I wrote a vectorized solution correctly?
Compare your vectorized output to a simple loop baseline on small inputs. Then measure execution time for both to confirm the speedup. Several exercises guide you through this.
Q3. I’m confused by broadcasting—what should I check first?
Always check array shapes. Align singleton dimensions (for example, (n,1) vs (1,n)) and use np.newaxis
or None
when needed. The broadcasting assignments cover row/column and outer operations step by step.
Q4. Why do changes to a slice sometimes affect my original array?
NumPy slices often return views, not copies. Use arr.copy()
(or flatten()
) if you need independence. Note that ravel()
usually returns a view on contiguous arrays.
Q5. How do I choose the right dtype?
Pick the smallest safe dtype for your required range and precision. Watch out for integer division/overflow and float precision issues in statistics or linear algebra tasks. You’ll practice dtype checks and casting in basics and statistics sets.
Q6. What’s the quickest way to diagnose shape errors?
Print shapes before every major step and prefer axis-aware operations such as sum(axis=0/1)
or mean(axis=...)
. The reshape and transpose exercises train you to build this reflex.
Q7. Are masked or structured arrays important for beginners?
Yes. Masked arrays help handle missing or special values, while structured (record) arrays allow you to model labeled fields without pandas. The advanced set introduces both with practical tasks.
Q8. When should I use np.linalg.inv vs np.linalg.solve?
Prefer solve(A, b)
over forming inv(A) @ b
. It’s faster and numerically more stable. You’ll benchmark both approaches in the linear algebra assignments.
Q9. How do I keep results reproducible with randomness?
Set np.random.seed(...)
or use Generator.default_rng
. Always use the same seed when comparing algorithms such as normalization or statistical methods.
Q10. How much time should I allocate per assignment?
Plan 20–60 minutes per difficulty band. Tasks on linear algebra, broadcasting tricks, and performance comparisons may take longer—prioritize correctness and clarity first.