Loading problem...
In machine learning, kernel functions are fundamental mathematical tools that measure the similarity between pairs of data points. Among all kernel functions, the linear kernel is the simplest and most intuitive—it computes the standard dot product (inner product) between two vectors.
The linear kernel between two vectors x₁ and x₂, each of dimension d, is defined mathematically as:
$$K(x_1, x_2) = x_1 \cdot x_2 = \sum_{i=1}^{d} x_{1i} \cdot x_{2i}$$
This operation multiplies corresponding elements of the two vectors and sums the results by pairwise multiplication. The result is a scalar value that quantifies the similarity between the two input vectors in their original feature space.
Key Properties of the Linear Kernel:
Geometric Interpretation: The linear kernel measures how well two vectors align in the feature space. When both vectors point in similar directions, the kernel value is large and positive. When they are orthogonal (perpendicular), the kernel returns zero. When they point in opposite directions, the value is negative.
Your Task: Write a Python function that computes the linear kernel between two input vectors. The function should accept two NumPy arrays of equal length and return their dot product as a scalar value.
x1 = [1, 2, 3]
x2 = [4, 5, 6]32The linear kernel is computed as the sum of element-wise products:
K(x₁, x₂) = (1 × 4) + (2 × 5) + (3 × 6) = 4 + 10 + 18 = 32
Both vectors have positive components pointing in generally similar directions, resulting in a positive kernel value.
x1 = [3, 4]
x2 = [1, 2]11Computing the dot product for these 2D vectors:
K(x₁, x₂) = (3 × 1) + (4 × 2) = 3 + 8 = 11
The positive result indicates both vectors point in similar directions in the 2D plane.
x1 = [1, 0]
x2 = [0, 1]0These are the standard unit basis vectors along the x-axis and y-axis respectively:
K(x₁, x₂) = (1 × 0) + (0 × 1) = 0 + 0 = 0
A kernel value of zero indicates the vectors are orthogonal (perpendicular) to each other—they have no component in common and are completely independent directions.
Constraints