Chapter 1: NumPy Tutorial

NumPy – The Real Beginning (What You Actually Need to Know First)

NumPy is not just “faster lists”. It is a completely different way of thinking about data.

Most important mindset change:

In normal Python → you think item by item In NumPy → you think whole array at once

This single change makes your code 10–100× faster and much cleaner.

Python

(We almost always use np — just accept this convention)

1. Creating arrays – the 6 most common ways people really use

Python

Quick tip people forget: randn → normal distribution rand → uniform [0,1)

2. The 4 things you must check every time you create an array

Python

Memory layout reminder (very useful when debugging):

text

Example real meaning:

Python

3. The golden rule: NumPy almost never copies data by default

This is where 80% of beginners get confused and angry.

Python

View vs Copy cheat sheet (very important)

Operation Usually View or Copy?
b = a View (same data)
b = a.copy() Copy
b = a[:] View (most cases)
b = a[::2] View
b = a.reshape(…) View (if possible)
b = a.T View
b = a[a > 5] Copy

4. Vectorization – why NumPy feels like magic

Classic slow Python:

Python

NumPy version (10–100× faster):

Python

All these operations are vectorized (done on whole array at once):

Python

5. Broadcasting – the feature that feels like cheating

Broadcasting rule (memorize this):

Two arrays can be used together if: • their dimensions are equal, or • one of them has dimension 1 (it gets stretched)

Examples that work:

Python

Examples that fail:

Python

6. Indexing & Slicing – real-world patterns

Python

7. Reshaping & stacking – daily bread

Python

8. Most useful statistics & reductions

Python

Very common normalization (you will write this 1000 times):

Python

9. Quick real-life mini-examples you will actually use

Example 1: Remove outliers

Python

Example 2: Simple moving average

Python

Example 3: Distance matrix between points

Python

Summary Table – Print & Keep

Operation Most common syntax
Create array np.array(), zeros(), ones(), arange(), linspace()
Random rand(), randn(), randint()
Shape info .shape, .ndim, .size, .dtype
Copy .copy()
Math on arrays + – * / ** sin exp log sqrt abs
Matrix multiply a @ b or np.dot(a,b)
Transpose a.T
Reshape reshape(), -1 is magic
Stack vstack, hstack, concatenate
Boolean select arr[arr > 5]
Where np.where(cond, value_if_true, value_if_false)

Where do you want to go deeper next?

  • Matrix operations & np.linalg (eigenvalues, SVD, solve…)
  • Advanced indexing & memory views vs copies in detail
  • Performance tricks & when vectorization fails
  • Common bugs & how to avoid them
  • NumPy + matplotlib mini visualization session
  • NumPy in machine learning (data preparation patterns)

Tell me what feels most useful for you right now! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *