def test_cv_validator_mc(self): """ It tests cross-validation for a multi-class TSVM-based estimator. """ expected_output = [100, 0.0] eval_m = Validator(self.mc_X, self.mc_y, ('CV', 5), self.mc_clf) eval_func = eval_m.choose_validator() acc, std, _ = eval_func({'C1': 1, 'C2': 1}) self.assertEqual([acc, std], expected_output)
def test_tt_validator_mc(self): """ It tests train/test split for a multi-class TSVM-based estimator. """ expected_output = [100, 0.0] eval_m = Validator(self.mc_X, self.mc_y, ('t_t_split', 0.3), self.mc_clf) eval_func = eval_m.choose_validator() acc, std, _ = eval_func({'C1': 1, 'C2': 1}) self.assertEqual([acc, std], expected_output)
def test_save_model(self): """ It saves a TSVM-based model on the disk for test purpose. """ if not isfile(self.save_model_path): tsvm_model = TSVM() eval_m = Validator(self.X, self.y, ('CV', 5), tsvm_model) save_model(eval_m, {'C1': 1, 'C2': 1}, self.save_model_path)
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)
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)
# LIBTwinSVM: A Library for Twin Support Vector Machines # Developers: Mir, A. and Mahdi Rahbar # License: GNU General Public License v3.0 """ A benchmark script for demonstrating the effectiveness of LIBTwinSVM in terms of prediction accuracy. """ from libtsvm.preprocess import DataReader from libtsvm.estimators import TSVM from libtsvm.model_selection import Validator, grid_search data_path = '../dataset/australian.csv' dataset = DataReader(data_path, ',', True) dataset.load_data(True, False) X, y, _ = dataset.get_data() tsvm_clf = TSVM(kernel='linear') val = Validator(X, y, ('CV', 5), tsvm_clf) eval_method = val.choose_validator() params = {'C1': (-5, 5), 'C2': (-5, 5), 'gamma': None} best_acc, best_acc_std, opt_params, _ = grid_search(eval_method, params) print("Best accuracy: %.2f+-%.2f | Optimal parameters: %s" % (best_acc, best_acc_std, str(opt_params)))
def test_choose_validator(self): """ It tests whether the right validator is returned. """ output = [] eval_m = Validator(self.bin_X, self.bin_y, ('CV', 5), self.bin_clf) output.append(eval_m.choose_validator().__name__ \ == Validator.cv_validator.__name__) eval_m = Validator(self.bin_X, self.bin_y, ('t_t_split', 0.3), self.bin_clf) output.append(eval_m.choose_validator().__name__ \ == Validator.tt_validator.__name__) eval_m = Validator(self.mc_X, self.mc_y, ('CV', 5), self.mc_clf) output.append(eval_m.choose_validator().__name__ \ == Validator.cv_validator_mc.__name__) eval_m = Validator(self.mc_X, self.mc_y, ('t_t_split', 0.3), self.mc_clf) output.append(eval_m.choose_validator().__name__ \ == Validator.tt_validator_mc.__name__) self.assertEqual(True, all(output))