NumPy

1. What is NumPy really? (The honest explanation)

NumPy = Numerical Python

It’s the foundation library for almost everything serious in data science, machine learning, scientific computing, image processing, finance, etc.

Main superpowers:

  • Extremely fast arrays (called ndarray)
  • Vectorized operations (no slow Python loops)
  • Broadcasting (magic shape matching)
  • Linear algebra, statistics, random numbers, Fourier transforms…
  • Memory efficient compared to Python lists

Rule of thumb you should burn into your brain:

If you’re doing numerical computation in Python and you’re using loops → you’re probably doing it wrong.

2. First things first — Importing NumPy

Python

Almost everyone uses np as the alias. Just accept it 😄

3. Creating NumPy Arrays (The Most Important Skill)

Python

Quick tip: np.random.seed(42) — makes random results reproducible (very important when debugging)

4. Understanding .shape, .ndim, .size, .dtype

Python

Memorize this order: (depth, rows, columns) or (z, y, x)

5. Super Important: NumPy is PASS BY REFERENCE (not like Python lists!)

Python

6. Vectorization — The Reason NumPy is Fast

Bad (slow Python style):

Python

Good (NumPy way — 10–100× faster):

Python

All these work element-wise:

Python

7. Broadcasting — The Magic You’ll Love & Hate

Rules (very simple actually):

  1. Dimensions must be equal or
  2. One of them is 1 → it gets stretched

Examples that work:

Python

Example that fails:

Python

8. Indexing & Slicing (very powerful)

Python

9. Reshaping, Transpose, Flatten

Python

10. Important Aggregation Functions

Python

11. Mini Real-Life Examples

Normalize data (very common in ML)

Python

Image as array (RGB example)

Python

Distance between all pairs of points

Python

Quick Reference Table (Keep this somewhere)

Operation Syntax
Create array np.array(), np.zeros(), np.linspace()
Element-wise math + – * / ** sin exp log …
Matrix multiply np.dot(a,b) or a @ b (Python 3.5+)
Transpose a.T
Reshape a.reshape(3,4,-1)
Flatten a.ravel() or a.flatten()
Concatenate np.concatenate(…, axis=0)
Stack np.vstack(), np.hstack()
Boolean indexing a[a > 5]
Where (if-else) np.where(condition, x, y)

Would you like to go deeper into any of these topics?

  • Linear algebra (np.linalg)
  • Advanced indexing & views vs copies
  • Masked arrays
  • Performance tricks
  • Common mistakes people make
  • NumPy + Pandas + Matplotlib mini project

Just tell me where you want to zoom in! 🚀