Debian Codegraph @

examples/domain-adaptation/plot_otda_classes.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ========================
4 OT for domain adaptation
5 ========================
6
7 This example introduces a domain adaptation in a 2D setting and the 4 OTDA
8 approaches currently supported in POT.
9
10 """
11
12 # Authors: Remi Flamary <[email protected]>
13 # Stanislas Chambon <[email protected]>
14 #
15 # License: MIT License
16
17 import matplotlib.pylab as pl
18 import ot
19
20 ##############################################################################
21 # Generate data
22 # -------------
23
24 n_source_samples = 150
25 n_target_samples = 150
26
27 Xs, ys = ot.datasets.make_data_classif("3gauss", n_source_samples)
28 Xt, yt = ot.datasets.make_data_classif("3gauss2", n_target_samples)
29
30
31 ##############################################################################
32 # Instantiate the different transport algorithms and fit them
33 # -----------------------------------------------------------
34
35 # EMD Transport
36 ot_emd = ot.da.EMDTransport()
37 ot_emd.fit(Xs=Xs, Xt=Xt)
38
39 # Sinkhorn Transport
40 ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)
41 ot_sinkhorn.fit(Xs=Xs, Xt=Xt)
42
43 # Sinkhorn Transport with Group lasso regularization
44 ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0)
45 ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt)
46
47 # Sinkhorn Transport with Group lasso regularization l1l2
48 ot_l1l2 = ot.da.SinkhornL1l2Transport(reg_e=1e-1, reg_cl=2e0, max_iter=20, verbose=True)
49 ot_l1l2.fit(Xs=Xs, ys=ys, Xt=Xt)
50
51 # transport source samples onto target samples
52 transp_Xs_emd = ot_emd.transform(Xs=Xs)
53 transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs)
54 transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs)
55 transp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs)
56
57
58 ##############################################################################
59 # Fig 1 : plots source and target samples
60 # ---------------------------------------
61
62 pl.figure(1, figsize=(10, 5))
63 pl.subplot(1, 2, 1)
64 pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples")
65 pl.xticks([])
66 pl.yticks([])
67 pl.legend(loc=0)
68 pl.title("Source samples")
69
70 pl.subplot(1, 2, 2)
71 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples")
72 pl.xticks([])
73 pl.yticks([])
74 pl.legend(loc=0)
75 pl.title("Target samples")
76 pl.tight_layout()
77
78
79 ##############################################################################
80 # Fig 2 : plot optimal couplings and transported samples
81 # ------------------------------------------------------
82
83 param_img = {"interpolation": "nearest"}
84
85 pl.figure(2, figsize=(15, 8))
86 pl.subplot(2, 4, 1)
87 pl.imshow(ot_emd.coupling_, **param_img)
88 pl.xticks([])
89 pl.yticks([])
90 pl.title("Optimal coupling\nEMDTransport")
91
92 pl.subplot(2, 4, 2)
93 pl.imshow(ot_sinkhorn.coupling_, **param_img)
94 pl.xticks([])
95 pl.yticks([])
96 pl.title("Optimal coupling\nSinkhornTransport")
97
98 pl.subplot(2, 4, 3)
99 pl.imshow(ot_lpl1.coupling_, **param_img)
100 pl.xticks([])
101 pl.yticks([])
102 pl.title("Optimal coupling\nSinkhornLpl1Transport")
103
104 pl.subplot(2, 4, 4)
105 pl.imshow(ot_l1l2.coupling_, **param_img)
106 pl.xticks([])
107 pl.yticks([])
108 pl.title("Optimal coupling\nSinkhornL1l2Transport")
109
110 pl.subplot(2, 4, 5)
111 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3)
112 pl.scatter(
113 transp_Xs_emd[:, 0],
114 transp_Xs_emd[:, 1],
115 c=ys,
116 marker="+",
117 label="Transp samples",
118 s=30,
119 )
120 pl.xticks([])
121 pl.yticks([])
122 pl.title("Transported samples\nEmdTransport")
123 pl.legend(loc="lower left")
124
125 pl.subplot(2, 4, 6)
126 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3)
127 pl.scatter(
128 transp_Xs_sinkhorn[:, 0],
129 transp_Xs_sinkhorn[:, 1],
130 c=ys,
131 marker="+",
132 label="Transp samples",
133 s=30,
134 )
135 pl.xticks([])
136 pl.yticks([])
137 pl.title("Transported samples\nSinkhornTransport")
138
139 pl.subplot(2, 4, 7)
140 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3)
141 pl.scatter(
142 transp_Xs_lpl1[:, 0],
143 transp_Xs_lpl1[:, 1],
144 c=ys,
145 marker="+",
146 label="Transp samples",
147 s=30,
148 )
149 pl.xticks([])
150 pl.yticks([])
151 pl.title("Transported samples\nSinkhornLpl1Transport")
152
153 pl.subplot(2, 4, 8)
154 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3)
155 pl.scatter(
156 transp_Xs_l1l2[:, 0],
157 transp_Xs_l1l2[:, 1],
158 c=ys,
159 marker="+",
160 label="Transp samples",
161 s=30,
162 )
163 pl.xticks([])
164 pl.yticks([])
165 pl.title("Transported samples\nSinkhornL1l2Transport")
166 pl.tight_layout()
167
168 pl.show()