def __init__(self, alpha, threshold, sample, **kwargs): r"""See ``SVMClassifier`` for full documentation. """ Classifier.__init__(self) num_patterns = len(sample) check_svm_classification_sample(sample) self.dim = len(sample[0].pattern) if len(alpha) != num_patterns: raise ValueError('The supplied sample and multipliers vector do \ not have the same size') self.sv_indices = [i for i in range(len(alpha)) if alpha[i] != 0] self.support_vectors = [sample[i].pattern for i in self.sv_indices] self.signed_alphas = [alpha[i] * sample[i].label for i in self.sv_indices] self.threshold = threshold try: self.kernel = kwargs['kernel'] except KeyError: self.kernel = Kernel.get_default()
def __repr__(self): alpha = [abs(a) for a in self.signed_alphas] # was # map(abs, self.signed_alphas) patterns = self.support_vectors labels = [sign(a) for a in self.signed_alphas] # was # labels = map(sign, self.signed_alphas) sample = [LabeledExample(*pl) for pl in zip(patterns, labels)] # was # sample = map(lambda x: LabeledExample(*x), zip(patterns, labels)) result = 'SVMClassifier(' + str(alpha) + ', ' result += str(self.threshold) + ', ' result += str(sample.__repr__()) if self.kernel != Kernel.get_default(): result += ', kernel = ' + str(self.kernel.__repr__()) result += ')' return result