Skip to main content

Large Margin Nearest Neighbor implementation in python

Project description

PyLMNN is an implementation of the Large Margin Nearest Neighbor algorithm for metric learning in pure python.

This implementation follows closely the original MATLAB code by Kilian Weinberger found at https://bitbucket.org/mlcircus/lmnn. This version solves the unconstrained optimisation problem and finds a linear transformation using L-BFGS as the backend optimizer.

This package also uses Bayesian Optimization to find the optimal hyper-parameters for LMNN using the excellent GPyOpt package.

Installation

The code was developed in python 3.5 under Ubuntu 16.04. You can clone the repo with:

git clone https://github.com/johny-c/pylmnn.git

or install it via pip:

pip3 install pylmnn

Dependencies

  • numpy>=1.11.2

  • scipy>=0.18.1

  • scikit_learn>=0.18.1

  • GPy>=1.5.6

  • GPyOpt>=1.0.3

  • matplotlib>=1.5.3

Usage

The simplest use case would be something like:

from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_mldata
from pylmnn.lmnn import LargeMarginNearestNeighbor as LMNN
from pylmnn.helpers import test_knn, plot_ba


# Load a data set
dataset = fetch_mldata('iris')
X, y = dataset.data, dataset.target

# Split in training and testing set
x_tr, x_te, y_tr, y_te = train_test_split(X, y, test_size=0.7, stratify=y, random_state=42)

# Set up the hyperparameters
k_tr, k_te, dim_out, max_iter = 3, 1, X.shape[1], 180

# Instantiate the classifier
clf = LMNN(k=k_tr, max_iter=max_iter, dim_out=dim_out)

# Train the classifier
clf = clf.fit(x_tr, y_tr)

# Compute the k-nearest neighbor test accuracy after applying the learned transformation
test_acc = test_knn(x_tr, y_tr, x_te, y_te, k=k_te, L=clf.L)

# Draw a comparison plot of the test data before and after applying the learned transformation
plot_ba(clf.L, x_te, y_te, dim_pref=3)

You can check the examples directory for a demonstration of how to use the code with different datasets and how to estimate good hyperparameters with Bayesian Optimisation.

Documentation can also be found at http://pylmnn.readthedocs.io/en/latest/ .

References

If you use this code in your work, please cite the following publication.

@ARTICLE{weinberger09distance,
    title={Distance metric learning for large margin nearest neighbor classification},
    author={Weinberger, K.Q. and Saul, L.K.},
    journal={The Journal of Machine Learning Research},
    volume={10},
    pages={207--244},
    year={2009},
    publisher={MIT Press}
}

License and Contact

This work is released under the GNU General Public License Version 3 (GPLv3).

Contact John Chiotellis :envelope: for questions, comments and reporting bugs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyLMNN-1.2.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyLMNN-1.2.1-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file PyLMNN-1.2.1.tar.gz.

File metadata

  • Download URL: PyLMNN-1.2.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyLMNN-1.2.1.tar.gz
Algorithm Hash digest
SHA256 56b4bfce2ae9465eaa64ebcad4e284fd401274af145418b99dae3d8d622278ae
MD5 696afe9013c2b10ba1e830c59a654298
BLAKE2b-256 c4578919e8c24f27246ee53e945e847a453cea712790f1ff5af50156650589df

See more details on using hashes here.

File details

Details for the file PyLMNN-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for PyLMNN-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0a49cb9fb9d559cc8fa3a06576325ceca84bd416f7e8a84b387d03e7496c5a11
MD5 e011713f7daf588a97c728ad511bf744
BLAKE2b-256 920dd6f67e4e5ef1d3203ba5a501d955d49a882285bb90a8f8a83f45fedf3e3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page