-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsvm.cpp
More file actions
58 lines (41 loc) · 1.13 KB
/
libsvm.cpp
File metadata and controls
58 lines (41 loc) · 1.13 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
#include "libsvm.h"
#include "csr.h"
#include "file_name.h"
#include <fstream>
#include <iostream>
libsvm_c::libsvm_c(file_name_c const & fn) : file_name(fn) {
}
void libsvm_c::load(csr_c & data) {
std::ifstream input;
input.open(file_name.get_full_name());
if (!input.is_open()) {
std::cerr << "Error: unable to open " << file_name.get_file_name() << std::endl;
exit(EXIT_SUCCESS);
}
data.clear();
std::size_t r_idx = 0;
data.add_row_idx(r_idx);
for (std::string line; std::getline(input, line); ) {
std::stringstream is(line);
is >> std::skipws;
std::string label;
if (!(is >> label)) {
break;
}
data.add_label(label);
for (;; r_idx++) {
std::size_t idx;
double value;
char token;
if (!(is >> idx)) {
break;
}
is >> token; // :
is >> value;
data.add_col_idx(idx - 1); // libsvm is one based not zero
data.add_value(value);
}
data.add_row_idx(r_idx);
}
input.close();
}