Created
June 7, 2012 07:49
-
-
Save pluskid/2887246 to your computer and use it in GitHub Desktop.
Convert Sparse Feature to Dense Feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class Data: | |
def __init__(self, line): | |
data = line.split() | |
self.label = data[0] | |
self.feats = dict() | |
for i in range(len(data)-1): | |
a,b = data[i+1].split(':') | |
self.feats[int(a)] = b | |
def max_idx(self): | |
return max(self.feats.keys()) | |
def print_line(self, f, maxi): | |
f.write(self.label) | |
for i in range(maxi+1): | |
if i in self.feats: | |
f.write(" %s" % self.feats[i]) | |
else: | |
f.write(" 0") | |
f.write("\n") | |
data = [Data(x) for x in open(sys.argv[1]).readlines()] | |
max_idx = max([x.max_idx() for x in data]) | |
f = open(sys.argv[1]+'.cvt', 'w') | |
for d in data: | |
d.print_line(f, max_idx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is input format here? If you can provide an example it will be very helpful. thanks in advance.