TheAlgorithms/Python · error · ValueError
rbf kernel requires gamma
Error message
rbf kernel requires gamma
What it means
Raised by the SupportVectorMachine constructor when kernel='rbf' is requested but gamma is left at its default 0.0. The RBF kernel needs a positive width parameter; unlike sklearn this class has no automatic default (the source comments note a future default may be added), so the caller must supply gamma explicitly.
Source
Thrown at machine_learning/support_vector_machines.py:67
Traceback (most recent call last):
...
ValueError: gamma must be > 0
"""
def __init__(
self,
*,
regularization: float = np.inf,
kernel: str = "linear",
gamma: float = 0.0,
) -> 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:View on GitHub (pinned to f5988cc097)
Solutions
- Pass an explicit positive gamma, e.g. SupportVectorMachine(kernel='rbf', gamma=0.5) or gamma=1/n_features.
- If you do not need nonlinearity, keep kernel='linear' which needs no gamma.
- Compute a sklearn-style default yourself: gamma = 1 / (n_features * X.var()).
Example fix
# before svm = SupportVectorMachine(kernel='rbf') # after svm = SupportVectorMachine(kernel='rbf', gamma=1.0 / X.shape[1])
Defensive patterns
Strategy: validation
Validate before calling
gamma = gamma if gamma else 1.0 / n_features # avoid default-0 trap svm = SupportVectorMachine(kernel='rbf', gamma=gamma)
Type guard
def has_positive_gamma(kernel: str, gamma: float) -> bool:
return kernel != 'rbf' or (isinstance(gamma, (int, float)) and gamma > 0) Prevention
- Do not assume sklearn defaults transfer; this API has no gamma='scale'.
- Always pass gamma explicitly when kernel='rbf'.
When it happens
Trigger: Constructing SupportVectorMachine(kernel='rbf') without passing gamma, or explicitly passing gamma=0 with the rbf kernel.
Common situations: Porting code from sklearn.svm.SVC where gamma='scale' works out of the box, then hitting this class's stricter contract; or toggling the kernel string from 'linear' to 'rbf' in a config without adding the gamma key.
Related errors
- gamma value must be non-negative
- gamma must be > 0
- 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
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d63c5a4e5178fdd8.
Report an issue: GitHub.