TheAlgorithms/Python · error · ValueError

gamma must be > 0

Error message

gamma must be > 0

What it means

Raised by the SupportVectorMachine constructor when the rbf kernel receives a negative gamma. The order of checks means gamma=0 is reported as 'rbf kernel requires gamma' and any negative number reaches this 'gamma must be > 0' branch, enforcing a strictly positive RBF width.

Source

Thrown at machine_learning/support_vector_machines.py:71

    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:
        """
        RBF: Radial Basis Function Kernel

        Note: for more information see:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a positive gamma; start with 1/n_features and tune on a log scale.
  2. Fix sign errors when converting from sigma: gamma = 1/(2*sigma**2).
  3. Clamp or filter grid values to gamma > 0 before constructing the model.

Example fix

# before
svm = SupportVectorMachine(kernel='rbf', gamma=-1.0 / n_features)

# after
svm = SupportVectorMachine(kernel='rbf', gamma=1.0 / n_features)
Defensive patterns

Strategy: validation

Validate before calling

assert gamma > 0, 'rbf gamma must be strictly positive'
svm = SupportVectorMachine(kernel='rbf', gamma=gamma)

Type guard

def is_strictly_positive(value) -> bool:
    return isinstance(value, (int, float)) and not isinstance(value, bool) and value > 0

Prevention

When it happens

Trigger: Constructing SupportVectorMachine(kernel='rbf', gamma=-0.1) or passing a computed gamma expression that evaluates negative (e.g. -1/n_features from a sign mistake).

Common situations: Hyperparameter grid searches that include negative values, sign errors when converting from sigma (gamma = -1/(2*sigma^2) mistakenly), or arithmetic that yields 0/negative for degenerate feature counts.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/75d0ec7842894423. Report an issue: GitHub.