The Five Patterns
Almost every piece of vector code in AI is one of these five patterns. Knowing them by sight saves enormous reading time.
- Element-wise op —
a + b,a * b,np.exp(a). Same shape in, same shape out. - Reduction —
a.sum(),a.mean(),a.max(). Many in, one (or fewer) out. Optionally specifyaxis=. - Dot product / matmul —
a @ b. Two vectors → scalar. Vector and matrix → vector. Two matrices → matrix. - Broadcasting — operating on different-shaped arrays where one is "lifted" to match.
matrix + vectoroften Just Works because of this. - Slicing / fancy indexing —
a[2:5],a[a > 0],a[indices]. Pulling subsets without writing loops.
Broadcasting in 30 Seconds
NumPy/PyTorch will let you add a (3,) vector to a (4, 3) matrix — the vector gets broadcast across each row. The rule: dimensions are compatible if they're equal or one of them is 1. Lined up from the right. If the rule fails, you get the legendary shapes ... not aligned error.
Stop writing loops over array elements. If you find yourself writing
for i in range(len(arr)), ask: is there a vectorized version? In 95% of cases, yes — and it'll be 10-1000× faster.Track Reward
You now think in vectors. Magnitude × direction is the unit. The dot product is alignment-as-a-number. Norms are choosable rulers. Operations vectorize. From here, matrices are just stacks of vectors that act in concert — and that's the next track.
just do it 정신을 잠시 잃고 너무 오랜만에 돌아왔어 , 평균 0 , 표준편차 1 인거 확인!
import numpy as np
rng = np.random.default_rng(42) X = rng.standard_normal((5, 3))
mu = X.mean(axis=0) sigma = X.std(axis=0)
Z = (X - mu) / sigma
print("열 평균:", Z.mean(axis=0)) print("열 std :", Z.std(axis=0))
assert np.allclose(Z.mean(axis=0), 0, atol=1e-12) assert np.allclose(Z.std(axis=0), 1, atol=1e-12) print("검증 통과")