-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
kernel_support_vector_machine.sg
37 lines (30 loc) · 1.26 KB
/
kernel_support_vector_machine.sg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
CSVFile f_feats_train("../../data/classifier_binary_2d_nonlinear_features_train.dat")
CSVFile f_feats_test("../../data/classifier_binary_2d_nonlinear_features_test.dat")
CSVFile f_labels_train("../../data/classifier_binary_2d_nonlinear_labels_train.dat")
CSVFile f_labels_test("../../data/classifier_binary_2d_nonlinear_labels_test.dat")
#![create_features]
RealFeatures features_train(f_feats_train)
RealFeatures features_test(f_feats_test)
BinaryLabels labels_train(f_labels_train)
BinaryLabels labels_test(f_labels_test)
#![create_features]
#![set_parameters]
Kernel k = kernel("GaussianKernel", log_width=1.0074515102711323)
#![set_parameters]
#![create_instance]
KernelMachine svm = kernel_machine("LibSVM", C1=1.0, C2=1.0, kernel=k, labels=labels_train, epsilon=0.001)
#![create_instance]
#![train_and_apply]
svm.train(features_train)
BinaryLabels labels_predict = svm.apply_binary(features_test)
#![train_and_apply]
#![extract_weights_bias]
RealVector alphas = svm.get_real_vector("m_alpha")
real b = svm.get_real("m_bias")
#![extract_weights_bias]
#![evaluate_accuracy]
AccuracyMeasure eval()
real accuracy = eval.evaluate(labels_predict, labels_test)
#![evaluate_accuracy]
# additional integration testing variables
RealVector output = labels_predict.get_real_vector("labels")