Note
Go to the end to download the full example code.
Wasserstein Discriminant Analysis
Note
Example added in release: 0.3.0.
This example illustrate the use of WDA as proposed in [11].
[11] Flamary, R., Cuturi, M., Courty, N., & Rakotomamonjy, A. (2016). Wasserstein Discriminant Analysis.
Generate data
n = 1000 # nb samples in source and target datasets
nz = 0.2
np.random.seed(1)
# generate circle dataset
t = np.random.rand(n) * 2 * np.pi
ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1
xs = np.concatenate((np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)
xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2)
t = np.random.rand(n) * 2 * np.pi
yt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1
xt = np.concatenate((np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)
xt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2)
nbnoise = 8
xs = np.hstack((xs, np.random.randn(n, nbnoise)))
xt = np.hstack((xt, np.random.randn(n, nbnoise)))
Plot data
pl.figure(1, figsize=(6.4, 3.5))
pl.subplot(1, 2, 1)
pl.scatter(xt[:, 0], xt[:, 1], c=ys, marker="+", label="Source samples")
pl.legend(loc=0)
pl.title("Discriminant dimensions")
pl.subplot(1, 2, 2)
pl.scatter(xt[:, 2], xt[:, 3], c=ys, marker="+", label="Source samples")
pl.legend(loc=0)
pl.title("Other dimensions")
pl.tight_layout()

Compute Fisher Discriminant Analysis
Compute Wasserstein Discriminant Analysis
Optimizing...
Iteration Cost Gradient norm
--------- ----------------------- --------------
1 +8.3042776946697483e-01 5.65147154e-01
2 +4.4401037686381051e-01 2.16760501e-01
3 +4.2234351238819923e-01 1.30555049e-01
4 +4.2169879996364512e-01 1.39115407e-01
5 +4.1924746118060841e-01 1.25387848e-01
6 +4.1177409528991332e-01 6.70993539e-02
7 +4.0862213476138881e-01 3.52716830e-02
8 +4.0747229322240458e-01 3.34923131e-02
9 +4.0678766065260497e-01 2.74029183e-02
10 +4.0621337155460591e-01 2.03651803e-02
11 +4.0577080390746972e-01 2.59605592e-02
12 +4.0543140912488929e-01 3.28883715e-02
13 +4.0470236926315090e-01 1.47528039e-02
14 +4.0445628466206923e-01 5.03183251e-02
15 +4.0364189451327920e-01 3.31006492e-02
16 +4.0303977567702015e-01 1.39885386e-02
17 +4.0301476219898225e-01 2.17467509e-02
18 +4.0292344215507014e-01 1.79959449e-02
19 +4.0271888266378736e-01 6.94409958e-03
20 +4.0183218086345140e-01 1.98335236e-02
21 +3.9762874238462542e-01 1.03191920e-01
22 +3.8226838815740732e-01 1.35965986e-01
23 +3.0858990514200146e-01 1.92704442e-01
24 +2.7992480824426968e-01 2.01780716e-01
25 +2.3706912526769566e-01 9.14845638e-02
26 +2.3383707512681917e-01 6.77029657e-02
27 +2.3061384974645791e-01 3.50827672e-03
28 +2.3060565162891331e-01 1.30758424e-03
29 +2.3060500411815468e-01 9.09652652e-04
30 +2.3060443826676186e-01 1.28425737e-04
31 +2.3060443086379259e-01 7.48382598e-05
32 +2.3060443053026455e-01 7.15023865e-05
33 +2.3060442932498662e-01 5.76927362e-05
34 +2.3060442708796106e-01 4.21443992e-06
35 +2.3060442707847920e-01 1.92929974e-06
36 +2.3060442707794709e-01 1.71282892e-06
37 +2.3060442707639084e-01 7.91907525e-07
Terminated - min grad norm reached after 37 iterations, 4.05 seconds.
Plot 2D projections
xsp = projfda(xs)
xtp = projfda(xt)
xspw = projwda(xs)
xtpw = projwda(xt)
pl.figure(2)
pl.subplot(2, 2, 1)
pl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker="+", label="Projected samples")
pl.legend(loc=0)
pl.title("Projected training samples FDA")
pl.subplot(2, 2, 2)
pl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker="+", label="Projected samples")
pl.legend(loc=0)
pl.title("Projected test samples FDA")
pl.subplot(2, 2, 3)
pl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker="+", label="Projected samples")
pl.legend(loc=0)
pl.title("Projected training samples WDA")
pl.subplot(2, 2, 4)
pl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker="+", label="Projected samples")
pl.legend(loc=0)
pl.title("Projected test samples WDA")
pl.tight_layout()
pl.show()

/home/circleci/.local/lib/python3.12/site-packages/matplotlib/cbook.py:1719: ComplexWarning: Casting complex values to real discards the imaginary part
return math.isfinite(val)
/home/circleci/.local/lib/python3.12/site-packages/matplotlib/collections.py:200: ComplexWarning: Casting complex values to real discards the imaginary part
offsets = np.asanyarray(offsets, float)
Total running time of the script: (0 minutes 4.631 seconds)