forked from jkrijthe/RSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvmlin_rcpp.cpp
More file actions
128 lines (116 loc) · 4.95 KB
/
svmlin_rcpp.cpp
File metadata and controls
128 lines (116 loc) · 4.95 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Copyright 2006 Vikas Sindhwani ([email protected])
SVM-lin: Fast SVM Solvers for Supervised and Semi-supervised Learning
This file is part of SVM-lin.
SVM-lin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SVM-lin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SVM-lin (see gpl.txt); if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Rcpp.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <cstring>
#include "ssl.h"
using namespace Rcpp;
struct options *Options = new options[1];
struct data *Data = new data[1];
struct vector_double *Weights = new vector_double[1];
struct vector_double *Outputs = new vector_double[1];
struct vector_double *Labels = new vector_double[1];
// [[Rcpp::export]]
List svmlin_rcpp(S4 X,
NumericVector y,
int l,
int algorithm,
double lambda,
double lambda_u,
int max_switch,
double pos_frac,
double Cp,
double Cn,
NumericVector costs,
bool verbose) {
// Set options
Options->algo = algorithm;
Options->lambda=lambda;
Options->lambda_u=lambda_u;
Options->S=max_switch;
Options->R=pos_frac;
Options->epsilon=EPSILON;
Options->cgitermax=CGITERMAX;
Options->mfnitermax=MFNITERMAX;
Options->Cp = Cp;
Options->Cn = Cn;
Options->verbose = verbose;
NumericVector ycop( y.begin(), y.end() );
NumericVector costcop( costs.begin(), costs.end() );
// Rprintf("Step 1\n");
// size_t size = ((DoubleVector)X.slot("x")).length()+((DoubleVector)Xu.slot("x")).length();
// std::vector<double> vals(size);
// std::vector<double>::iterator it = vals.begin();
// vals.insert(it,((DoubleVector)X.slot("x")).begin(),((DoubleVector)Xu.slot("x")).end());
// it = vals.begin()+((DoubleVector)X.slot("x")).length();
// vals.insert(it,((DoubleVector)Xu.slot("x")).begin(),((DoubleVector)Xu.slot("x")).end());
//
// Rprintf("Step 2\n");
//
// size = ((IntegerVector)X.slot("i")).length()+((IntegerVector)Xu.slot("i")).length();
// std::vector<int> colinds(size);
// std::vector<int>::iterator it2 = colinds.begin();
// colinds.insert(it2,((IntegerVector)X.slot("i")).begin(),((IntegerVector)X.slot("i")).end());
// it2 = colinds.begin() + ((IntegerVector)X.slot("i")).length();
// colinds.insert(it2,((IntegerVector)Xu.slot("i")).begin(),((IntegerVector)Xu.slot("i")).end());
//
// size = ((IntegerVector)X.slot("p")).length()+((IntegerVector)Xu.slot("p")).length();
// std::vector<int> rowpts(size);
// it2 = rowpts.begin();
// rowpts.insert(it2,((IntegerVector)X.slot("p")).begin(),((IntegerVector)X.slot("p")).end());
// it2 = rowpts.begin() + ((IntegerVector)X.slot("p")).length();
// rowpts.insert(it2,((IntegerVector)Xu.slot("p")).begin(),((IntegerVector)Xu.slot("p")).end());
// R data to svmlin data structure
Data->m=((IntegerVector)X.slot("Dim"))[1];
Data->l=l;
Data->u=Data->m-Data->l;
Data->n=((IntegerVector)X.slot("Dim"))[0];
Data->nz=((DoubleVector)X.slot("x")).size();
Data->val=((DoubleVector)X.slot("x")).begin();
Data->rowptr=((IntegerVector)X.slot("p")).begin();
Data->colind=((IntegerVector)X.slot("i")).begin();
Data->Y=ycop.begin();
Data->C=costcop.begin();
// TODO: load correct costs for unlabeled data.
if (Options->verbose) {
Rcout << " Input Data Matrix Statistics:" << std::endl;
Rcout << " Examples: " << Data->m << std::endl;
Rcout << " Features: " << Data->n << " (including bias feature)" << std::endl;
Rcout << " Non-zeros: " << Data->nz << " (including bias features)" << std::endl;
Rcout << " Average sparsity: " << Data->nz*1.0/Data->m << " non-zero features per example." << std::endl;
}
// for (int i = 0; i<((DoubleVector)X.slot("x")).length();i++) {
// Rprintf("val: %f \n",Data->val[i]);
// }
// for (int i = 0; i<((IntegerVector)X.slot("i")).length();i++) {
// Rprintf("col: %d \n",Data->colind[i]);
// }
// for (int i = 0; i<((IntegerVector)X.slot("p")).length();i++) {
// Rprintf("row: %d \n",Data->rowptr[i]);
// }
// Run
ssl_train(Data,
Options,
Weights,
Outputs);
//Clear(Data);
return Rcpp::List::create(Rcpp::Named("Weights") = std::vector<double>(Weights->vec, Weights->vec+Weights->d),
Rcpp::Named("Outputs") = std::vector<double>(Outputs->vec, Outputs->vec+Outputs->d)
);
}