+10 XP

Vector Length

Before we can do anything useful with vectors — compare them, combine them, find angles between them — we need to know how to measure them. Length is the ruler. That's it. You'll need it for normalization and for the dot product later.

Take 2 neurons: neuron 1 fires at 3 Hz, neuron 2 fires at 4 Hz → vector [3, 4]. Draw it as an arrow on a 2D plot. How long is that arrow?

It's exactly the same as the hypotenuse of a right triangle — the Pythagorean theorem you know from school:

Length = √(3² + 4²) = √(9 + 16) = √25 = 5

‖v‖ = √(v₁² + v₂² + ... + vₙ²) The length of any vector = square root of the sum of all components squared.

Same Pythagorean theorem, extended to N dimensions.

To find the length of a vector in Python: np.linalg.norm(v)

That's it. One function. You will use this constantly.

python
import numpy as np

# 2 neurons: one fires at 3 Hz, one at 4 Hz
v = np.array([3, 4])

# To find vector length → np.linalg.norm()
length = np.linalg.norm(v)
print(length)  # 5.0

# Works for any number of neurons
firing_rates = np.array([10, 50, 2])
print(np.linalg.norm(firing_rates))  # ≈ 51.0

np.linalg.norm(v) — memorize this. 'linalg' = linear algebra, 'norm' = length.