-
Notifications
You must be signed in to change notification settings - Fork 218
/
svm.R
49 lines (47 loc) · 2.31 KB
/
svm.R
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
38
39
40
41
42
43
44
45
46
47
48
49
check_location <- function(){
if(Sys.info()['sysname'] == 'Windows'){
if(!file.exists("../build/bin/Debug/thundersvm.dll")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/bin/Debug/thundersvm.dll")
} else if(Sys.info()['sysname'] == 'Linux'){
if(!file.exists("../build/lib/libthundersvm.so")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/lib/libthundersvm.so")
} else if(Sys.info()['sysname'] == 'Darwin'){
if(!file.exists("../build/lib/libthundersvm.dylib")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/lib/libthundersvm.dylib")
} else{
stop("OS not supported!")
}
}
check_location() # Run this when the file is sourced
svm_train_R <-
function(
svm_type = 0, kernel = 2,degree = 3,gamma = 'auto',
coef0 = 0.0, nu = 0.5, cost = 1.0, epsilon = 0.1,
tol = 0.001, probability = FALSE, class_weight = 'None', cv = '-1',
verbose = FALSE, max_iter = -1, n_cores = -1, dataset = 'None', model_file = 'None'
)
{
check_location()
if(!file.exists(dataset)){stop("The file containing the training dataset provided as an argument in 'dataset' does not exist")}
res <- .C("train_R", as.character(dataset), as.integer(kernel), as.integer(svm_type),
as.integer(degree), as.character(gamma), as.double(coef0), as.double(nu),
as.double(cost), as.double(epsilon), as.double(tol), as.integer(probability),
as.character(class_weight), as.integer(length(class_weight)), as.integer(cv),
as.integer(verbose), as.integer(max_iter), as.integer(n_cores), as.character(model_file))
}
svm_predict_R <-
function(
test_dataset = 'None', model_file = 'None', out_file = 'None'
)
{
check_location()
if(!file.exists(test_dataset)){stop("The file containing the training dataset provided as an argument in 'test_dataset' does not exist")}
if(!file.exists(model_file)){stop("The file containing the model provided as an argument in 'model_file' does not exist")}
res <- .C("predict_R", as.character(test_dataset), as.character(model_file), as.character(out_file))
}