{"record":{"id":"5f97212995ed3ddc","repo":"TheAlgorithms/Python","slug":"gamma-must-be-float-or-int","errorCode":null,"errorMessage":"gamma must be float or int","messagePattern":"gamma must be float or int","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/support_vector_machines.py","lineNumber":69,"sourceCode":"    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:\n        \"\"\"\n        RBF: Radial Basis Function Kernel","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/support_vector_machines.py#L51-L87","documentation":"Raised by the SupportVectorMachine constructor when gamma for the rbf kernel is not a float or int. Note that with the annotated default gamma: float = 0.0 this branch is effectively a defensive type check: only exotic objects (strings, None, numpy scalars that are not Python numbers) reach it, and Python bool passes because bool subclasses int.","triggerScenarios":"Passing kernel='rbf' with gamma as a string ('auto'), None, or a non-numeric object. Python True/False bypass this check since bool is an int subclass; np.float64 also passes as it registers as a float-like via isinstance in most builds or is caught here depending on version.","commonSituations":"Forwarding unvalidated config values (from JSON/CLI) straight into the constructor, e.g. gamma='0.5' parsed as a string; or passing gamma=None as a 'use default' sentinel, which this API does not support.","solutions":["Convert the config value to float before passing: gamma=float(cfg['gamma']).","Do not use None or 'auto' sentinels; this API requires a concrete number.","Validate config at load time with a numeric check (isinstance(value, (int, float)) and not isinstance(value, bool))."],"exampleFix":"# before\nsvm = SupportVectorMachine(kernel='rbf', gamma=cfg['gamma'])  # cfg value is '0.5'\n\n# after\ngamma = float(cfg['gamma'])\nsvm = SupportVectorMachine(kernel='rbf', gamma=gamma)","handlingStrategy":"type-guard","validationCode":"if not isinstance(gamma, (int, float)) or isinstance(gamma, bool):\n    gamma = float(gamma)  # or raise your own error\nsvm = SupportVectorMachine(kernel='rbf', gamma=gamma)","typeGuard":"def is_numeric_gamma(value) -> bool:\n    return isinstance(value, (int, float)) and not isinstance(value, bool)","tryCatchPattern":null,"preventionTips":["Convert config-sourced values with float() at load time.","Never pass None or 'auto' as gamma to this API."],"tags":["machine-learning","svm","type-validation","configuration"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}