TheAlgorithms/Python · error · ValueError
Unknown kernel: {kernel}
Error message
Unknown kernel: {kernel} What it means
Raised by the SupportVectorMachine constructor when the kernel string is anything other than 'linear' or 'rbf'. The constructor maps the string to an internal kernel method and has no fallback, so an unrecognized name fails fast with a message echoing the bad value.
Source
Thrown at machine_learning/support_vector_machines.py:78
) -> None:
self.regularization = regularization
self.gamma = gamma
if kernel == "linear":
self.kernel = self.__linear
elif kernel == "rbf":
if self.gamma == 0:
raise ValueError("rbf kernel requires gamma")
if not isinstance(self.gamma, (float, int)):
raise ValueError("gamma must be float or int")
if not self.gamma > 0:
raise ValueError("gamma must be > 0")
self.kernel = self.__rbf
# in the future, there could be a default value like in sklearn
# sklear: def_gamma = 1/(n_features * X.var()) (wiki)
# previously it was 1/(n_features)
else:
msg = f"Unknown kernel: {kernel}"
raise ValueError(msg)
# kernels
def __linear(self, vector1: ndarray, vector2: ndarray) -> float:
"""Linear kernel (as if no kernel used at all)"""
return np.dot(vector1, vector2)
def __rbf(self, vector1: ndarray, vector2: ndarray) -> float:
"""
RBF: Radial Basis Function Kernel
Note: for more information see:
https://en.wikipedia.org/wiki/Radial_basis_function_kernel
Args:
vector1 (ndarray): first vector
vector2 (ndarray): second vector)
Returns:View on GitHub (pinned to f5988cc097)
Solutions
- Use exactly 'linear' or 'rbf'.
- If you imported the name from elsewhere, strip and lower-case it: kernel=kernel.strip().lower().
- For kernels this class lacks (poly, sigmoid), implement them externally or use a library that supports them.
Example fix
# before svm = SupportVectorMachine(kernel='poly') # after svm = SupportVectorMachine(kernel='rbf') # or 'linear'; only these exist
Defensive patterns
Strategy: validation
Validate before calling
kernel = kernel.strip().lower()
if kernel not in ('linear', 'rbf'):
raise ValueError(f"unsupported kernel {kernel!r}; use 'linear' or 'rbf'")
svm = SupportVectorMachine(kernel=kernel) Type guard
def is_supported_kernel(name: str) -> bool:
return isinstance(name, str) and name.strip().lower() in ('linear', 'rbf') Prevention
- Keep the supported-kernel list next to your config schema.
- Normalize case and whitespace of config strings before use.
When it happens
Trigger: Calling SupportVectorMachine(kernel='poly'), kernel='sigmoid', or any typo like 'RBF' or 'Linear' (matching is case-sensitive).
Common situations: Copying kernel names valid in sklearn (poly, sigmoid, precomputed) into this class, case mismatches, or whitespace in config strings (' rbf').
Related errors
- gamma must be float or int
- Length of predicted and actual array must be same.
- y_true can have values -1 or 1 only.
- Test samples' feature length does not equal to that of train
- gamma value must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/f4b47997cf8bd25b.
Report an issue: GitHub.