Skip to content

Commit 073451a

Browse files
wdevazelhesperimosocordiae
authored andcommitted
Add documentation for the new API (#133)
* Create some text to initialize the PR * DOC: add doc outline * DOC: Add data visualisation to possible examples * Update documentation outline * Add doc from master * DOC: add beginning of doc tree * DOC: add some beginning of example to get started with the examples section using sphinx-gallery * DOC: modify gitignore to ignore auto_examples * WIP: add preprocessor section and some section about weakly supervised learners and copy the previous content to the different sections * A few style improvements (text wraping to line limit, better references formatting ...) * Address #133 (review) * raise instead of return * Fix quickstart example * Emphasize scikit-learn compatibility * Update introduction with new methods * address #133 (comment) * explain what happens when preprocessor=None * Precisions in doc about the input accepted by the preprocessor * address #133 (comment) * Better formulation of sentences * change title formatting in index * Fix references and some numering issue * Reformat link to preprocessor * Fix automatic link to algorithms for the supervised section * Reformatting and adding examples about supervised version in the end of the weakly supervised version * add precisions in the intro * add precisions for score_pairs in the intro * Change examples for weakly supervised section * add _Supervised section in Supervised section * change examples in weakly supervised section * fix empty module contents * rename sandwich.py into plot_sandwich.py to be found by sphinx-gallery
1 parent 010b34a commit 073451a

22 files changed

+799
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist/
55
.coverage
66
htmlcov/
77
.cache/
8+
doc/auto_examples/*

doc/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'sphinx.ext.viewcode',
88
'sphinx.ext.mathjax',
99
'numpydoc',
10+
'sphinx_gallery.gen_gallery'
1011
]
1112

1213
templates_path = ['_templates']
@@ -15,7 +16,7 @@
1516

1617
# General information about the project.
1718
project = u'metric-learn'
18-
copyright = u'2015-2017, CJ Carey and Yuan Tang'
19+
copyright = u'2015-2018, CJ Carey and Yuan Tang'
1920
author = u'CJ Carey and Yuan Tang'
2021
version = '0.4.0'
2122
release = '0.4.0'
@@ -31,3 +32,6 @@
3132
html_static_path = ['_static']
3233
htmlhelp_basename = 'metric-learndoc'
3334

35+
# Option to only need single backticks to refer to symbols
36+
default_role = 'any'
37+

doc/getting_started.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
###############
2+
Getting started
3+
###############
4+
5+
Installation and Setup
6+
======================
7+
8+
Run ``pip install metric-learn`` to download and install from PyPI.
9+
10+
Alternately, download the source repository and run:
11+
12+
- ``python setup.py install`` for default installation.
13+
- ``python setup.py test`` to run all tests.
14+
15+
**Dependencies**
16+
17+
- Python 2.7+, 3.4+
18+
- numpy, scipy, scikit-learn
19+
- (for running the examples only: matplotlib)
20+
21+
**Notes**
22+
23+
If a recent version of the Shogun Python modular (``modshogun``) library
24+
is available, the LMNN implementation will use the fast C++ version from
25+
there. The two implementations differ slightly, and the C++ version is
26+
more complete.
27+
28+
29+
Quick start
30+
===========
31+
32+
This example loads the iris dataset, and evaluates a k-nearest neighbors
33+
algorithm on an embedding space learned with `NCA`.
34+
35+
>>> from metric_learn import NCA
36+
>>> from sklearn.datasets import load_iris
37+
>>> from sklearn.model_selection import cross_val_score
38+
>>> from sklearn.pipeline import make_pipeline
39+
>>>
40+
>>> X, y = load_iris(return_X_y=True)
41+
>>> clf = make_pipeline(NCA(), KNeighborsClassifier())
42+
>>> cross_val_score(clf, X, y)

doc/index.rst

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,31 @@ metric-learn: Metric Learning in Python
22
=======================================
33
|License| |PyPI version|
44

5-
Distance metrics are widely used in the machine learning literature.
6-
Traditionally, practicioners would choose a standard distance metric
7-
(Euclidean, City-Block, Cosine, etc.) using a priori knowledge of
8-
the domain.
9-
Distance metric learning (or simply, metric learning) is the sub-field of
10-
machine learning dedicated to automatically constructing optimal distance
11-
metrics.
12-
13-
This package contains efficient Python implementations of several popular
14-
metric learning algorithms.
5+
Welcome to metric-learn's documentation !
6+
-----------------------------------------
157

168
.. toctree::
17-
:caption: Algorithms
18-
:maxdepth: 1
19-
20-
metric_learn.covariance
21-
metric_learn.lmnn
22-
metric_learn.itml
23-
metric_learn.sdml
24-
metric_learn.lsml
25-
metric_learn.nca
26-
metric_learn.lfda
27-
metric_learn.rca
28-
29-
Each metric supports the following methods:
30-
31-
- ``fit(...)``, which learns the model.
32-
- ``transformer()``, which returns a transformation matrix
33-
:math:`L \in \mathbb{R}^{D \times d}`, which can be used to convert a
34-
data matrix :math:`X \in \mathbb{R}^{n \times d}` to the
35-
:math:`D`-dimensional learned metric space :math:`X L^{\top}`,
36-
in which standard Euclidean distances may be used.
37-
- ``transform(X)``, which applies the aforementioned transformation.
38-
- ``metric()``, which returns a Mahalanobis matrix
39-
:math:`M = L^{\top}L` such that distance between vectors ``x`` and
40-
``y`` can be computed as :math:`\left(x-y\right)M\left(x-y\right)`.
9+
:maxdepth: 2
4110

11+
getting_started
4212

43-
Installation and Setup
44-
======================
45-
46-
Run ``pip install metric-learn`` to download and install from PyPI.
47-
48-
Alternately, download the source repository and run:
49-
50-
- ``python setup.py install`` for default installation.
51-
- ``python setup.py test`` to run all tests.
13+
.. toctree::
14+
:maxdepth: 2
5215

53-
**Dependencies**
16+
user_guide
5417

55-
- Python 2.7+, 3.4+
56-
- numpy, scipy, scikit-learn
57-
- (for running the examples only: matplotlib)
18+
.. toctree::
19+
:maxdepth: 2
5820

59-
**Notes**
21+
Package Overview <metric_learn>
6022

61-
If a recent version of the Shogun Python modular (``modshogun``) library
62-
is available, the LMNN implementation will use the fast C++ version from
63-
there. The two implementations differ slightly, and the C++ version is
64-
more complete.
23+
.. toctree::
24+
:maxdepth: 2
6525

66-
Navigation
67-
----------
26+
auto_examples/index
6827

6928
:ref:`genindex` | :ref:`modindex` | :ref:`search`
7029

71-
.. toctree::
72-
:maxdepth: 4
73-
:hidden:
74-
75-
Package Overview <metric_learn>
76-
7730
.. |PyPI version| image:: https://badge.fury.io/py/metric-learn.svg
7831
:target: http://badge.fury.io/py/metric-learn
7932
.. |License| image:: http://img.shields.io/:license-mit-blue.svg?style=flat

doc/introduction.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
============
2+
Introduction
3+
============
4+
5+
Distance metrics are widely used in the machine learning literature.
6+
Traditionally, practitioners would choose a standard distance metric
7+
(Euclidean, City-Block, Cosine, etc.) using a priori knowledge of
8+
the domain.
9+
Distance metric learning (or simply, metric learning) is the sub-field of
10+
machine learning dedicated to automatically construct task-specific distance
11+
metrics from (weakly) supervised data.
12+
The learned distance metric often corresponds to a Euclidean distance in a new
13+
embedding space, hence distance metric learning can be seen as a form of
14+
representation learning.
15+
16+
This package contains a efficient Python implementations of several popular
17+
metric learning algorithms, compatible with scikit-learn. This allows to use
18+
all the scikit-learn routines for pipelining and model selection for
19+
metric learning algorithms.
20+
21+
22+
Currently, each metric learning algorithm supports the following methods:
23+
24+
- ``fit(...)``, which learns the model.
25+
- ``metric()``, which returns a Mahalanobis matrix
26+
:math:`M = L^{\top}L` such that distance between vectors ``x`` and
27+
``y`` can be computed as :math:`\left(x-y\right)M\left(x-y\right)`.
28+
- ``transformer_from_metric(metric)``, which returns a transformation matrix
29+
:math:`L \in \mathbb{R}^{D \times d}`, which can be used to convert a
30+
data matrix :math:`X \in \mathbb{R}^{n \times d}` to the
31+
:math:`D`-dimensional learned metric space :math:`X L^{\top}`,
32+
in which standard Euclidean distances may be used.
33+
- ``transform(X)``, which applies the aforementioned transformation.
34+
- ``score_pairs(pairs)`` which returns the distance between pairs of
35+
points. ``pairs`` should be a 3D array-like of pairs of shape ``(n_pairs,
36+
2, n_features)``, or it can be a 2D array-like of pairs indicators of
37+
shape ``(n_pairs, 2)`` (see section :ref:`preprocessor_section` for more
38+
details).

doc/metric_learn.covariance.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Covariance metric (baseline method)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.itml.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Information Theoretic Metric Learning (ITML)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.lfda.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Local Fisher Discriminant Analysis (LFDA)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.lmnn.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Large Margin Nearest Neighbor (LMNN)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.lsml.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Least Squares Metric Learning (LSML)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.mlkr.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Metric Learning for Kernel Regression (MLKR)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.mmc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Mahalanobis Metric Learning for Clustering (MMC)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.nca.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Neighborhood Components Analysis (NCA)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.rca.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Relative Components Analysis (RCA)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

doc/metric_learn.rst

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
metric_learn package
22
====================
33

4-
Submodules
5-
----------
4+
Module Contents
5+
---------------
66

77
.. toctree::
88

@@ -16,11 +16,3 @@ Submodules
1616
metric_learn.nca
1717
metric_learn.rca
1818
metric_learn.sdml
19-
20-
Module contents
21-
---------------
22-
23-
.. automodule:: metric_learn
24-
:members:
25-
:undoc-members:
26-
:show-inheritance:

doc/metric_learn.sdml.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Sparse Determinant Metric Learning (SDML)
66
:undoc-members:
77
:inherited-members:
88
:show-inheritance:
9+
:special-members: __init__
910

1011
Example Code
1112
------------

0 commit comments

Comments
 (0)