02.02 Lesson 2 of 12

54 min

Vectors & 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

Exercise

Build a vector distance helper

Use NumPy arrays to compare two small observations and report their magnitude and distance.

  1. Create two three-feature arrays named morning and evening.
  2. Calculate each vector’s L2 norm with np.linalg.norm.
  3. Calculate the Euclidean distance between them.
  4. Write a helper function that accepts two arrays and returns their distance.
  5. Test the helper with identical arrays and explain why the distance is zero.
Done when

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

2 links
  1. 01
    Vectors | Chapter 1, Essence of linear algebra Video

    Watch this single chapter to see vectors as geometric objects and understand addition and scaling.

  2. 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

2 questions
  1. Question 1

    What is the Euclidean distance between two vectors a and b?

  2. Question 2

    Why does vector component order matter in a feature vector?

Completion check

Move on when

You can construct a feature vector, calculate its norm and compute distance to another vector in NumPy.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels