Python Docs

NumPy for Numerical Operations

NumPy is the backbone of numerical computing in Python. It provides high-performance multidimensional arrays, vectorized operations, broadcasting, mathematical functions, linear algebra, and random number utilities — essential for data science, ML, and scientific simulation.

Why NumPy for Numerical Computing?

  • Fast vectorized operations without Python loops
  • Efficient memory usage through contiguous arrays
  • High-level mathematical functions
  • Broadcasting for shape-flexible arithmetic
  • Linear algebra routines (matrix multiplication, decomposition)
  • Random sampling for ML, simulations, and statistics

Basic Array Operations

NumPy arrays support element-wise arithmetic, aggregation, and reshaping, much faster than Python lists.

Example: Basic Operations

import numpy as np

a = np.array([2, 4, 6])
b = np.array([1, 3, 5])

print(a + b)        # element-wise addition
print(a * b)        # element-wise multiplication
print(a ** 2)       # exponentiation
print(a / 2)        # vectorized division
print(a.mean())     # average
print(a.sum())      # sum of all elements

Broadcasting

Broadcasting allows NumPy to perform arithmetic on arrays with different shapes by “stretching” dimensions automatically.

# Shapes:
# a → (3,) 
# b → (2, 3)

a = np.array([1, 2, 3])
b = np.arange(6).reshape(2, 3)

print(b + a)

Broadcasting Diagram:

a:      [1, 2, 3]
b:     [[0, 1, 2],
        [3, 4, 5]]

b + a: [[1, 3, 5],
        [4, 6, 8]]

Mathematical Functions

NumPy offers a wide range of universal functions (ufuncs) that run fast in C — not Python.

Example: Math Operations

x = np.linspace(0, 2*np.pi, 100)

print(np.sin(x))
print(np.cos(x))
print(np.exp(x))
print(np.log(x + 1))  # avoid log(0)

Linear Algebra Operations

NumPy includes matrix multiplication, determinants, inversion, eigenvalues, QR decomposition, and more.

Example: Matrix Algebra

A = np.array([[2, 1],
               [3, 4]])

b = np.array([5, 6])

print(A @ b)                 # matrix-vector multiply
print(np.linalg.det(A))      # determinant
print(np.linalg.inv(A))      # inverse
print(np.linalg.eig(A))      # eigenvalues & eigenvectors

Random Number Generation

NumPy’s random module is essential for ML initialization, sampling, bootstrapping, and Monte Carlo simulations.

Example: Random Sampling

rng = np.random.default_rng(42)

print(rng.normal(size=5))            # Gaussian distribution
print(rng.integers(0, 10, size=5))   # random ints
print(rng.random(3))                 # uniform
print(rng.choice([1, 2, 3, 4], 10))  # sampling with replacement

Tips

  • Always use rng = np.random.default_rng() (new API).
  • Prefer broadcasting over loops for speed.
  • Use .astype(float) to ensure numeric precision.
  • Use @ operator for matrix multiplication rather thannp.dot.
  • Use np.linalg for all linear algebra tasks.

Summary

NumPy provides the core tools required for fast numerical computing — from basic arithmetic to advanced linear algebra and random simulations. Every data scientist should master these concepts.