TheAlgorithms/Python · error · ValueError
gamma value must be non-negative
Error message
gamma value must be non-negative
What it means
Raised by the kernel helper class inside sequential_minimum_optimization.py when the RBF kernel is selected with a negative gamma. _check() runs during setup and validates that the RBF width parameter is non-negative, because a negative gamma would produce an exploding (non-decaying) exponential kernel that is mathematically invalid for RBF.
Source
Thrown at machine_learning/sequential_minimum_optimization.py:424
self.degree = np.float64(degree)
self.coef0 = np.float64(coef0)
self.gamma = np.float64(gamma)
self._kernel_name = kernel
self._kernel = self._get_kernel(kernel_name=kernel)
self._check()
def _polynomial(self, v1, v2):
return (self.gamma * np.inner(v1, v2) + self.coef0) ** self.degree
def _linear(self, v1, v2):
return np.inner(v1, v2) + self.coef0
def _rbf(self, v1, v2):
return np.exp(-1 * (self.gamma * np.linalg.norm(v1 - v2) ** 2))
def _check(self):
if self._kernel == self._rbf and self.gamma < 0:
raise ValueError("gamma value must be non-negative")
def _get_kernel(self, kernel_name):
maps = {"linear": self._linear, "poly": self._polynomial, "rbf": self._rbf}
return maps[kernel_name]
def __call__(self, v1, v2):
return self._kernel(v1, v2)
def __repr__(self):
return self._kernel_name
def count_time(func):
def call_func(*args, **kwargs):
import time
start_time = time.time()
func(*args, **kwargs)View on GitHub (pinned to f5988cc097)
Solutions
- Set gamma to a non-negative value; typical starting points are 1/n_features or values from a logspace grid like 0.001-10.
- If you intended gamma = 1/(2*sigma^2), compute it explicitly so the result is positive.
- Switch to kernel='linear' if you do not need the RBF kernel at all.
Example fix
# before model = SMO(train_samples, train_labels, kernel='rbf', gamma=-0.5) # after model = SMO(train_samples, train_labels, kernel='rbf', gamma=0.5)
Defensive patterns
Strategy: validation
Validate before calling
gamma = 1.0 / train_samples.shape[1] # positive default assert gamma >= 0, 'gamma must be non-negative for rbf'
Type guard
def is_valid_gamma(g) -> bool:
return isinstance(g, (int, float)) and not isinstance(g, bool) and g >= 0 Prevention
- Generate gamma candidates from np.logspace, which never yields negatives.
- Centralize kernel hyperparameters in one validated config object.
- Keep sign conventions written next to the config keys.
When it happens
Trigger: Constructing the SMO classifier (or its kernel object) with kernel='rbf' and a gamma value less than 0. Other kernels (linear, poly) never trigger this check even with negative gamma.
Common situations: Typosigning gamma (e.g. gamma=-0.1 instead of 0.1), copying hyperparameters from a convention where gamma means inverse width (1/(2*sigma^2)) and mixing up signs, or sweeping gamma over a log range that includes negatives.
Related errors
- rbf kernel requires gamma
- 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/8a4bf33995d5c83e.
Report an issue: GitHub.