Variable-quality SV classifiers

This recipe explains how yaplf can deal with data of variable quality in SV classification. A basic knowledge of the python programming language is required, as well as the comprehension of the basic concepts related to SV classification with data of variable quality.

A light gray cell denotes one or more python statements, while a subsequent darw gray cell contains the expected output of the above statements. Statements can either be executed in a python or in a sage shell. For sake of visualization this document assumes that statements are executed in a sage notebook, so that graphics are shown right after the cell generating them. The execution in a pure python environment works in the same way, the only difference being that graphic functions return a matplotlib object that can be dealt with as usual.

Learning a SV classifier exploiting data of variable quality

Data of variable quality can be used in yaplf through the AccuracyExample decorator class, adding a generic example a numeric evaluation of its quality with reference to accuracy. Moreover, the SVMVQClassificationAlgorithm can be used in order to learn a SV classifier starting from a sample of AccuracyExample instances as follows:

from yaplf.data import LabeledExample, AccuracyExample
and_sample = [AccuracyExample(LabeledExample((1., 1.), 1.), 0.),
  AccuracyExample(LabeledExample((-1., -1.), -1.), 0.),
  AccuracyExample(LabeledExample((-1., 1.), 1.), -1.3),
  AccuracyExample(LabeledExample((1., -1.), 1.), 0.)]
from yaplf.algorithms.svm.classification import SVMVQClassificationAlgorithm
from yaplf.models.kernel import PolynomialKernel
alg = SVMVQClassificationAlgorithm(and_sample,
  kernel = PolynomialKernel(degree = 1))
alg.run()
model = alg.model
model.plot((-1.2, 1.2), (-1.2, 1.2), shading = True)
sage-plot-0

In previous cell, the upper-left example is given a negative quality profile and indeed the corresponding pattern is misclassified precisely as this negative value qualifies the example as unreliable. A more complex example involves how the result of this experiment scales with the quality profile. This can be achieved via definition of a get_accuracy_plot function modifying the quality profile of the upper-left point, running again the algorithm, now with a second degree polynomial kernel function, and returning the plot of the inferred SV classifier boundary between the two classes:

def get_accuracy_plot(accuracy):
  try:
    and_sample[2].accuracy = accuracy
    alg = SVMVQClassificationAlgorithm(and_sample,
	 kernel = PolynomialKernel(degree = 2))
    alg.run()
    model = alg.model
    return model.plot((-1.2, 1.2), (-1.2, 1.2), shading = False)
  except ArithmeticError:
    return 0

In this way, a set of different class boundaries can be obtained and joined in a unique plot:

sum([get_accuracy_plot(acc) for acc in [-2 + 4. * i/ 10 for i in range(11)]]) 
sage-output-1