MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __init__

Method __init__

machine_learning/support_vector_machines.py:54–78  ·  view source on GitHub ↗
(
        self,
        *,
        regularization: float = np.inf,
        kernel: str = "linear",
        gamma: float = 0.0,
    )

Source from the content-addressed store, hash-verified

52 """
53
54 def __init__(
55 self,
56 *,
57 regularization: float = np.inf,
58 kernel: str = "linear",
59 gamma: float = 0.0,
60 ) -> None:
61 self.regularization = regularization
62 self.gamma = gamma
63 if kernel == "linear":
64 self.kernel = self.__linear
65 elif kernel == "rbf":
66 if self.gamma == 0:
67 raise ValueError("rbf kernel requires gamma")
68 if not isinstance(self.gamma, (float, int)):
69 raise ValueError("gamma must be float or int")
70 if not self.gamma > 0:
71 raise ValueError("gamma must be > 0")
72 self.kernel = self.__rbf
73 # in the future, there could be a default value like in sklearn
74 # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
75 # previously it was 1/(n_features)
76 else:
77 msg = f"Unknown kernel: {kernel}"
78 raise ValueError(msg)
79
80 # kernels
81 def __linear(self, vector1: ndarray, vector2: ndarray) -> float:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected