Skip to content

Commit

Permalink
update doc for 0.7.0 (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdevazelhes authored Oct 9, 2023
1 parent 8f8b1f7 commit f034b25
Show file tree
Hide file tree
Showing 183 changed files with 14,371 additions and 29,773 deletions.
Binary file not shown.
16 changes: 6 additions & 10 deletions _downloads/4f011433714ef22e0c66be75bbfd08e3/plot_sandwich.ipynb
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"%matplotlib inline"
"\n# Sandwich demo\n\nSandwich demo based on code from http://nbviewer.ipython.org/6576096\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\nSandwich demo\n=============\n\nSandwich demo based on code from http://nbviewer.ipython.org/6576096\n"
"<div class=\"alert alert-info\"><h4>Note</h4><p>In order to show the charts of the examples you need a graphical\n ``matplotlib`` backend installed. For intance, use ``pip install pyqt5``\n to get Qt graphical interface or use your favorite one.</p></div>\n\n"
]
},
{
Expand All @@ -26,7 +22,7 @@
},
"outputs": [],
"source": [
"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()"
"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(n_constraints=200),\n SDML_Supervised(n_constraints=200, balance_param=0.001),\n LSML_Supervised(n_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()"
]
}
],
Expand All @@ -46,7 +42,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
Binary file not shown.
13 changes: 10 additions & 3 deletions _downloads/a6bf7d7136399675f28d77d001faa48f/plot_sandwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
Sandwich demo based on code from http://nbviewer.ipython.org/6576096
"""

######################################################################
# .. note::
#
# In order to show the charts of the examples you need a graphical
# ``matplotlib`` backend installed. For intance, use ``pip install pyqt5``
# to get Qt graphical interface or use your favorite one.

import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances
Expand All @@ -28,9 +35,9 @@ def sandwich_demo():

mls = [
LMNN(),
ITML_Supervised(num_constraints=200),
SDML_Supervised(num_constraints=200, balance_param=0.001),
LSML_Supervised(num_constraints=200),
ITML_Supervised(n_constraints=200),
SDML_Supervised(n_constraints=200, balance_param=0.001),
LSML_Supervised(n_constraints=200),
]

for ax_num, ml in enumerate(mls, start=3):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
######################################################################
# Imports
# ^^^^^^^
# .. note::
#
# In order to show the charts of the examples you need a graphical
# ``matplotlib`` backend installed. For intance, use ``pip install pyqt5``
# to get Qt graphical interface or use your favorite one.

from sklearn.manifold import TSNE

Expand All @@ -35,9 +39,9 @@
# We will be using a synthetic dataset to illustrate the plotting,
# using the function `sklearn.datasets.make_classification` from
# scikit-learn. The dataset will contain:
# - 100 points in 3 classes with 2 clusters per class
# - 5 features, among which 3 are informative (correlated with the class
# labels) and two are random noise with large magnitude
# - 100 points in 3 classes with 2 clusters per class
# - 5 features, among which 3 are informative (correlated with the class
# labels) and two are random noise with large magnitude

X, y = make_classification(n_samples=100, n_classes=3, n_clusters_per_class=2,
n_informative=3, class_sep=4., n_features=5,
Expand Down Expand Up @@ -139,7 +143,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
#

# setting up LMNN
lmnn = metric_learn.LMNN(k=5, learn_rate=1e-6)
lmnn = metric_learn.LMNN(n_neighbors=5, learn_rate=1e-6)

# fit the data!
lmnn.fit(X, y)
Expand Down Expand Up @@ -310,7 +314,7 @@ def plot_tsne(X, y, colormap=plt.cm.Paired):
# - See more in the documentation of the class :py:class:`RCA
# <metric_learn.RCA>`

rca = metric_learn.RCA_Supervised(num_chunks=30, chunk_size=2)
rca = metric_learn.RCA_Supervised(n_chunks=30, chunk_size=2)
X_rca = rca.fit_transform(X, y)

plot_tsne(X_rca, y)
Expand Down
Binary file not shown.
Loading

0 comments on commit f034b25

Please sign in to comment.