Advanced NumPy Techniques

πŸ“˜ Python for Data Science πŸ‘ 76 views πŸ“… Nov 14, 2025
⏱ Estimated reading time: 2 min

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
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes