Skip to content

Commit 1c5d001

Browse files
author
William de Vazelhes
committed
Update gh-pages
1 parent ef4efe3 commit 1c5d001

File tree

66 files changed

+631
-817
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+631
-817
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: 89e7bf0528b77a77d89d8167f4184500
3+
config: 7a311e9c0e4adf6a4015559eaf8b21ea
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

_downloads/47d7e96fc5ed57a08669b70c0daf7f4f/plot_metric_learning_examples.py renamed to _downloads/1701602fecf66f059913dd6559226da0/plot_metric_learning_examples.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
8888
# distances between points for the task at hand. Especially in higher
8989
# dimensions when Euclidean distances are a poor way to measure distance, this
9090
# becomes very useful.
91-
#
91+
#
9292
# Basically, we learn this distance:
9393
# :math:`D(x, x') = \sqrt{(x-x')^\top M(x-x')}`. And we learn the parameters
9494
# :math:`M` of this distance to satisfy certain constraints on the distance
@@ -113,12 +113,12 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
113113
######################################################################
114114
# Large Margin Nearest Neighbour
115115
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116-
#
116+
#
117117
# LMNN is a metric learning algorithm primarily designed for k-nearest
118118
# neighbor classification. The algorithm is based on semidefinite
119119
# programming, a sub-class of convex programming (as most Metric Learning
120120
# algorithms are).
121-
#
121+
#
122122
# The main intuition behind LMNN is to learn a pseudometric under which
123123
# all data instances in the training set are surrounded by at least k
124124
# instances that share the same class label. If this is achieved, the
@@ -136,7 +136,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
136136
######################################################################
137137
# Fit and then transform!
138138
# -----------------------
139-
#
139+
#
140140

141141
# setting up LMNN
142142
lmnn = metric_learn.LMNN(k=5, learn_rate=1e-6)
@@ -162,7 +162,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
162162

163163
######################################################################
164164
# Pretty neat, huh?
165-
#
165+
#
166166
# The rest of this notebook will briefly explain the other Metric Learning
167167
# algorithms before plotting them. Also, while we have first run ``fit``
168168
# and then ``transform`` to see our data transformed, we can also use
@@ -172,7 +172,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
172172
######################################################################
173173
# Information Theoretic Metric Learning
174174
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
175-
#
175+
#
176176
# ITML uses a regularizer that automatically enforces a Semi-Definite
177177
# Positive Matrix condition - the LogDet divergence. It uses soft
178178
# must-link or cannot like constraints, and a simple algorithm based on
@@ -231,7 +231,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
231231
######################################################################
232232
# Least Squares Metric Learning
233233
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
234-
#
234+
#
235235
# LSML is a simple, yet effective, algorithm that learns a Mahalanobis
236236
# metric from a given set of relative comparisons. This is done by
237237
# formulating and minimizing a convex loss function that corresponds to
@@ -277,7 +277,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
277277
######################################################################
278278
# Local Fisher Discriminant Analysis
279279
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
280-
#
280+
#
281281
# LFDA is a linear supervised dimensionality reduction method. It is
282282
# particularly useful when dealing with multimodality, where one ore more
283283
# classes consist of separate clusters in input space. The core
@@ -298,7 +298,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
298298
######################################################################
299299
# Relative Components Analysis
300300
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301-
#
301+
#
302302
# RCA is another one of the older algorithms. It learns a full rank
303303
# Mahalanobis distance metric based on a weighted sum of in-class
304304
# covariance matrices. It applies a global linear transformation to assign
@@ -402,7 +402,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
402402
def create_constraints(labels):
403403
import itertools
404404
import random
405-
405+
406406
# aggregate indices of same class
407407
zeros = np.where(y == 0)[0]
408408
ones = np.where(y == 1)[0]
@@ -413,7 +413,7 @@ def create_constraints(labels):
413413
twos_ = list(itertools.combinations(twos, 2))
414414
# put them together!
415415
sim = np.array(zeros_ + ones_ + twos_)
416-
416+
417417
# similarily, put together indices in different classes
418418
dis = []
419419
for zero in zeros:
@@ -424,21 +424,25 @@ def create_constraints(labels):
424424
for one in ones:
425425
for two in twos:
426426
dis.append((one, two))
427-
427+
428428
# pick up just enough dissimilar examples as we have similar examples
429429
dis = np.array(random.sample(dis, len(sim)))
430-
431-
# return an array of pairs of indices of shape=(2*len(sim), 2), and the corresponding labels, array of shape=(2*len(sim))
432-
# Each pair of similar points have a label of +1 and each pair of dissimilar points have a label of -1
433-
return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]), np.column_stack([dis[:, 0], dis[:, 1]])]),
430+
431+
# return an array of pairs of indices of shape=(2*len(sim), 2), and the
432+
# corresponding labels, array of shape=(2*len(sim))
433+
# Each pair of similar points have a label of +1 and each pair of
434+
# dissimilar points have a label of -1
435+
return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]),
436+
np.column_stack([dis[:, 0], dis[:, 1]])]),
434437
np.concatenate([np.ones(len(sim)), -np.ones(len(sim))]))
435438

439+
436440
pairs, pairs_labels = create_constraints(y)
437441

438442

439443
######################################################################
440444
# Now that we've created our constraints, let's see what it looks like!
441-
#
445+
#
442446

443447
print(pairs)
444448
print(pairs_labels)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\nSandwich demo\n=============\n\nSandwich demo based on code from http://nbviewer.ipython.org/6576096\n\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"import numpy as np\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import pairwise_distances\nfrom sklearn.neighbors import NearestNeighbors\n\nfrom metric_learn import (LMNN, ITML_Supervised, LSML_Supervised,\n SDML_Supervised)\n\n\ndef sandwich_demo():\n x, y = sandwich_data()\n knn = nearest_neighbors(x, k=2)\n ax = plt.subplot(3, 1, 1) # take the whole top row\n plot_sandwich_data(x, y, ax)\n plot_neighborhood_graph(x, knn, y, ax)\n ax.set_title('input space')\n ax.set_aspect('equal')\n ax.set_xticks([])\n ax.set_yticks([])\n\n mls = [\n LMNN(),\n ITML_Supervised(num_constraints=200),\n SDML_Supervised(num_constraints=200, balance_param=0.001),\n LSML_Supervised(num_constraints=200),\n ]\n\n for ax_num, ml in enumerate(mls, start=3):\n ml.fit(x, y)\n tx = ml.transform(x)\n ml_knn = nearest_neighbors(tx, k=2)\n ax = plt.subplot(3, 2, ax_num)\n plot_sandwich_data(tx, y, axis=ax)\n plot_neighborhood_graph(tx, ml_knn, y, axis=ax)\n ax.set_title(ml.__class__.__name__)\n ax.set_xticks([])\n ax.set_yticks([])\n plt.show()\n\n\n# TODO: use this somewhere\ndef visualize_class_separation(X, labels):\n _, (ax1, ax2) = plt.subplots(ncols=2)\n label_order = np.argsort(labels)\n ax1.imshow(pairwise_distances(X[label_order]), interpolation='nearest')\n ax2.imshow(pairwise_distances(labels[label_order, None]),\n interpolation='nearest')\n\n\ndef nearest_neighbors(X, k=5):\n knn = NearestNeighbors(n_neighbors=k)\n knn.fit(X)\n return knn.kneighbors(X, return_distance=False)\n\n\ndef sandwich_data():\n # number of distinct classes\n num_classes = 6\n # number of points per class\n num_points = 9\n # distance between layers, the points of each class are in a layer\n dist = 0.7\n\n data = np.zeros((num_classes, num_points, 2), dtype=float)\n labels = np.zeros((num_classes, num_points), dtype=int)\n\n x_centers = np.arange(num_points, dtype=float) - num_points / 2\n y_centers = dist * (np.arange(num_classes, dtype=float) - num_classes / 2)\n for i, yc in enumerate(y_centers):\n for k, xc in enumerate(x_centers):\n data[i, k, 0] = np.random.normal(xc, 0.1)\n data[i, k, 1] = np.random.normal(yc, 0.1)\n labels[i, :] = i\n return data.reshape((-1, 2)), labels.ravel()\n\n\ndef plot_sandwich_data(x, y, axis=plt, colors='rbgmky'):\n for idx, val in enumerate(np.unique(y)):\n xi = x[y == val]\n axis.scatter(*xi.T, s=50, facecolors='none', edgecolors=colors[idx])\n\n\ndef plot_neighborhood_graph(x, nn, y, axis=plt, colors='rbgmky'):\n for i, a in enumerate(x):\n b = x[nn[i, 1]]\n axis.plot((a[0], b[0]), (a[1], b[1]), colors[y[i]])\n\n\nif __name__ == '__main__':\n sandwich_demo()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.7.1"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}

_downloads/c3c89065d16de63f152d556f31fd7ee6/plot_metric_learning_examples.ipynb renamed to _downloads/434a24c66bbfd5f8398470a8c859e27b/plot_metric_learning_examples.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
},
368368
"outputs": [],
369369
"source": [
370-
"def create_constraints(labels):\n import itertools\n import random\n \n # aggregate indices of same class\n zeros = np.where(y == 0)[0]\n ones = np.where(y == 1)[0]\n twos = np.where(y == 2)[0]\n # make permutations of all those points in the same class\n zeros_ = list(itertools.combinations(zeros, 2))\n ones_ = list(itertools.combinations(ones, 2))\n twos_ = list(itertools.combinations(twos, 2))\n # put them together!\n sim = np.array(zeros_ + ones_ + twos_)\n \n # similarily, put together indices in different classes\n dis = []\n for zero in zeros:\n for one in ones:\n dis.append((zero, one))\n for two in twos:\n dis.append((zero, two))\n for one in ones:\n for two in twos:\n dis.append((one, two))\n \n # pick up just enough dissimilar examples as we have similar examples\n dis = np.array(random.sample(dis, len(sim)))\n \n # return an array of pairs of indices of shape=(2*len(sim), 2), and the corresponding labels, array of shape=(2*len(sim))\n #\u00a0Each pair of similar points have a label of +1 and each pair of dissimilar points have a label of -1\n return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]), np.column_stack([dis[:, 0], dis[:, 1]])]),\n np.concatenate([np.ones(len(sim)), -np.ones(len(sim))]))\n\npairs, pairs_labels = create_constraints(y)"
370+
"def create_constraints(labels):\n import itertools\n import random\n\n # aggregate indices of same class\n zeros = np.where(y == 0)[0]\n ones = np.where(y == 1)[0]\n twos = np.where(y == 2)[0]\n # make permutations of all those points in the same class\n zeros_ = list(itertools.combinations(zeros, 2))\n ones_ = list(itertools.combinations(ones, 2))\n twos_ = list(itertools.combinations(twos, 2))\n # put them together!\n sim = np.array(zeros_ + ones_ + twos_)\n\n # similarily, put together indices in different classes\n dis = []\n for zero in zeros:\n for one in ones:\n dis.append((zero, one))\n for two in twos:\n dis.append((zero, two))\n for one in ones:\n for two in twos:\n dis.append((one, two))\n\n # pick up just enough dissimilar examples as we have similar examples\n dis = np.array(random.sample(dis, len(sim)))\n\n # return an array of pairs of indices of shape=(2*len(sim), 2), and the\n # corresponding labels, array of shape=(2*len(sim))\n # Each pair of similar points have a label of +1 and each pair of\n # dissimilar points have a label of -1\n return (np.vstack([np.column_stack([sim[:, 0], sim[:, 1]]),\n np.column_stack([dis[:, 0], dis[:, 1]])]),\n np.concatenate([np.ones(len(sim)), -np.ones(len(sim))]))\n\n\npairs, pairs_labels = create_constraints(y)"
371371
]
372372
},
373373
{

_downloads/1e6862ea24bc862357b6b17e58c7e812/plot_sandwich.py renamed to _downloads/5632643bbb0183e351bf2b4dc2ab7c39/plot_sandwich.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from sklearn.metrics import pairwise_distances
1212
from sklearn.neighbors import NearestNeighbors
1313

14-
from metric_learn import LMNN, ITML_Supervised, LSML_Supervised, SDML_Supervised
14+
from metric_learn import (LMNN, ITML_Supervised, LSML_Supervised,
15+
SDML_Supervised)
1516

1617

1718
def sandwich_demo():
@@ -47,10 +48,10 @@ def sandwich_demo():
4748

4849
# TODO: use this somewhere
4950
def visualize_class_separation(X, labels):
50-
_, (ax1,ax2) = plt.subplots(ncols=2)
51+
_, (ax1, ax2) = plt.subplots(ncols=2)
5152
label_order = np.argsort(labels)
5253
ax1.imshow(pairwise_distances(X[label_order]), interpolation='nearest')
53-
ax2.imshow(pairwise_distances(labels[label_order,None]),
54+
ax2.imshow(pairwise_distances(labels[label_order, None]),
5455
interpolation='nearest')
5556

5657

@@ -77,19 +78,19 @@ def sandwich_data():
7778
for k, xc in enumerate(x_centers):
7879
data[i, k, 0] = np.random.normal(xc, 0.1)
7980
data[i, k, 1] = np.random.normal(yc, 0.1)
80-
labels[i,:] = i
81+
labels[i, :] = i
8182
return data.reshape((-1, 2)), labels.ravel()
8283

8384

8485
def plot_sandwich_data(x, y, axis=plt, colors='rbgmky'):
8586
for idx, val in enumerate(np.unique(y)):
86-
xi = x[y==val]
87+
xi = x[y == val]
8788
axis.scatter(*xi.T, s=50, facecolors='none', edgecolors=colors[idx])
8889

8990

9091
def plot_neighborhood_graph(x, nn, y, axis=plt, colors='rbgmky'):
9192
for i, a in enumerate(x):
92-
b = x[nn[i,1]]
93+
b = x[nn[i, 1]]
9394
axis.plot((a[0], b[0]), (a[1], b[1]), colors[y[i]])
9495

9596

_downloads/a271e21261676d095ce71b98d99a82c3/plot_sandwich.ipynb

Lines changed: 0 additions & 54 deletions
This file was deleted.
Loading
Loading
-7.29 KB
Loading
-4.67 KB
Loading

_modules/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ <h1>All modules for which code is available</h1>
178178

179179
<div role="contentinfo">
180180
<p>
181-
&copy; Copyright 2015-2019, CJ Carey, Yuan Tang, William de Vazelhes, Aurélien Bellet, and Nathalie Vauquier
181+
&copy; Copyright 2015-2019, CJ Carey, Yuan Tang, William de Vazelhes, Aurélien Bellet and Nathalie Vauquier
182182

183183
</p>
184184
</div>

0 commit comments

Comments
 (0)