pyai.docs

Data & Math › 8 of 14

NumPy Essentials

Vectorized arrays — the foundation of ML in Python.

python
import numpy as np

X = np.random.randn(100, 3)
w = np.array([0.5, -1.0, 2.0])
y = X @ w                     # matrix multiply

X_norm = (X - X.mean(axis=0)) / X.std(axis=0)
mask = y > 0
print(X.shape, y[mask].shape)
# output
(100, 3) (52,)
  • Broadcasting aligns shapes from the right
  • axis=0 → down columns, axis=1 → across rows
  • Avoid Python loops; vectorize

⚡ Quick check

  • ✓ @ for matmul
  • ✓ arr.reshape(-1, 1)
  • ✓ Boolean masks: a[a > 0]