Linear Kernel implementation.

Classes

LinearKernel

class algorithms.svm.kernels.linear.LinearKernel(Kernel)

Linear Kernel computing the standard dot product between two vectors.

The Linear Kernel is the simplest kernel function, equivalent to operating in the original input space without any non-linear mapping. It is well suited for linearly separable data and high-dimensional sparse feature spaces.
Constructor
__init__(
    self,
)

Overview

The linear kernel evaluation proceeds as follows:

  1. Accept two input vectors x and y
  2. Compute their inner product (dot product)
  3. Return the scalar result --- no feature-space transformation is applied

Theory

The linear kernel function is defined as:

K(x, y) = x^T y = \sum_{i=1}^{p} x_i y_i

where p is the number of features. The implicit feature map is the identity: \phi(x) = x, so the kernel corresponds to a linear decision boundary in the original input space.

Parameters

(No parameters.)

Attributes

X\_
np.ndarray
Training data stored after build().
n_samples\_
int
Number of training samples.
n_features\_
int
Number of features observed during build().

Notes

Complexity:

  • Evaluation: O(p) per pair, where p = number of features
  • Matrix computation: O(n^2 p) for n samples
When to use LinearKernel:
  • Text classification with high-dimensional TF-IDF or bag-of-words features
  • Linearly separable data or when a simple baseline is needed
  • Very high-dimensional data where non-linear kernels are too expensive
  • When interpretability of the weight vector is important

References

Vapnik1995
Vapnik, V.N. (1995). The Nature of Statistical Learning Theory. Springer-Verlag, New York. DOI: 10.1007/978-1-4757-2440-0

Basic usage for computing the kernel matrix:

python
>>> from tuiml.algorithms.svm.kernels import LinearKernel
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4], [5, 6]])
>>> kernel = LinearKernel()
>>> kernel.build(X_train)
>>> K = kernel.compute_matrix()
>>> print(K.shape)
(3, 3)

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

evaluate (self, x1: np.ndarray, x2: np.ndarray) -> float

Evaluate linear kernel: K(x, y) = x^T * y.

Parameters
x1
np.ndarray
First vector.
x2
np.ndarray
Second vector.
Returns
val
float
Dot product of x1 and x2.
compute_matrix (self) -> np.ndarray

Compute the linear kernel matrix :math:`K = X X^T` efficiently.

Returns
K
np.ndarray of shape (n_samples, n_samples)
The kernel (Gram) matrix.
compute_matrix_cross (self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray

Compute the linear kernel matrix between two sample sets.

Parameters
X1
np.ndarray of shape (n1, n_features)
First set of samples.
X2
np.ndarray of shape (n2, n_features)
Second set of samples.
Returns
K
np.ndarray of shape (n1, n2)
Kernel matrix :math:`K = X_1 X_2^T`.
__repr__ (self) -> str

String representation.