Back to Problems

Linear Kernel Function

Write a Python function `kernel_function` that computes the linear kernel between two input vectors `x1` and `x2`. The linear kernel is defined as the dot product (inner product) of two vectors.

Example

Example:
import numpy as np

x1 = np.array([1, 2, 3])
x2 = np.array([4, 5, 6])

result = kernel_function(x1, x2)
print(result)
# Expected Output: 32

Understanding the Linear Kernel

A kernel function in machine learning is used to measure the similarity between two data points in a higher-dimensional space without having to compute the coordinates of the points in that space explicitly. The linear kernel is one of the simplest and most commonly used kernel functions. It computes the dot product (or inner product) of two vectors.

Mathematical Definition

The linear kernel between two vectors \( \mathbf{x}_1 \) and \( \mathbf{x}_2 \) is mathematically defined as:

\[ K(\mathbf{x}_1, \mathbf{x}_2) = \mathbf{x}_1 \cdot \mathbf{x}_2 = \sum_{i=1}^{n} x_{1,i} \cdot x_{2,i} \]

Where \( n \) is the number of features, and \( x_{1,i} \) and \( x_{2,i} \) are the components of the vectors \( \mathbf{x}_1 \) and \( \mathbf{x}_2 \) respectively.

The linear kernel is widely used in support vector machines (SVMs) and other machine learning algorithms for linear classification and regression tasks. It is computationally efficient and works well when the data is linearly separable.

Characteristics

  • Simplicity: The linear kernel is straightforward to implement and compute.
  • Efficiency: It is computationally less expensive compared to other complex kernels like polynomial or RBF kernels.
  • Interpretability: The linear kernel is interpretable because it corresponds directly to the dot product, a well-understood operation in vector algebra.

In this problem, you will implement a function capable of computing the linear kernel between two vectors.

import numpy as np

def kernel_function(x1, x2):
    return np.inner(x1, x2)

Your Solution

Output will be shown here.