forked from zygmuntz/phraug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsvm2vw.py
More file actions
31 lines (25 loc) · 707 Bytes
/
libsvm2vw.py
File metadata and controls
31 lines (25 loc) · 707 Bytes
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
"convert a libsvm file to VW format"
"skip malformed lines"
"in case of binary classification with 0/1 labels set the third argument to True"
"this will convert labels to -1/1"
import sys
input_file = sys.argv[1]
output_file = sys.argv[2]
try:
convert_zero_to_negative_one = bool( sys.argv[3] )
except IndexError:
convert_zero_to_negative_one = False
i = open( input_file )
o = open( output_file, 'wb' )
for line in i:
try:
y, x = line.split( " ", 1 )
# ValueError: need more than 1 value to unpack
except ValueError:
print "line with ValueError (skipping):"
print line
continue
if convert_zero_to_negative_one and y == '0':
y = '-1'
new_line = y + " |n " + x
o.write( new_line )