machine_learning.support_vector_machines

Classes

SVC

Support Vector Classifier

Functions

norm_squared(→ float)

Return the squared second norm of vector

Module Contents

class machine_learning.support_vector_machines.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
__linear(vector1: numpy.ndarray, vector2: numpy.ndarray) float

Linear kernel (as if no kernel used at all)

__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)))

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})

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
gamma = 0.0
regularization
machine_learning.support_vector_machines.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