-
Notifications
You must be signed in to change notification settings - Fork 2
/
libsvm2bin.cpp
189 lines (157 loc) · 3.69 KB
/
libsvm2bin.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <vector.h>
#include <math.h>
#include "svqp2/svqp2.h"
#include "svqp2/vector.h"
vector <lasvm_sparsevector_t*> X; // feature vectors
vector <int> Y; // labels
int m; // number of examples
int sparse=1;
int max_index = 0;
void libsvm_load_data(char *filename)
// loads the same format as LIBSVM
{
int index; double value;
int elements, i;
FILE *fp = fopen(filename,"r");
lasvm_sparsevector_t* v;
int splitpos=0;
if(fp == NULL)
{
fprintf(stderr,"Can't open input file \"%s\"\n",filename);
exit(1);
}
else
printf("\"%s\".. ",filename);
int msz = 0;
elements = 0;
while(1)
{
int c = fgetc(fp);
switch(c)
{
case '\n':
v=lasvm_sparsevector_create();
X.push_back(v);
++msz;
//printf("%d\n",m);
elements=0;
break;
case ':':
++elements;
break;
case EOF:
goto out;
default:
;
}
}
out:
rewind(fp);
for(i=0;i<msz;i++)
{
int label;
fscanf(fp,"%d",&label);
Y.push_back(label);
while(1)
{
int c;
do {
c = getc(fp);
if(c=='\n') goto out2;
} while(char(c)==' '); //(isspace(c));
ungetc(c,fp);
fscanf(fp,"%d:%lf",&index,&value);
lasvm_sparsevector_set(X[m+i],index,value);
if (index>max_index) max_index=index;
}
out2:
label=1; // dummy
}
fclose(fp);
m=X.size();
printf("examples: %d features: %d\n",m,max_index);
}
void fullbin_save(char *fname)
{
int i,j;
ofstream f;
f.open(fname,ios::out|ios::binary);
// write number of examples and number of features
int sz[2]; sz[0]=m; sz[1]=max_index;
f.write((char*)sz,2*sizeof(int));
if (!f) { printf("File writing error in line %d.\n",i); exit(1);}
float buf[max_index+1];
for(i=0;i<m;i++)
{
sz[0]=Y[i]; // write label
f.write((char*)sz,1*sizeof(int));
// write out features for each example
for(j=0;j<max_index;j++) buf[j]=0;
lasvm_sparsevector_pair_t *p;
lasvm_sparsevector_foreach(p, X[i]) {
if(p->index>max_index)
{
printf("error! index %d??\n",p->index); exit(1);
}
buf[p->index]=p->value;
}
f.write((char*)buf,max_index*sizeof(float));
}
f.close();
}
void bin_save(char *fname)
{
int i,j;
ofstream f;
f.open(fname,ios::out|ios::binary);
// write number of examples and a 0 to say that the matrix is sparse
int sz[2]; sz[0]=m; sz[1]=0;
f.write((char*)sz,2*sizeof(int));
if (!f) {printf("File writing error in line %d.\n",i); exit(1);}
float buf[max_index];
int ind[max_index];
for(i=0;i<m;i++)
{
lasvm_sparsevector_pair_t *p;
max_index=0;
lasvm_sparsevector_foreach(p, X[i]) {
//printf("%d:%g ",p->index,p->value);
buf[max_index]=p->value;
ind[max_index]=p->index;
max_index++;
}
sz[0]=Y[i]; // write label
sz[1]=max_index; // write length of example (how many nonsparse entries)
f.write((char*)sz,2*sizeof(int));
f.write((char*)ind,max_index*sizeof(int)); // write indices
f.write((char*)buf,max_index*sizeof(float)); // write values
if (!f) {printf("File writing error in line %d.\n",i); exit(1);}
}
f.close();
}
int main(int argc, char **argv)
{
printf("\n");
printf("libsvm2bin file converter\n");
printf("_________________________\n");
if(argc<3 || (argc==3 && strcmp("-F",argv[1])==0))
{
printf("usage: %s [-F] <input file> <output file>\n",argv[0]);
printf(" -F full matrix : forces full rather than sparse matrix storage.\n");
exit(0);
}
if(strcmp(argv[1],"-F")==0)
{
sparse=0;printf("[storing as a full matrix]\n");
libsvm_load_data(argv[2]);
fullbin_save(argv[3]);
}
else
{
libsvm_load_data(argv[1]);
bin_save(argv[2]);
}
}