-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsvmlwrite.m
More file actions
74 lines (69 loc) · 1.97 KB
/
Copy pathsvmlwrite.m
File metadata and controls
74 lines (69 loc) · 1.97 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
function svmlwrite(fname, X, Y, floatformat)
% SVMLWRITE - Write matrix into data file for SVM light
%
% SVMLWRITE(FNAME, X) writes out matrix X into file FNAME, in the format
% needed for SVM light. X is a matrix of data points, with one point
% per row.
% SVMLWRITE(FNAME, X, Y) writes out data points X with target values
% given in the column vector Y. In the case of classification, Y(i) may
% either be +1, -1 or 0 (indicating unknown class label, for
% transductive SVM). In the case of regression data, Y(i) is
% real-valued.
% SVMLWRITE(FNAME, X, Y, FLOATFORMAT) uses format string FLOATFORMAT to
% write out the features in X (and in case of regression, also the
% target values Y(i)). Default: '%.16g'
%
% See also
% SVM_LEARN, SVMLOPT, SVM_CLASSIFY, SVMLREAD
%
%
% Copyright (c) by Anton Schwaighofer (2001)
% $Revision: 1.8 $ $Date: 2002/02/19 12:28:03 $
% mailto:anton.schwaighofer@gmx.net
%
% This program is released unter the GNU General Public License.
%
error(nargchk(2, 4, nargin));
if nargin<4,
floatformat = '%.16g';
end
[N, d] = size(X);
if nargin<3,
Y = zeros(N, 1);
end
if isempty(Y),
Y = zeros(N, 1);
end
if ~all(size(Y)==[N 1]),
error('Input parameter Y must be a column vector with length SIZE(X,1)');
end
f = fopen(fname, 'wt');
if (f<0),
error(sprintf('Unable to open file %s', fname));
end
% Check whether this is a regression or a classification problem
uY = unique(Y);
if isempty(setdiff(uY, [-1 0 +1])),
% Classification:
labelformat = '%i ';
else
% Regression:
labelformat = [floatformat ' '];
end
% transpose for increased efficiency when working with sparse matrices
X = X';
fprintf('Writing ');
for i = 1:N,
Xi = X(:,i);
% Write label as the first entry
s = sprintf(labelformat, Y(i));
% Then follow 'feature:value' pairs
ind = find(Xi);
s = [s sprintf(['%i:' floatformat ' '], [ind'; full(Xi(ind))'])];
fprintf(f, '%s\n', s);
if (rem(i,100)==0),
fprintf(' %i', i);
end
end
fprintf(' done.\n');
fclose(f);