LIBTwinSVM: A Library for Twin Support Vector Machines¶
This is the LIBTwinSVM ‘s documentation.
Usage Examples¶
User Interface¶
An example of classification using the GUI¶
In this section we have provided an easy step-by-step Usage Example which it shows how the GUI of the LIBTwinSVM works. For more information on the application and its features, go to this link.
Step 1: Data Import¶
- To use the application, the first step is to import the data. In the following section, we have shown how to import data and how to use some of the application features.
- By default, the application starts on the Data tab.
- By clicking on the Open button in the Import box, the File Explorer will be open and you can then choose the file you want to train.
- You can choose the file content separator if it is other than comma.
- In Read box in Data tab, you can choose how you want the data to be processed by the application. You may want to Normalize or Shuffle the data.
- Then you must click on Load Button, and the data will be imported and also displayed in the feature box.
Step 2: Classifying¶
- For this section, the data must be already imported and you are going to see how Classify tab works.
- Switch the tabs to Classify tab.
- Now there is several options to choose such as Classifiers, Kernel type, Evaluation Method and Hyper Parameters. You can choose one of them, or leave them with their default values and just go to the next step.
- There is a save result box on that page. Before starting the training, you have to select a path so the application saves the final results. You can also check Best Model if you want the application to save the trained model and Log FIle if you need the application logs. (Note that for using the trained pretrained model, refer to Model Example.)
- Now It is time to Classify. By Clicking on the Run! button, you can see a Confirmation message, pops up on the screen just to check if everything is exactly the way you had set before training.
- After checking you click OK if everything is the way you want and it takes a few seconds to several minutes (depends on the data size) to be done!
An example of Visualisation using the GUI¶
In this section we have provided an easy step-by-step Usage Example of Visualisation with LIBTwinSVM. For more information on the application and its features, go to this link.
Step 1: Data Import¶
- To visualize a model, the first step is to import the data. In the following section, you have shown how to import data and how to use some of the application features.
- By default, the application starts on the Data tab.
- By clicking on the Open button in the Import box, the File Explorer will be open and you can then choose the file you want to train.
- You can choose the file content separator if it is other than comma.
- In Read box in Data tab, you can choose how you want the data to be processed by the application. You may want to Normalize or Shuffle the data.
- Then you must click on Load Button, and the data will be imported and also displayed in the feature box.
Step 2: Visualisation¶
- So far, the data is already imported and you are going to see how Visualize tab works.
- You switch the tabs to Visualize tab.
- Now you have several options to choose or modify such as Classifier, Kernel type, Hyper Parameters and DPI which it determines the output plot quality.
- In the Options box in Visualize tab, there is a check-box for saving the figure. By checking that check-box, you need to select a path, so the application can save the final figure.
- In the final step by Clicking on the Plot button, the plot will be displayed in the figure box.
An example of using a pre-trained Model for classification¶
In this section, we have illustrated how to use a pretrain model, saved by the LIBTwinSVM. Note that, this step requires a pre-trained model file saved in classifying step as a .joblib file. If you don’t have the previously said file, please refer to [classication usage example](https://libtwinsvm.readthedocs.io/en/latest/examples/GUI/classify.html#).
Step 1: Data Import¶
Please note that, to use a model on test samples, the test data must have the same features as the training data. Below is a step-by-step procudure on how to load your data for reusing a pre-trained model.
- By default, the application starts on the Data tab.
![]()
- By clicking on the Open button in the Import box, the file dialog will be opened and you can then choose the dataset you want to test with a pre-trained model.
![]()
- You may need to change the column separator if it is other than comma.
- In Read box in Data tab, you may want to Normalize and/or Shuffle the data.
- Then you must click on Load Button, and the data will be loaded and also displayed in the feature box.
![]()
Step 2: Using a Pre-trained Model¶
Up to now, the dataset should be loaded. Follow the below instructions for evaluating a pre-trained model on test samples.
- First, switch the tabs to Model tab.
![]()
- Click on the Select button in the Import box to select your pre-trained model file. You can see a pre-trained model file by LIBTwinSVM in the figure below.
![]()
- After setting the pre-trained model’s path, click on the Load button to load the model and display the model’s characteristics such as its classification type and the used kernel.
![]()
- In the final step, you can evaluate your model by clicking on the Evaluate model. Moreover, if you want to save the predicted label of each sample, you can check Save Predictions button.
![]()
A one-minute example of the GUI usage¶
- This GIF file takes less than a minute to be over!
API’s examples¶
An example of binary classification using TSVM-based classifiers¶
Here, we provided an example to help you classify using binary TSVM-based classifiers that are available in the library’s API. Comments are also provided in the code example to make API usage clear.
from libtsvm.preprocess import DataReader
from libtsvm.estimators import TSVM
from libtsvm.model_selection import Validator
# Step 1: Load your dataset
data_path = '../../dataset/australian.csv'
sep_char = ',' # separtor character of the CSV file
header = True # Whether the dataset has header names.
dataset = DataReader(data_path, sep_char, header)
shuffle_data = True
normalize_data = False
dataset.load_data(shuffle_data, normalize_data)
X, y, file_name = dataset.get_data()
# Step 2: Choose a TSVM-based estimator
kernel = 'linear'
tsvm_clf = TSVM(kernel=kernel)
# Step 3: Evaluate the estimator using train/test split
eval_method = 't_t_split' # Train/Test split
test_set_size = 30 # 30% of samples
val = Validator(X, y, (eval_method, test_set_size), tsvm_clf)
eval_func = val.choose_validator()
# Hyper-parameters of the classifier
h_params = {'C1': 2**-3, 'C2': 2**-5}
acc, std, full_report = eval_func(h_params)
print("Accuracy: %.2f" % acc)
print(full_report)
An example of multi-class classification using OVO-LSTSVM¶
This example shows how you can use Least Squares TwinSVM classifier with One-vs-One strategy to solve a multi-class classification problem.
from libtsvm.preprocess import DataReader
from libtsvm.estimators import LSTSVM
from libtsvm.mc_scheme import OneVsOneClassifier
from libtsvm.model_selection import Validator
# Step 1: Load your dataset
data_path = '../../dataset/iris.csv'
sep_char = ',' # separtor character of the CSV file
header = True # Whether the dataset has header names.
dataset = DataReader(data_path, sep_char, header)
shuffle_data = True
normalize_data = False
dataset.load_data(shuffle_data, normalize_data)
X, y, file_name = dataset.get_data()
# Step 2: Choose a TSVM-based estimator
kernel = 'RBF'
lstsvm_clf = LSTSVM(kernel=kernel)
# Step 3: Select a multi-class approach
ovo_lstsvm = OneVsOneClassifier(lstsvm_clf)
# Step 4: Evaluate the multi-class estimator using train/test split
eval_method = 't_t_split' # Train/Test split
test_set_size = 20 # 20% of samples
val = Validator(X, y, (eval_method, test_set_size), ovo_lstsvm)
eval_func = val.choose_validator()
# Hyper-parameters of the classifier
h_params = {'C1': 2**-2, 'C2': 2**-2, 'gamma': 2**-7}
acc, std, full_report = eval_func(h_params)
print("Accuracy: %.2f" % acc)
print(full_report)
An example of model evaluation with cross-validation¶
This user guide is provided to help you evaluate the model with cross validation.
from libtsvm.preprocess import DataReader
from libtsvm.estimators import TSVM
from libtsvm.model_selection import Validator
# Step 1: Load your dataset
data_path = '../../dataset/hepatits.csv'
sep_char = ',' # separtor character of the CSV file
header = True # Whether the dataset has header names.
dataset = DataReader(data_path, sep_char, header)
shuffle_data = True
normalize_data = False
dataset.load_data(shuffle_data, normalize_data)
X, y, file_name = dataset.get_data()
# Step 2: Choose a TSVM-based estimator
kernel = 'linear'
tsvm_clf = TSVM(kernel=kernel)
# Step 3: Evaluate the estimator using cross validation
eval_method = 'CV' # Cross validation
folds = 5
val = Validator(X, y, (eval_method, folds), tsvm_clf)
eval_func = val.choose_validator()
# Hyper-parameters of the classifier
h_params = {'C1': 2**-2, 'C2': 2**1}
acc, std, full_report = eval_func(h_params)
print("Accuracy: %.2f" % acc)
print(full_report)
An example of model selection with grid search and cross-validation¶
In this example, a code samples is provided to help you find the best model for your classification task. The best model will be found using grid search and cross validation that are available in the library’s API. At the end, the classification results is also saved in a speadsheet file for further analysis.
from libtsvm.preprocess import DataReader
from libtsvm.estimators import TSVM
from libtsvm.model_selection import Validator, grid_search, save_result
# Step 1: Load your dataset
data_path = './dataset/australian.csv'
sep_char = ',' # separtor character of the CSV file
header = True # Whether the dataset has header names.
dataset = DataReader(data_path, sep_char, header)
shuffle_data = True
normalize_data = False
dataset.load_data(True, False)
X, y, _ = dataset.get_data()
# Step 2: Choose a TSVM-based estimator
tsvm_clf = TSVM(kernel='RBF')
# Step 3: Choose an evaluation method.
val = Validator(X, y, ('CV', 5), tsvm_clf) # 5-fold cross-validation
eval_method = val.choose_validator()
# Step 4: Specify range of each hyper-parameter for a TSVM-based estimator.
params = {'C1': (-2, 2), 'C2': (-2, 2), 'gamma': (-8, 2)}
best_acc, best_acc_std, opt_params, clf_results = grid_search(eval_method, params)
print("Best accuracy: %.2f+-%.2f | Optimal parameters: %s" % (best_acc, best_acc_std,
str(opt_params)))
# Step 5: Save the classification results
clf_type = 'binary' # Type of classification problem
save_result(val, clf_type, clf_results, 'TSVM-RBF-Australian')
API Reference¶
This page contains the list of the project’s modules
estimators |
In this module, Standard TwinSVM and Least Squares TwinSVM estimators are defined. |
mc_scheme |
In this module, multi-class schemes such as One-vs-One and One-vs-All are implemented. |
model |
This modules models data, user input in classes and functions |
model_selection |
This module contains functions and classes for model evaluation and selection. |
preprocess |
In this module, functions for reading and processing datasets are defined. |
model_eval |
This module contains code for saving, loading, and evaluating pre-trained models. |