Create your own
Lesson illustration

NumPy Vector Operations and Euclidean Norms

Good to continue from the array-structure work. Previously, you learned to inspect an array’s ndim, shape, size, and dtype; that matters here because vector arithmetic only makes sense when the arrays represent compatible quantities.

This lesson turns one-dimensional NumPy arrays into mathematical vectors. You will add vectors, scale them by a number, and measure their Euclidean length with a norm. These operations are foundational for feature vectors, residuals, distances, and gradient-based optimization later in the course. Plan for about 35–40 minutes, including the 15-line micro-challenge.


Vectors: arrays with magnitude and direction

For this course, a vector is usually a one-dimensional numeric array such as:

v = np.array([3.0, -4.0, 12.0])

Its shape is (3,), meaning it has three components. You can interpret those components as coordinates, feature values, changes in model parameters, or any other ordered numerical quantity.

Two core operations work component by component.

For vectors and of the same length:

For a scalar , which is just one number:

So if

then:

and:

Addition combines two compatible movements or feature changes. Scalar multiplication changes a vector’s magnitude: multiplying by a positive scalar preserves direction, while a negative scalar reverses direction.

The diagram shows head-to-tail vector addition: the purple vector is the combined displacement \(v + w\). It also shows scalar multiplication, where \(2v\) points in the same direction as \(v\) but is twice as long.

NumPy performs these operations elementwise

In NumPy, + adds corresponding entries, and multiplying an array by a single number scales every entry:

import numpy as np

v = np.array([3.0, -4.0, 12.0])
w = np.array([-1.0, 5.0, 2.0])

print(v + w)    # [ 2.  1. 14.]
print(2.5 * v)  # [  7.5 -10.   30. ]

The vectors must have compatible shapes. For this lesson, use same-length one-dimensional vectors. Adding a vector of shape (3,) to one of shape (4,) is not defined component by component, so NumPy will raise an error.

Read the short official NumPy explanation before coding. Its distinction that arithmetic normally applies elementwise is especially important: next lesson, you will contrast that behavior with matrix multiplication.

NumPy quickstart — NumPy v2.3 Manual

Read the NumPy documentation’s explanation of array arithmetic. It establishes the exact behavior that makes vector addition and scalar multiplication concise in NumPy.

In the “Basic operations” section, begin with the opening explanation. Then follow the examples beginning with a = np.array([20, 30, 40, 50]) through the scalar expression 10 * np.sin(a). Stop before the paragraph beginning “Unlike in many matrix languages,” since matrix products are the focus of the next lesson. Then read the opening of “Universal functions,” especially the function overview, and inspect the np.sqrt example. You will use square root to compute a norm manually.

A practical caution: the operation below is scalar multiplication because 2.5 is one number.

2.5 * v

By contrast, multiplying two arrays with v * w also happens elementwise in NumPy:

v * w

That produces one product per position; it is not a vector norm and, in general, is not matrix multiplication. Keep that boundary clear.


Euclidean norm: the length of a vector

The Euclidean norm, also called the norm, measures a vector’s ordinary geometric length:

For the vector

the Euclidean norm is:

In NumPy, you can calculate this in either of two ways:

manual_norm = np.sqrt(np.sum(v ** 2))
numpy_norm = np.linalg.norm(v)

For a one-dimensional vector, np.linalg.norm(v) uses the Euclidean norm by default. It is useful to calculate the same value manually once: it connects the code to the mathematical definition and makes debugging easier.

One property provides a valuable sanity check:

Since and :

The absolute value matters. A multiplier of reverses the vector direction, but its length is still multiplied by , not by a negative number.

This idea will recur in machine learning. A residual vector’s norm summarizes the overall size of errors; a gradient norm measures the size of a proposed parameter update; and feature-vector norms can reveal variables with drastically different scales.

For a concise mathematical account of these operations and norms, use the following reference.

Basics of Linear Algebra

This reading connects NumPy’s syntax to the formal definitions of vectors, vector addition, scalar multiplication, and the Euclidean norm.

First, in the “Vectors” section, read the norm passage. Focus on the L_2 formula and distinguish it from the L_1 and L_\infty norms; this lesson uses only L_2. Next, read the section beginning with vector addition. Follow addition and scaling. Pay attention to the phrase “each element”: it is the conceptual reason the NumPy expressions v + w and a * v work as they do.


Concept-Level Micro-Challenge: operate on vectors and verify the norm

Type and run this 15-line program. It performs all three operations and verifies the results with assert statements. An assert that produces no error has passed.

import numpy as np
v = np.array([3.0, -4.0, 12.0])
w = np.array([-1.0, 5.0, 2.0])
alpha = 2.5
vector_sum = v + w
scaled_v = alpha * v
norm_manual = np.sqrt(np.sum(v ** 2))
norm_numpy = np.linalg.norm(v)
print("v + w:", vector_sum)
print("alpha * v:", scaled_v)
print("manual norm:", norm_manual)
print("NumPy norm:", norm_numpy)
assert np.array_equal(vector_sum, np.array([2.0, 1.0, 14.0]))
assert np.isclose(norm_manual, 13.0)
assert np.isclose(norm_manual, norm_numpy)
assert np.isclose(np.linalg.norm(scaled_v), abs(alpha) * norm_numpy)

Your output should include:

v + w: [ 2.  1. 14.]
alpha * v: [  7.5 -10.   30. ]
manual norm: 13.0
NumPy norm: 13.0

Interpret the program rather than treating the assertions as decoration:

  • vector_sum is the componentwise combination of two vectors.
  • scaled_v makes every component times as large.
  • norm_manual implements the definition .
  • norm_numpy confirms NumPy’s linear-algebra routine gives the same Euclidean length.
  • The final assertion checks the scaling law .

Use np.isclose for norm comparisons rather than ==. Floating-point calculations can contain tiny rounding differences even when two values are mathematically equal.


A compact debugging checklist

When vector code does not behave as expected, check these points in order:

  1. Are both inputs numeric NumPy arrays?
    Inspect type(v) and v.dtype.

  2. Do vectors have compatible shapes?
    Check v.shape and w.shape. For direct vector addition in this lesson, both should be (n,).

  3. Did you intend scalar scaling or array-by-array multiplication?
    alpha * v scales a vector; v * w multiplies entries pairwise.

  4. Is the norm nonnegative?
    A Euclidean norm cannot be negative.

  5. Does scaling change length by the scalar’s magnitude?
    Test whether np.linalg.norm(alpha * v) matches abs(alpha) * np.linalg.norm(v).

These short checks are worth making habitual. Many later modeling errors reduce to an unintended shape, an elementwise operation used where a matrix operation was intended, or an unexamined scale difference.


Takeaways

You can now use NumPy arrays as simple vectors:

  • Vector addition, v + w, adds corresponding components.
  • Scalar multiplication, alpha * v, scales every component.
  • The Euclidean norm is
  • np.linalg.norm(v) computes the Euclidean norm for a one-dimensional vector.
  • Assertions and np.isclose turn mathematical expectations into executable checks.

Next, you will examine a crucial distinction in NumPy: elementwise multiplication with * versus matrix multiplication with @.

Can't find a good explanation? Sign up and we'll make it for you

Sign up