def __init__(self): self.mc = MultiClassifier() self.labelset = set() self.c = 1.0 self.j = None self.tmp = "/tmp/" self.binary = "svm_perf_learn"
class SVMMultiClassifier: def __init__(self): self.mc = MultiClassifier() self.labelset = set() self.c = 1.0 self.j = None self.tmp = "/tmp/" self.binary = "svm_perf_learn" def train(self, ds): bds = ds.binarize() positions = None print "SVMMultiClassifier: Training with %d docs, %d labels" % (len(ds.docs), len(bds)) for label in bds: if positions is None: fout = tempfile.NamedTemporaryFile(suffix='svm', dir=self.tmp) positions = bds[label].toSVM(fout) else: bds[label].toSVMSubsequent(fout, positions) fout.file.flush() modelfilename = "%s-%s.model" % (fout.name, label) _run("%s %s %s %s > /dev/null 2>&1" % (self.binary, _svm_params(self), fout.name, modelfilename)) self.mc.add(label, SVMClassifier(modelfilename)) os.remove(modelfilename) fout.close() self.labelset = set(bds) def __repr__(self): return "<SVMMultiClassfier: %d labels>" % len(self.labelset) def score(self, av): return self.mc.score(av)
class SVMMultiClassifier: def __init__(self): self.mc = MultiClassifier() self.labelset = set() self.c = 1.0 self.j = None self.tmp = "/tmp/" self.binary = "svm_perf_learn" def train(self, ds): bds = ds.binarize() positions = None print "SVMMultiClassifier: Training with %d docs, %d labels" % (len( ds.docs), len(bds)) for label in bds: if positions is None: fout = tempfile.NamedTemporaryFile(suffix='svm', dir=self.tmp) positions = bds[label].toSVM(fout) else: bds[label].toSVMSubsequent(fout, positions) fout.file.flush() modelfilename = "%s-%s.model" % (fout.name, label) _run("%s %s %s %s > /dev/null 2>&1" % (self.binary, _svm_params(self), fout.name, modelfilename)) self.mc.add(label, SVMClassifier(modelfilename)) os.remove(modelfilename) fout.close() self.labelset = set(bds) def __repr__(self): return "<SVMMultiClassfier: %d labels>" % len(self.labelset) def score(self, av): return self.mc.score(av)