{"record":{"id":"d63c5a4e5178fdd8","repo":"TheAlgorithms/Python","slug":"rbf-kernel-requires-gamma","errorCode":null,"errorMessage":"rbf kernel requires gamma","messagePattern":"rbf kernel requires gamma","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/support_vector_machines.py","lineNumber":67,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: gamma must be > 0\n    \"\"\"\n\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:","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/support_vector_machines.py#L49-L85","documentation":"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.","triggerScenarios":"Constructing SupportVectorMachine(kernel='rbf') without passing gamma, or explicitly passing gamma=0 with the rbf kernel.","commonSituations":"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.","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())."],"exampleFix":"# before\nsvm = SupportVectorMachine(kernel='rbf')\n\n# after\nsvm = SupportVectorMachine(kernel='rbf', gamma=1.0 / X.shape[1])","handlingStrategy":"validation","validationCode":"gamma = gamma if gamma else 1.0 / n_features  # avoid default-0 trap\nsvm = SupportVectorMachine(kernel='rbf', gamma=gamma)","typeGuard":"def has_positive_gamma(kernel: str, gamma: float) -> bool:\n    return kernel != 'rbf' or (isinstance(gamma, (int, float)) and gamma > 0)","tryCatchPattern":null,"preventionTips":["Do not assume sklearn defaults transfer; this API has no gamma='scale'.","Always pass gamma explicitly when kernel='rbf'."],"tags":["machine-learning","svm","kernel","hyperparameters"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}