Support vector machines
SVC
- class DLL.MachineLearning.SupervisedLearning.SupportVectorMachines.SVC(kernel=<DLL.MachineLearning.SupervisedLearning.Kernels.RBF object>, C=1, opt_method='coord_ascent')[source]
Bases:
object
The support vector machine classifier. “cvxopt”-optimization method’s implementation is largly based on this article. The “smo”-optimization algorithm is based on this paper.
- Parameters:
kernel (Kernels, optional) – The non-linearity function for fitting the model. Defaults to RBF.
C (float or int, optional) – A regularization parameter. Defaults to 1. Must be positive real number.
opt_method (str, optional) – The method that will be used in the optimization step. Must be in [“cvxopt”, “smo”, “coord_ascent”]. Defaults to “coord_ascent”. For optimal results one should experiment with every optimization method, but as a rule of thumb, “coord_ascent” is the most efficient.
- n_features
The number of features. Available after fitting.
- Type:
int
- alpha
The optimized dual coefficients. Available after fitting.
- Type:
torch.Tensor of shape (n_samples,)
- fit(X, y, epochs=10, multi_method='ovr')[source]
Fits the SVC model to the input data by finding the hyperplane that separates the data with maximum margin. It uses the optimization method defined in the constructor of the class.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature.
y (torch.Tensor of shape (n_samples,)) – The labels corresponding to each sample. Every element must be in [0, …, n_classes - 1].
epochs (int, optional) – The number of iterations the training loop runs. Defaults to 100. Must be a positive integer. Is ignored for opt_method=”cvxopt”.
multi_method (str, optional) – The method for multi-class classification. Is ignored for binary classification. Must be one of “ovr” or “ovo”. Defaults to “ovr”.
- Returns:
None
- Raises:
TypeError – If the input matrix or the label matrix is not a PyTorch tensor or multi_method is not a string.
ValueError – If the input matrix or the label matrix is not the correct shape or multi_method is not in allowed methods.
- predict(X)[source]
Applies the fitted SVC model to the input data, predicting the correct classes.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.
- Returns:
The predicted labels corresponding to each sample.
- Return type:
labels (torch.Tensor of shape (n_samples,))
- Raises:
NotFittedError – If the SVC model has not been fitted before predicting.
TypeError – If the input matrix is not a PyTorch tensor.
ValueError – If the input matrix is not the correct shape.
SVR
- class DLL.MachineLearning.SupervisedLearning.SupportVectorMachines.SVR(kernel=<DLL.MachineLearning.SupervisedLearning.Kernels.RBF object>, C=1, epsilon=0.1)[source]
Bases:
object
The support vector machine regressor with a quadratic programming solver.
- Parameters:
kernel (Kernels, optional) – The non-linearity function for fitting the model. Defaults to RBF.
C (float or int, optional) – A regularization parameter. Defaults to 1. Must be positive real number.
epsilon (float or int, optional) – The width of the tube of no penalty in epsilon-SVR. Must be a positive real number.
- n_features
The number of features. Available after fitting.
- Type:
int
- alpha
The optimized dual coefficients. Available after fitting.
- Type:
torch.Tensor of shape (n_samples,)
- alpha_star
The optimized dual coefficients. Available after fitting.
- Type:
torch.Tensor of shape (n_samples,)
- fit(X, y)[source]
Fits the SVR model to the input data by finding the hypertube that contains the data with minimum loss.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature.
y (torch.Tensor of shape (n_samples,)) – The target values corresponding to each sample.
- Returns:
None
- Raises:
TypeError – If the input matrix or the target matrix is not a PyTorch tensor.
ValueError – If the input matrix or the target matrix is not the correct shape.
- predict(X)[source]
Applies the fitted SVR model to the input data, predicting the correct values.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data to be regressed.
- Returns:
The predicted values corresponding to each sample.
- Return type:
target values (torch.Tensor of shape (n_samples,))
- Raises:
NotFittedError – If the SVR model has not been fitted before predicting.
TypeError – If the input matrix is not a PyTorch tensor.
ValueError – If the input matrix is not the correct shape.