botiverse.models.SVM package#

Submodules#

botiverse.models.SVM.SVM module#

This module implements and provides an interface for the multiclass Support Vector Machine (SVM) algorithm implemented from scratch.

class botiverse.models.SVM.SVM.SVM(kernel='rbf', C=1, k=2)[source]#

Bases: object

This class implements the kernelized soft-margin Support Vector Machine algorithm.

Initialize an instance of the SVM model.

Parameters:
  • kernel (str) – The kernel function to use. Can be ‘linear’, ‘polynomial’, or ‘rbf’.

  • C (float) – The regularization parameter.

  • k (int) – The hyperparameter for the polynomial and rbf kernels. Ignored for the linear kernel.

linear(xࠤ, c=0)#
polynomial(xࠤ, Q=5)#
rbf(xࠤ, γ=10)#
kernel_funs = {'linear': <function SVM.<lambda>>, 'polynomial': <function SVM.<lambda>>, 'rbf': <function SVM.<lambda>>}#
evaluate(X, y)#

Compare the predicted labels for given test data y with the actual labels by passing X to model.

Parameters:
  • X (numpy.ndarray) – The test data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The test labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1

Returns:

The accuracy of the model on the given test data.

Return type:

float

fit(X, y, eval_train=False)#

Fit the SVM model to the given data with N training examples and d features.

Parameters:
  • X (numpy.ndarray) – The training data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The training labels arranged as a numpy array of shape (N,) where each element is in {-1, 1} or {0, 1}.

  • eval_train (bool) – Whether to print the training accuracy after training is done.

Note:

This function assume C is not too small; otherwise, it becomes hard to distinguish support vectors.

multi_fit(X, y, eval_train=False)#

Fit k classifier for k classes.

Parameters:
  • X (numpy.ndarray) – The training data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The training labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1

  • eval_train (bool) – Whether to print the training accuracy after training is done.

multi_predict(X)#

Predict the labels for given test data.

Parameters:

X (numpy.ndarray) – The test data arranged as a float numpy array of shape (N, d)

Returns:

The predicted labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1 and the corresponding highest scores.

Return type:

tuple

predict(X_t)#

Predict the labels for given test data.

Parameters:

X_t – The test data arranged as a float numpy array of shape (N, d)

Returns:

The predicted labels arranged as a numpy array of shape (N,) where each element is in {-1, 1} or {0, 1}.

Returns:

The predicted labels arranged as a numpy array of shape (N,) and the scores for the positive class.

Return type:

tuple

botiverse.models.SVM.SVM.SVMClass(func)#
botiverse.models.SVM.SVM.fit(self, X, y, eval_train=False)[source]#

Fit the SVM model to the given data with N training examples and d features.

Parameters:
  • X (numpy.ndarray) – The training data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The training labels arranged as a numpy array of shape (N,) where each element is in {-1, 1} or {0, 1}.

  • eval_train (bool) – Whether to print the training accuracy after training is done.

Note:

This function assume C is not too small; otherwise, it becomes hard to distinguish support vectors.

botiverse.models.SVM.SVM.multi_fit(self, X, y, eval_train=False)[source]#

Fit k classifier for k classes.

Parameters:
  • X (numpy.ndarray) – The training data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The training labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1

  • eval_train (bool) – Whether to print the training accuracy after training is done.

botiverse.models.SVM.SVM.predict(self, X_t)[source]#

Predict the labels for given test data.

Parameters:

X_t – The test data arranged as a float numpy array of shape (N, d)

Returns:

The predicted labels arranged as a numpy array of shape (N,) where each element is in {-1, 1} or {0, 1}.

Returns:

The predicted labels arranged as a numpy array of shape (N,) and the scores for the positive class.

Return type:

tuple

botiverse.models.SVM.SVM.evaluate(self, X, y)[source]#

Compare the predicted labels for given test data y with the actual labels by passing X to model.

Parameters:
  • X (numpy.ndarray) – The test data arranged as a float numpy array of shape (N, d)

  • y (numpy.ndarray) – The test labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1

Returns:

The accuracy of the model on the given test data.

Return type:

float

botiverse.models.SVM.SVM.multi_predict(self, X)[source]#

Predict the labels for given test data.

Parameters:

X (numpy.ndarray) – The test data arranged as a float numpy array of shape (N, d)

Returns:

The predicted labels arranged as a numpy array of shape (N,) where each element is an integer between 0 and k-1 and the corresponding highest scores.

Return type:

tuple

Module contents#