Median Generalized Learning Vector Quantization

Implements median generalized vector quantization, inspired by the paper

Nebel, D., Hammer, B., Frohberg, K., & Villmann, T. (2015). Median variants of learning vector quantization for learning of dissimilarity data. Neurocomputing, 169, 295-305. doi:10.1016/j.neucom.2014.12.096

class proto_dist_ml.mglvq.MGLVQ(K, T=50, phi=None)

A median generalized learning vector quantization model aims at identifying a subset of data points, called prototypes, which maximize the accuracy of a one-nearest neighbor classifier based on only this subet of data points. This makes such a model quite efficient because we only need the distances to the prototypes for classification.

K

The number of prototypes per class to be learned.

Type:int
T

The number of epochs in training.

Type:int (optional, default=50)
phi

A squashing function to post-process each error term. Defaults to the identity.

Type:function handle (optional, default=identity)
_m

The number of training data points. This is not set by the user but during fit().

Type:int
_w

A K dimensional index vector storing for each prototype the index in the training data. This is not set by the user but during fit().

Type:array_like
_y

A K dimensional label vector. This is not set by the user but during fit().

Type:array_like
_loss

The GLVQ loss during the last training run. This is not set by the user but during fit().

Type:array_like
fit(D, y)

Fits a median generalized learning vector quantization model to the given distance matrix.

In more detail, the training iterates the following steps:

  1. re-compute the GLVQ error term for each data point, which is given as self.phi[(d_i^+ - d_i^-) / (d_i^+ + d_i^-)], where d_i^+ is the distance to the closest prototype with the same label and d_i^- is the distance to the closest prototype with another label.
  2. rank the prototypes according to their contributions to the overall loss.
  3. Start with the highest ranking prototype, iterate over all data points to which it is the closest with the same label, and check whether we could improve the error by switching the prototype location to that data point.

Note that step 3 can, at worst, take m² steps because for each potential new prototype location we need to check the change in error for each other data point. However, in practice step 3 will take much fewer computations because we can greedily accept the first change that improves our loss.

Parameters:
  • D (array_like) – A m x m matrix of pairwise distances. Note that we have no preconditions for this matrix. It may even be asymmetric.
  • y (array_like) – A m dimensional label vector for the data points.
Returns:

self

Return type:

class proto_dist_ml.mglvq.MGLVQ

predict(D)

Predicts the label for the given test data, represented by their distances either to all training data points or only to the prototypes.

Parameters:D (A n x m matrix of distances from the test to the training) – datapoints OR a n x K matrix of distances from the test datapoints to the prototypes
Returns:y – a n-dimensional vector containing the predicted labels for each test datapoint.
Return type:array_like