twinsvm

Classes and functios are defined for training and testing TwinSVM classifier.

TwinSVM classifier generates two non-parallel hyperplanes. For more info, refer to the original papar. Khemchandani, R., & Chandra, S. (2007). Twin support vector machines for pattern classification. IEEE Transactions on pattern analysis and machine intelligence, 29(5), 905-910.

Motivated by the following paper, the multi-class TSVM is developed. Tomar, D., & Agarwal, S. (2015). A comparison on multi-class classification methods based on least squares twin support vector machine. Knowledge-Based Systems, 81, 131-147.

Functions

rbf_kernel(x, y, u) It transforms samples into higher dimension using Gaussian (RBF) kernel.

Classes

HyperPlane() Its object represents a hyperplane
MCTSVM([kernel, C, gamma]) Multi-class Twin Support Vector Machine (One-vs-All Scheme)
OVO_TSVM([kernel, C1, C2, gamma]) Multi Class Twin Support Vector Machine (One-vs-One Scheme)
TSVM([kernel, rect_kernel, C1, C2, gamma]) Twin Support Vector Machine for binary classification.
class twinsvm.TSVM(kernel='linear', rect_kernel=1, C1=1, C2=1, gamma=1)[source]

Bases: sklearn.base.BaseEstimator

Twin Support Vector Machine for binary classification.

Parameters:

kernel : str, optional (default=’linear’)

Type of the kernel function which is either ‘linear’ or ‘RBF’.

rect_kernel : float, optional (default=1.0)

Percentage of training samples for Rectangular kernel.

C1 : float, optional (default=1.0)

Penalty parameter of first optimization problem.

C2 : float, optional (default=1.0)

Penalty parameter of second optimization problem.

gamma : float, optional (default=1.0)

Parameter of the RBF kernel function.

Attributes

mat_C_t (array-like, shape = [n_samples, n_samples]) A matrix that contains kernel values.
cls_name (str) Name of the classifier.
w1 (array-like, shape=[n_features]) Weight vector of class +1’s hyperplane.
b1 (float) Bias of class +1’s hyperplane.
w2 (array-like, shape=[n_features]) Weight vector of class -1’s hyperplane.
b2 (float) Bias of class -1’s hyperplane.

Methods

fit(X_train, y_train) It fits the binary TwinSVM model according to the given training data.
get_params([deep]) Get parameters for this estimator.
get_params_names() For retrieving the names of hyper-parameters of this classifier.
predict(X_test) Performs classification on samples in X using the TwinSVM model.
set_params(**params) Set the parameters of this estimator.
get_params_names()[source]

For retrieving the names of hyper-parameters of this classifier.

Returns:

parameters : list of str, {[‘C1’, ‘C2’], [‘C1’, ‘C2’, ‘gamma’]}

Returns the names of the hyperparameters which are same as the class’ attributes.

fit(X_train, y_train)[source]

It fits the binary TwinSVM model according to the given training data.

Parameters:

X_train : array-like, shape (n_samples, n_features)

Training feature vectors, where n_samples is the number of samples and n_features is the number of features.

y_train : array-like, shape(n_samples,)

Target values or class labels.

predict(X_test)[source]

Performs classification on samples in X using the TwinSVM model.

Parameters:

X_test : array-like, shape (n_samples, n_features)

Feature vectors of test data.

Returns:

output : array, shape (n_samples,)

Predicted class lables of test data.

twinsvm.rbf_kernel(x, y, u)[source]

It transforms samples into higher dimension using Gaussian (RBF) kernel.

Parameters:

x, y : array-like, shape (n_features,)

A feature vector or sample.

u : float

Parameter of the RBF kernel function.

Returns:

float

Value of kernel matrix for feature vector x and y.

class twinsvm.HyperPlane[source]

Bases: object

Its object represents a hyperplane

Attributes

w (array-like, shape (n_features,)) Weight vector. If the RBF kernel is used, the shape will be (n_samples,)
b (float) Bias.
class twinsvm.MCTSVM(kernel='linear', C=1, gamma=1)[source]

Bases: sklearn.base.BaseEstimator

Multi-class Twin Support Vector Machine (One-vs-All Scheme)

Parameters:

kernel : str, optional (default=’linear’)

Type of the kernel function which is either ‘linear’ or ‘RBF’.

C : float, optional (default=1.0)

Penalty parameter.

gamma : float, optional (default=1.0)

Parameter of the RBF kernel function.

Attributes

classifiers (dict) Stores an intance of HyperPlane class for each binary classifier.
mat_D_t (list of array-like objects) Stores kernel matrix for each binary classifier.
cls_name (str) Name of the classifier.

Methods

fit(X_train, y_train) It fits the OVA-TwinSVM model according to the given training data.
get_params([deep]) Get parameters for this estimator.
get_params_names() For retrieving the names of hyper-parameters of this classifier.
predict(X_test) Performs classification on samples in X using the OVA-TwinSVM model.
set_params(**params) Set the parameters of this estimator.
get_params_names()[source]

For retrieving the names of hyper-parameters of this classifier.

Returns:

parameters : list of str, {[‘C’], [‘C’, ‘gamma’]}

Returns the names of the hyperparameters which are same as the class’ attributes.

fit(X_train, y_train)[source]

It fits the OVA-TwinSVM model according to the given training data.

Parameters:

X_train : array-like, shape (n_samples, n_features)

Training feature vectors, where n_samples is the number of samples and n_features is the number of features.

y_train : array-like, shape(n_samples,)

Target values or class labels.

predict(X_test)[source]

Performs classification on samples in X using the OVA-TwinSVM model.

Parameters:

X_test : array-like, shape (n_samples, n_features)

Feature vectors of test data.

Returns:

output : array, shape (n_samples,)

Predicted class lables of test data.

class twinsvm.OVO_TSVM(kernel='linear', C1=1, C2=1, gamma=1)[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.ClassifierMixin

Multi Class Twin Support Vector Machine (One-vs-One Scheme)

The OVO_TSVM classifier is scikit-learn compatible, which means scikit-learn tools such as cross_val_score and GridSearchCV can be used for an instance of OVO_TSVM

Parameters:

kernel : str, optional (default=’linear’)

Type of the kernel function which is either ‘linear’ or ‘RBF’.

C1 : float, optional (default=1.0)

Penalty parameter of first optimization problem for each binary TSVM classifier.

C2 : float, optional (default=1.0)

Penalty parameter of second optimization problem for each binary TSVM classifier.

gamma : float, optional (default=1.0)

Parameter of the RBF kernel function.

Attributes

cls_name (str) Name of the classifier.
bin_TSVM_models_ (list) Stores intances of each binary TSVM classifier.

Methods

fit(X, y) It fits the OVO-TwinSVM model according to the given training data.
get_params([deep]) Get parameters for this estimator.
get_params_names() For retrieving the names of hyper-parameters of this classifier.
predict(X) Performs classification on samples in X using the OVO-TwinSVM model.
score(X, y[, sample_weight]) Returns the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
get_params_names()[source]

For retrieving the names of hyper-parameters of this classifier.

Returns:

parameters : list of str, {[‘C1’, ‘C2’], [‘C1’, ‘C2’, ‘gamma’]}

Returns the names of the hyperparameters which are same as the class’ attributes.

fit(X, y)[source]

It fits the OVO-TwinSVM model according to the given training data.

Parameters:

X : array-like, shape (n_samples, n_features)

Training feature vectors, where n_samples is the number of samples and n_features is the number of features.

y : array-like, shape(n_samples,)

Target values or class labels.

Returns:

self : object

predict(X)[source]

Performs classification on samples in X using the OVO-TwinSVM model.

Parameters:

X : array-like, shape (n_samples, n_features)

Feature vectors of test data.

Returns:

y_pred : array, shape (n_samples,)

Predicted class lables of test data.