02.02 Lesson 2 of 12
54 minVectors & Vector Geometry
Represent multiple measurements as a vector and reason about its direction, magnitude and distance from another vector.
By the end of this lesson
You will be able to
- Explain a vector as an ordered list of values with a geometric interpretation.
- Add vectors and multiply a vector by a scalar.
- Calculate an L2 norm and Euclidean distance.
- Distinguish vector shape from the values it contains.
One observation can be a vector
A vector is an ordered collection of components. A bike-sharing observation might be represented by a feature vector containing temperature, humidity and wind speed. Order matters: changing the order changes what each position means. A vector can be drawn as an arrow, but in code it is often stored as a one-dimensional array.
import numpy as np
ride = np.array([0.72, 0.61, 0.18])
print(ride.shape) # (3,) Magnitude and distance
The Euclidean norm measures a vector’s length: ||x||₂ = sqrt(x₁² + … + xₚ²). Distance between two vectors is the norm of their difference: d(x, z) = ||x - z||₂. These calculations are sensitive to units and scale. A temperature measured in degrees and a count measured in thousands should not be compared blindly without considering their scales.
x = np.array([3.0, 4.0])
z = np.array([0.0, 0.0])
print(np.linalg.norm(x)) # 5.0
print(np.linalg.norm(x - z)) # 5.0 Apply what you learned
ExerciseBuild a vector distance helper
Use NumPy arrays to compare two small observations and report their magnitude and distance.
- Create two three-feature arrays named
morningandevening. - Calculate each vector’s L2 norm with
np.linalg.norm. - Calculate the Euclidean distance between them.
- Write a helper function that accepts two arrays and returns their distance.
- Test the helper with identical arrays and explain why the distance is zero.
The helper returns a non-negative scalar, returns zero for equal vectors and produces the same result as np.linalg.norm(a - b).
Free references
Study resources
- 01 Vectors | Chapter 1, Essence of linear algebra Video
Watch this single chapter to see vectors as geometric objects and understand addition and scaling.
- 02 numpy.linalg.norm Article
Read the default norm behavior and the examples for one-dimensional arrays. Use the function for the exercise.
Check your understanding
Self-test
-
Question 1
What is the Euclidean distance between two vectors
aandb?It is the L2 norm of their difference:||a - b||₂. -
Question 2
Why does vector component order matter in a feature vector?
Each position represents a particular feature. Swapping positions changes the meaning of the observation.
Completion check
Move on when
You can construct a feature vector, calculate its norm and compute distance to another vector in NumPy.