Linear Kernel implementation.
Classes
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:
- Accept two input vectors x and y
- Compute their inner product (dot product)
- 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
- 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
See Also
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
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
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`.