{"record":{"id":"75d0ec7842894423","repo":"TheAlgorithms/Python","slug":"gamma-must-be-0","errorCode":null,"errorMessage":"gamma must be > 0","messagePattern":"gamma must be > 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/support_vector_machines.py","lineNumber":71,"sourceCode":"\n    def __init__(\n        self,\n        *,\n        regularization: float = np.inf,\n        kernel: str = \"linear\",\n        gamma: float = 0.0,\n    ) -> None:\n        self.regularization = regularization\n        self.gamma = gamma\n        if kernel == \"linear\":\n            self.kernel = self.__linear\n        elif kernel == \"rbf\":\n            if self.gamma == 0:\n                raise ValueError(\"rbf kernel requires gamma\")\n            if not isinstance(self.gamma, (float, int)):\n                raise ValueError(\"gamma must be float or int\")\n            if not self.gamma > 0:\n                raise ValueError(\"gamma must be > 0\")\n            self.kernel = self.__rbf\n            # in the future, there could be a default value like in sklearn\n            # sklear: def_gamma = 1/(n_features * X.var()) (wiki)\n            # previously it was 1/(n_features)\n        else:\n            msg = f\"Unknown kernel: {kernel}\"\n            raise ValueError(msg)\n\n    # kernels\n    def __linear(self, vector1: ndarray, vector2: ndarray) -> float:\n        \"\"\"Linear kernel (as if no kernel used at all)\"\"\"\n        return np.dot(vector1, vector2)\n\n    def __rbf(self, vector1: ndarray, vector2: ndarray) -> float:\n        \"\"\"\n        RBF: Radial Basis Function Kernel\n\n        Note: for more information see:","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/support_vector_machines.py#L53-L89","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Use a positive gamma; start with 1/n_features and tune on a log scale.","Fix sign errors when converting from sigma: gamma = 1/(2*sigma**2).","Clamp or filter grid values to gamma > 0 before constructing the model."],"exampleFix":"# before\nsvm = SupportVectorMachine(kernel='rbf', gamma=-1.0 / n_features)\n\n# after\nsvm = SupportVectorMachine(kernel='rbf', gamma=1.0 / n_features)","handlingStrategy":"validation","validationCode":"assert gamma > 0, 'rbf gamma must be strictly positive'\nsvm = SupportVectorMachine(kernel='rbf', gamma=gamma)","typeGuard":"def is_strictly_positive(value) -> bool:\n    return isinstance(value, (int, float)) and not isinstance(value, bool) and value > 0","tryCatchPattern":null,"preventionTips":["Derive gamma from positive quantities: 1/n_features or 1/(2*sigma^2).","Filter hyperparameter grids to gamma > 0 before model construction."],"tags":["machine-learning","svm","kernel","hyperparameters"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}