machine_learning.support_vector_machines ======================================== .. py:module:: machine_learning.support_vector_machines Classes ------- .. autoapisummary:: machine_learning.support_vector_machines.SVC Functions --------- .. autoapisummary:: machine_learning.support_vector_machines.norm_squared Module Contents --------------- .. py:class:: SVC(*, regularization: float = np.inf, kernel: str = 'linear', gamma: float = 0.0) Support Vector Classifier Args: kernel (str): kernel to use. Default: linear Possible choices: - linear regularization: constraint for soft margin (data not linearly separable) Default: unbound >>> SVC(kernel="asdf") Traceback (most recent call last): ... ValueError: Unknown kernel: asdf >>> SVC(kernel="rbf") Traceback (most recent call last): ... ValueError: rbf kernel requires gamma >>> SVC(kernel="rbf", gamma=-1) Traceback (most recent call last): ... ValueError: gamma must be > 0 .. py:method:: __linear(vector1: numpy.ndarray, vector2: numpy.ndarray) -> float Linear kernel (as if no kernel used at all) .. py:method:: __rbf(vector1: numpy.ndarray, vector2: numpy.ndarray) -> float RBF: Radial Basis Function Kernel Note: for more information see: https://en.wikipedia.org/wiki/Radial_basis_function_kernel Args: vector1 (ndarray): first vector vector2 (ndarray): second vector) Returns: float: exp(-(gamma * norm_squared(vector1 - vector2))) .. py:method:: fit(observations: list[numpy.ndarray], classes: numpy.ndarray) -> None Fits the SVC with a set of observations. Args: observations (list[ndarray]): list of observations classes (ndarray): classification of each observation (in {1, -1}) .. py:method:: predict(observation: numpy.ndarray) -> int Get the expected class of an observation Args: observation (Vector): observation Returns: int {1, -1}: expected class >>> xs = [ ... np.asarray([0, 1]), np.asarray([0, 2]), ... np.asarray([1, 1]), np.asarray([1, 2]) ... ] >>> y = np.asarray([1, 1, -1, -1]) >>> s = SVC() >>> s.fit(xs, y) >>> s.predict(np.asarray([0, 1])) 1 >>> s.predict(np.asarray([1, 1])) -1 >>> s.predict(np.asarray([2, 2])) -1 .. py:attribute:: gamma :value: 0.0 .. py:attribute:: regularization .. py:function:: norm_squared(vector: numpy.ndarray) -> float Return the squared second norm of vector norm_squared(v) = sum(x * x for x in v) Args: vector (ndarray): input vector Returns: float: squared second norm of vector >>> int(norm_squared([1, 2])) 5 >>> int(norm_squared(np.asarray([1, 2]))) 5 >>> int(norm_squared([0, 0])) 0