summaryrefslogtreecommitdiff
path: root/examples/plot_OTDA_classes.py
blob: 4d3846aed12a97a3c8ccc81a68582085fff71cad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""
========================
OT for domain adaptation
========================

"""

import matplotlib.pylab as pl
import ot


#%% parameters

n = 150  # nb samples in source and target datasets

xs, ys = ot.datasets.get_data_classif('3gauss', n)
xt, yt = ot.datasets.get_data_classif('3gauss2', n)


#%% plot samples

pl.figure(1, figsize=(6.4, 3))

pl.subplot(1, 2, 1)
pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
pl.legend(loc=0)
pl.title('Source  distributions')

pl.subplot(1, 2, 2)
pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
pl.legend(loc=0)
pl.title('target  distributions')


#%% OT estimation

# LP problem
da_emd = ot.da.OTDA()     # init class
da_emd.fit(xs, xt)       # fit distributions
xst0 = da_emd.interp()    # interpolation of source samples

# sinkhorn regularization
lambd = 1e-1
da_entrop = ot.da.OTDA_sinkhorn()
da_entrop.fit(xs, xt, reg=lambd)
xsts = da_entrop.interp()

# non-convex Group lasso regularization
reg = 1e-1
eta = 1e0
da_lpl1 = ot.da.OTDA_lpl1()
da_lpl1.fit(xs, ys, xt, reg=reg, eta=eta)
xstg = da_lpl1.interp()

# True Group lasso regularization
reg = 1e-1
eta = 2e0
da_l1l2 = ot.da.OTDA_l1l2()
da_l1l2.fit(xs, ys, xt, reg=reg, eta=eta, numItermax=20, verbose=True)
xstgl = da_l1l2.interp()

#%% plot interpolated source samples

param_img = {'interpolation': 'nearest', 'cmap': 'spectral'}

pl.figure(2, figsize=(8, 4.5))
pl.subplot(2, 4, 1)
pl.imshow(da_emd.G, **param_img)
pl.title('OT matrix')

pl.subplot(2, 4, 2)
pl.imshow(da_entrop.G, **param_img)
pl.title('OT matrix\nsinkhorn')

pl.subplot(2, 4, 3)
pl.imshow(da_lpl1.G, **param_img)
pl.title('OT matrix\nnon-convex Group Lasso')

pl.subplot(2, 4, 4)
pl.imshow(da_l1l2.G, **param_img)
pl.title('OT matrix\nGroup Lasso')

pl.subplot(2, 4, 5)
pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
           label='Target samples', alpha=0.3)
pl.scatter(xst0[:, 0], xst0[:, 1], c=ys,
           marker='+', label='Transp samples', s=30)
pl.title('Interp samples')
pl.legend(loc=0)

pl.subplot(2, 4, 6)
pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
           label='Target samples', alpha=0.3)
pl.scatter(xsts[:, 0], xsts[:, 1], c=ys,
           marker='+', label='Transp samples', s=30)
pl.title('Interp samples\nSinkhorn')

pl.subplot(2, 4, 7)
pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
           label='Target samples', alpha=0.3)
pl.scatter(xstg[:, 0], xstg[:, 1], c=ys,
           marker='+', label='Transp samples', s=30)
pl.title('Interp samples\nnon-convex Group Lasso')

pl.subplot(2, 4, 8)
pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
           label='Target samples', alpha=0.3)
pl.scatter(xstgl[:, 0], xstgl[:, 1], c=ys,
           marker='+', label='Transp samples', s=30)
pl.title('Interp samples\nGroup Lasso')
pl.tight_layout()
pl.show()