Advanced NumPy Techniques

Python for Data Science 162 views Nov 14, 2025 2 min read

NumPy becomes powerful when you move beyond basics (array creation, slicing) and start using vectorization, broadcasting, views, strides, structured arrays, and performance tricks.


1. Vectorization (Fast Computation without Loops)

Vectorization allows replacing slow Python loops with fast NumPy operations.

❌ Slow (Python loop)

result = [x**2 for x in range(10_000)]

✔ Fast (NumPy vectorized)

import numpy as np arr = np.arange(10_000) result = arr ** 2

2. Broadcasting (Different Shapes Operations)

NumPy automatically “broadcasts” arrays of different sizes.

Example:

a = np.array([1, 2, 3]) b = 5 print(a + b) # [6, 7, 8]

Broadcasting Rules:

  1. Dimensions must be equal or

  2. One of them must be 1.


3. Advanced Indexing

✔ Boolean indexing

arr = np.array([10,20,30,40,50]) arr[arr > 25]

✔ Fancy indexing (index array)

arr[[0, 3, 4]] # pick elements

✔ 2D fancy indexing

mat = np.arange(16).reshape(4,4) mat[[0,2], [1,3]] # picks (0,1) and (2,3)

4. Views vs Copies (Memory Efficiency)

✔ View → shares memory

✔ Copy → separate memory

a = np.arange(10) b = a[2:6] # view b[0] = 999 print(a) # original changes

To force a copy:

c = a[2:6].copy()

5. Strides (Internal Memory Tricks)

Strides define how NumPy moves in memory.

Example: Create a sliding window view:

from numpy.lib.stride_tricks import sliding_window_view arr = np.arange(10) windows = sliding_window_view(arr, window_shape=3)

6. Vectorized String Operations

np.char.upper(['india', 'usa', 'uK']) np.char.replace(['patna', 'delhi'], 'a', '@')

7. Matrix Operations for ML

✔ Dot Product

np.dot(a, b)

✔ Matrix Multiplication

A @ B

✔ Transpose

A.T

✔ Determinant & Inverse

np.linalg.det(A) np.linalg.inv(A)

8. NumPy Broadcasting Tricks

Add vector to each row

A + b # shape (3,3) + (3,)

Add vector to each column

A + b[:, None]

9. Performance Tips

✔ Use vectorized operations

✔ Avoid Python loops

✔ Use astype() wisely

✔ Use in-place operations

arr += 5 # faster

✔ Use efficient data types

arr = arr.astype(np.float32)

✔ Preallocate arrays instead of appending


10. Structured Arrays (Like SQL Tables)

data = np.array([ (1, 'Ram', 25), (2, 'Shyam', 30) ], dtype=[('id','i4'), ('name','U10'), ('age','i4')]) data['name']

11. Random Module (ML Essential)

np.random.rand(3,3) np.random.randn(3,3) np.random.randint(1, 10, size=5) np.random.seed(42) # reproducibility

12. Concatenation & Splitting

np.concatenate([a, b]) np.vstack([A, B]) np.hstack([A, B]) np.split(arr, 3)

Summary of Advanced Concepts

TopicImportance
VectorizationSuper-fast ML calculations
BroadcastingAutomatic shape expansion
Fancy IndexingPowerful data selection
StridesMemory-efficient tricks
Structured ArraysSQL-like data handling
Linear AlgebraCore of ML models
Random ModuleData generation, ML training
Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials