Skip to content

Instantly share code, notes, and snippets.

@pluskid
Created June 7, 2012 07:49
Show Gist options
  • Save pluskid/2887246 to your computer and use it in GitHub Desktop.
Save pluskid/2887246 to your computer and use it in GitHub Desktop.
Convert Sparse Feature to Dense Feature
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)
@minesh1291
Copy link

what is input format here? If you can provide an example it will be very helpful. thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment