Note
Go to the end to download the full example code.
Nyström approximation for OT
Shows how to use Nyström kernel approximation for approximating the Sinkhorn algorithm in linear time.
# Author: Titouan Vayer <titouan.vayer@inria.fr>
#
# License: MIT License
# sphinx_gallery_thumbnail_number = 2
import numpy as np
from ot.lowrank import kernel_nystroem, sinkhorn_low_rank_kernel
from ot.bregman import empirical_sinkhorn_nystroem
import math
import ot
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
Generate data
offset = 1
n_samples_per_blob = 500 # We use 2D ''blobs'' data
random_state = 42
std = 0.2 # standard deviation
np.random.seed(random_state)
centers = np.array(
[
[-offset, -offset], # Class 0 - blob 1
[-offset, offset], # Class 0 - blob 2
[offset, -offset], # Class 1 - blob 1
[offset, offset], # Class 1 - blob 2
]
)
X_list = []
y_list = []
for i, center in enumerate(centers):
blob_points = np.random.randn(n_samples_per_blob, 2) * std + center
label = 0 if i < 2 else 1
X_list.append(blob_points)
y_list.append(np.full(n_samples_per_blob, label))
X = np.vstack(X_list)
y = np.concatenate(y_list)
Xs = X[y == 0] # source data
Xt = X[y == 1] # target data
Plot data
plt.scatter(Xs[:, 0], Xs[:, 1], label="Source")
plt.scatter(Xt[:, 0], Xt[:, 1], label="Target")
plt.legend()