-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsvm_common.cpp
More file actions
executable file
·175 lines (160 loc) · 3.84 KB
/
svm_common.cpp
File metadata and controls
executable file
·175 lines (160 loc) · 3.84 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
*******************************************************************************
*
* Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
*
*******************************************************************************
*
* @file: svm_common.cpp
* @author: mazefeng(mazefeng01@baidu.com)
* @date: 2015/06/30 11:10:00
*
*/
#include "svm_common.h"
using std::ifstream;
using std::ofstream;
using std::cerr;
using std::cout;
using std::endl;
using std::ostringstream;
int split(const TString& s, char c, TStringArray& v){
TString::size_type i = 0;
TString::size_type j = s.find(c);
if (j != TString::npos){
while (j != TString::npos){
v.push_back(s.substr(i, j - i));
i = ++j;
j = s.find(c, j);
if (j == TString::npos){
v.push_back(s.substr(i, s.length()));
}
}
}
else{
v.push_back(s);
}
return 0;
}
bool cmp(const TVectorDim& p1, const TVectorDim& p2){
return p1.first < p2.first;
}
float dot_product(const TVector& v1, const TVector& v2){
float dot = 0.0;
int p1 = 0;
int p2 = 0;
int a1 = -1;
int a2 = -1;
while (p1 < v1.size() && p2 < v2.size()){
a1 = v1[p1].first;
a2 = v2[p2].first;
if (a1 == a2){
dot += v1[p1].second * v2[p2].second;
p1++;
p2++;
}
else if (a1 > a2){
p2++;
}
else{
p1++;
}
}
return dot;
}
void print_TVector(const TVector& v){
for (int i = 0; i < v.size(); i++){
cerr << v[i].first << ':' << v[i].second << endl;
}
}
TVector operator*(const TVector& v, float f){
TVector p;
for (int i = 0; i < v.size(); i++){
p.push_back(TVectorDim(v[i].first, v[i].second * f));
}
return p;
}
TVector operator*(float f, const TVector& v){
return v * f;
}
TVector operator+(const TVector& v1, const TVector& v2){
TVector s;
int p1 = 0;
int p2 = 0;
int a1 = -1;
int a2 = -1;
while (p1 < v1.size() && p2 < v2.size()){
a1 = v1[p1].first;
a2 = v2[p2].first;
if (a1 == a2){
TVectorDim p(a1, v1[p1].second + v2[p2].second);
s.push_back(p);
p1++;
p2++;
}
else if (a1 > a2){
s.push_back(v2[p2]);
p2++;
}
else{
s.push_back(v1[p1]);
p1++;
}
}
for (; p1 < v1.size(); p1++){
s.push_back(v1[p1]);
}
for (; p2 < v2.size(); p2++){
s.push_back(v2[p2]);
}
return s;
}
int batch_read_sample(ifstream& is, TVectorArray& x_array, TFloatArray& y_array, int& n){
TString s;
TVector x;
float y = 0.0;
n = 0;
while (getline(is, s, '\n')){
read_sample(s, x, y);
x_array.push_back(x);
y_array.push_back(y);
n += 1;
}
return 0;
}
int batch_write_sample(ofstream& os, TVectorArray& x_array, TFloatArray& y_array, int& n){
TString s;
n = 0;
for (int i = 0; i < x_array.size(); i++){
write_sample(s, x_array[i], y_array[i]);
os << s << endl;
n += 1;
}
return 0;
}
int read_sample(TString& s, TVector& x, float& y){
int key = -1;
float val = 0.0;
TStringArray s_arr;
split(s, ' ', s_arr);
y = atof(s_arr[0].c_str());
x.clear();
for (int i = 1; i < s_arr.size(); i++){
TStringArray kv;
split(s_arr[i], ':', kv);
key = atoi(kv[0].c_str());
val = atof(kv[1].c_str());
TVectorDim p(key, val);
x.push_back(p);
}
sort(x.begin(), x.end(), cmp);
return 0;
}
int write_sample(TString& s, TVector& x, float& y){
ostringstream oss;
oss << y;
for (int i = 0; i < x.size(); i++){
oss << ' ' << x[i].first << ':' << x[i].second;
}
s = oss.str();
return 0;
}