Now that you understand what vectors are and how to represent them using NumPy arrays, let's explore some fundamental operations you can perform on them. Two of the most basic operations are vector addition and subtraction. These operations work element-wise, meaning they operate on the corresponding components of the vectors involved.
Adding two vectors is straightforward. You simply add the corresponding elements of each vector together. For this operation to be valid, the vectors must have the same number of elements, meaning they must have the same dimension or length.
Mathematically, if you have two n-dimensional vectors, u and v:
u=[u1,u2,…,un] v=[v1,v2,…,vn]Their sum, w=u+v, is calculated as:
w=[u1+v1,u2+v2,…,un+vn]For example, let's add two 3-dimensional vectors: u=[1,2,3] v=[4,5,6]
The sum is: u+v=[1+4,2+5,3+6]=[5,7,9]
Geometric Interpretation
Geometrically, vector addition can be visualized by placing the tail of the second vector (v) at the head of the first vector (u). The resulting vector sum (u+v) starts at the origin (tail of u) and ends at the head of the repositioned v. This is often called the "head-to-tail" rule. Alternatively, it forms the diagonal of a parallelogram defined by the two original vectors when both start from the origin.
Vector addition using the head-to-tail method. Vector u (blue) is added to vector v (pink, shifted). The resultant vector u+v (green) goes from the origin to the tip of the shifted v. Dashed lines show the parallelogram completion.
Implementation in NumPy
NumPy makes vector addition very intuitive. You can use the standard +
operator directly on NumPy arrays representing your vectors. NumPy automatically handles the element-wise addition.
import numpy as np
# Define two vectors as NumPy arrays
u = np.array([1, 2, 3])
v = np.array([4, 5, 6])
# Add the vectors
w = u + v
print(f"Vector u: {u}")
print(f"Vector v: {v}")
print(f"Sum u + v: {w}")
Output:
Vector u: [1 2 3]
Vector v: [4 5 6]
Sum u + v: [5 7 9]
Remember, if you try to add vectors of different lengths, NumPy will raise a ValueError
because the element-wise operation cannot be performed.
# Example of incompatible shapes
a = np.array([1, 2])
b = np.array([3, 4, 5])
try:
c = a + b
except ValueError as e:
print(f"Error adding vectors a and b: {e}")
Output:
Error adding vectors a and b: operands could not be broadcast together with shapes (2,) (3,)
Vector subtraction works similarly to addition. You subtract the corresponding elements of the second vector from the first vector. Again, both vectors must have the same dimension.
If you have two n-dimensional vectors, u and v:
u=[u1,u2,…,un] v=[v1,v2,…,vn]Their difference, d=u−v, is calculated as:
d=[u1−v1,u2−v2,…,un−vn]For example, using the same vectors u and v as before: u=[1,2,3] v=[4,5,6]
The difference is: u−v=[1−4,2−5,3−6]=[−3,−3,−3]
Geometric Interpretation
Geometrically, subtracting v from u (u−v) is equivalent to adding the negative of v to u (u+(−v)). The vector −v has the same magnitude as v but points in the opposite direction. Another way to visualize u−v is as the vector that starts at the head of v and ends at the head of u, when both u and v start from the origin.
Vector subtraction using the u+(−v) method. Vector u (blue) is added to vector −v (pink, shifted). The resultant vector u−v (green) goes from the origin to the tip of the shifted −v.
Implementation in NumPy
Similar to addition, vector subtraction in NumPy uses the standard -
operator.
import numpy as np
# Define two vectors
u = np.array([1, 2, 3])
v = np.array([4, 5, 6])
# Subtract vector v from vector u
d = u - v
print(f"Vector u: {u}")
print(f"Vector v: {v}")
print(f"Difference u - v: {d}")
# Subtract vector u from vector v
d_reversed = v - u
print(f"Difference v - u: {d_reversed}")
Output:
Vector u: [1 2 3]
Vector v: [4 5 6]
Difference u - v: [-3 -3 -3]
Difference v - u: [3 3 3]
Notice that, unlike addition, the order matters in subtraction (u−v is generally not the same as v−u). Vector addition is commutative (u+v=v+u), but subtraction is not.
Vector addition and subtraction are fundamental tools in linear algebra. They form the basis for many operations used in machine learning algorithms, such as calculating error vectors or updating feature weights. Understanding how they work, both mathematically and in code using libraries like NumPy, is an important step in building your linear algebra foundation.
© 2025 ApX Machine Learning