Vectorization is a technique used to perform operations on entire arrays at once instead of iterating through elements using Python loops. It improves performance by using optimised implementations provided by libraries such as NumPy.
Example: Vectorization in NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Vectorized operation
result = arr * 2
print(result)
Output
[ 2 4 6 8 10]
Explanation:
- arr is a NumPy array containing five elements.
- The expression arr * 2 multiplies every element in the array by 2.
- The operation is applied to the entire array at once without using an explicit loop.
Vectorized Array Operations
1. Dot Product
The dot product, also known as the inner product, is a mathematical operation that multiplies corresponding elements of two vectors of equal length and sums the resulting products to produce a single scalar value.
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.dot(a, b)
print(result)
Output
32
Explanation:
- a and b are vectors of equal length.
- np.dot() multiplies corresponding elements and adds the results.
- The operation returns a single scalar value.
Syntax:
numpy.dot(a, b)
2. Outer Product
The outer product of two vectors produces a matrix containing the product of every element in the first vector with every element in the second vector.
Example:
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
result = np.outer(a, b)
print(result)
Output
[[3 4] [6 8]]
Explanation:
- Each element of a is multiplied by every element of b.
- The result is a matrix.
- If a has length m and b has length n, the result has shape (m, n).
Syntax:
numpy.outer(a, b)
3. Element wise Product
Element-wise multiplication multiplies each element of an array with the element at the same position in another array of the same shape. The operation returns a new array containing the products of the corresponding elements.
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.multiply(a, b)
print(result)
Output
[ 4 10 18]
Explanation:
- Each element in a is multiplied by the element at the same index in b.
- The output array has the same shape as the input arrays.
- This operation is equivalent to a * b for NumPy arrays.
Syntax:
numpy.multiply(a, b)
4. Array Addition
Array addition is a mathematical operation in which corresponding elements of two equally shaped arrays are added together to create a new array. It is commonly used in numerical computing and data analysis for efficient vectorized calculations.
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a + b
print(result)
Output
[5 7 9]
Explanation:
- Corresponding elements from both arrays are added together.
- The operation is vectorized and avoids explicit loops.
- The resulting array has the same shape as the input arrays.
Syntax:
a + b
5. Matrix Multiplication
Matrix multiplication is a linear algebra operation in which rows of the first matrix are multiplied with columns of the second matrix to generate a new matrix. It is widely used in machine learning, computer graphics, and scientific computing.
Example:
import numpy as np
a = np.array([[1, 2],
[3, 4]])
b = np.array([[5, 6],
[7, 8]])
result = a @ b
print(result)
Output
[[19 22] [43 50]]
Explanation:
- Rows of the first matrix are multiplied by columns of the second matrix.
- The @ operator provides a concise syntax for matrix multiplication.
- This operation is widely used in machine learning and linear algebra.
Syntax:
numpy.matmul(a, b)