Large margin nearest neighbours (LMNN) learns a Mahalanobis metric with so that each point's target neighbours are close and points of other classes (impostors) stay at least a margin further away:
Implement:
target_neighbours(X, y, k): an integer array of shape (N, k) whose row lists the nearest points with the same label as (excluding ), nearest first, by Euclidean distance; ties go to the smaller index. Every class has more than members.lmnn_loss(L, X, y, k, margin, lam) returning (loss, grad, n_active):
target_neighbours(X, y, k) (Euclidean, not the learned metric);grad is a subgradient with the shape of L (L may be rectangular, ), where a hinge term counts as active only when its argument is strictly positive;n_active is the number of active triples.