Vector Operations
In the field of linear algebra, vectors play a fundamental role. Vectors are mathematical objects that represent both magnitude and direction. In deep learning, they are commonly used to represent data points, features, and parameters of models. This chapter will introduce you to some essential vector operations, provide examples, and demonstrate how to perform these operations using the popular Python library, NumPy.
6.1 Addition and Subtraction of Vectors
Vectors can be added or subtracted component-wise. Given two vectors, A and B, with the same dimension, their addition and subtraction can be expressed as:
Example:
Let’s consider two vectors, $A = [2, 4] and B = [1, 3]$.
NumPy Code:
You can perform vector addition and subtraction in NumPy using the following code:
import numpy as np
A = np.array([2, 4])
B = np.array([1, 3])
# Addition
result_addition = A + B
# Subtraction
result_subtraction = A - B
print("Addition result:", result_addition)
print("Subtraction result:", result_subtraction)6.2 Scalar Multiplication
Scalar multiplication involves multiplying a vector by a scalar (a single numerical value). Given a vector A and a scalar c, the scalar multiplication is expressed as:
Example:
Let’s consider a vector $A = [2, 3]$ and a scalar $c = 3$.
NumPy Code:
You can perform scalar multiplication in NumPy using the following code:
import numpy as np
A = np.array([2, 3])
c = 3
result_scalar_mult = c * A
print("Scalar Multiplication result:", result_scalar_mult)This chapter provides a brief overview of vector operations in linear algebra and demonstrates how to perform these operations using NumPy. These operations are foundational in deep learning and are used extensively in various aspects of machine learning and data analysis.