Code for multiclass
"""A module for SVM^python for multiclass learning."""
import svmapi
def read_examples(filename, sparm):
"""Parses an input file into an example sequence."""
examples = []
for line in file(filename):
if line.find('#'): line = line[:line.find('#')]
tokens = line.split()
if not tokens: continue
target = int(tokens[0])
tokens = [tuple(t.split(':')) for t in tokens[1:]]
features = [(0,1)]+[(int(k),float(v)) for k,v in tokens]
examples.append((svmapi.Sparse(features), target))
print len(examples),'examples read'
return examples
def init_model(sample, sm, sparm):
"""Store the number of features and classes in the model."""
sm.num_features = max(max(x) for x,y in sample)[0]+1
sm.num_classes = max(y for x,y in sample)
sm.size_psi = sm.num_features * sm.num_classes
print 'size_psi set to',sm.size_psi
def classification_score(x,y,sm,sparm):
"""Return an example, label pair discriminant score."""
return sm.svm_model.classify(psi(x,y,sm,sparm))
def classify_example(x, sm, sparm):
"""Returns the classification of an example 'x'."""
scores = [(classification_score(x,c,sm,sparm), c)
for c in xrange(1,sm.num_classes+1)]
return max(scores)[1]
def find_most_violated_constraint(x, y, sm, sparm):
"""Returns the most violated constraint for example (x,y)."""
scores = [(classification_score(x,c,sm,sparm)+loss(y,c,sparm), c)
for c in xrange(1,sm.num_classes+1)]
return max(scores)[1]
def psi(x, y, sm, sparm):
"""Returns the combined feature vector Psi(x,y)."""
offset = sm.num_features * (y-1)
pvec = svmapi.Sparse([(k+offset,v) for k,v in x], kernel_id=y)
return pvec