1111import ot
1212
1313
14-
1514#%% parameters
1615
17- n = 150 # nb bins
16+ n = 150 # nb bins
1817
19- xs ,ys = ot .datasets .get_data_classif ('3gauss' ,n )
20- xt ,yt = ot .datasets .get_data_classif ('3gauss2' ,n )
18+ xs , ys = ot .datasets .get_data_classif ('3gauss' , n )
19+ xt , yt = ot .datasets .get_data_classif ('3gauss2' , n )
2120
22- a ,b = ot .unif (n ),ot .unif (n )
21+ a , b = ot .unif (n ), ot .unif (n )
2322# loss matrix
24- M = ot .dist (xs ,xt )
25- #M/=M.max()
23+ M = ot .dist (xs , xt )
24+ # M/=M.max()
2625
2726#%% plot samples
2827
2928pl .figure (1 )
30-
31- pl .subplot (2 ,2 ,1 )
32- pl .scatter (xs [:,0 ],xs [:,1 ],c = ys ,marker = '+' ,label = 'Source samples' )
29+ pl .subplot (2 , 2 , 1 )
30+ pl .scatter (xs [:, 0 ], xs [:, 1 ], c = ys , marker = '+' , label = 'Source samples' )
3331pl .legend (loc = 0 )
3432pl .title ('Source distributions' )
3533
36- pl .subplot (2 ,2 , 2 )
37- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' )
34+ pl .subplot (2 , 2 , 2 )
35+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' , label = 'Target samples' )
3836pl .legend (loc = 0 )
3937pl .title ('target distributions' )
4038
4139pl .figure (2 )
42- pl .imshow (M ,interpolation = 'nearest' )
40+ pl .imshow (M , interpolation = 'nearest' )
4341pl .title ('Cost matrix M' )
4442
4543
4644#%% OT estimation
4745
4846# EMD
49- G0 = ot .emd (a ,b , M )
47+ G0 = ot .emd (a , b , M )
5048
5149# sinkhorn
52- lambd = 1e-1
53- Gs = ot .sinkhorn (a ,b , M , lambd )
50+ lambd = 1e-1
51+ Gs = ot .sinkhorn (a , b , M , lambd )
5452
5553
5654# Group lasso regularization
57- reg = 1e-1
58- eta = 1e0
59- Gg = ot .da .sinkhorn_lpl1_mm (a ,ys .astype (np .int ),b , M , reg ,eta )
55+ reg = 1e-1
56+ eta = 1e0
57+ Gg = ot .da .sinkhorn_lpl1_mm (a , ys .astype (np .int ), b , M , reg , eta )
6058
6159
6260#%% visu matrices
6361
6462pl .figure (3 )
6563
66- pl .subplot (2 ,3 , 1 )
67- pl .imshow (G0 ,interpolation = 'nearest' )
64+ pl .subplot (2 , 3 , 1 )
65+ pl .imshow (G0 , interpolation = 'nearest' )
6866pl .title ('OT matrix ' )
6967
70- pl .subplot (2 ,3 , 2 )
71- pl .imshow (Gs ,interpolation = 'nearest' )
68+ pl .subplot (2 , 3 , 2 )
69+ pl .imshow (Gs , interpolation = 'nearest' )
7270pl .title ('OT matrix Sinkhorn' )
7371
74- pl .subplot (2 ,3 , 3 )
75- pl .imshow (Gg ,interpolation = 'nearest' )
72+ pl .subplot (2 , 3 , 3 )
73+ pl .imshow (Gg , interpolation = 'nearest' )
7674pl .title ('OT matrix Group lasso' )
7775
78- pl .subplot (2 ,3 , 4 )
79- ot .plot .plot2D_samples_mat (xs ,xt ,G0 ,c = [.5 ,.5 ,1 ])
80- pl .scatter (xs [:,0 ],xs [:,1 ],c = ys ,marker = '+' ,label = 'Source samples' )
81- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' )
76+ pl .subplot (2 , 3 , 4 )
77+ ot .plot .plot2D_samples_mat (xs , xt , G0 , c = [.5 , .5 , 1 ])
78+ pl .scatter (xs [:, 0 ], xs [:, 1 ], c = ys , marker = '+' , label = 'Source samples' )
79+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' , label = 'Target samples' )
8280
8381
84- pl .subplot (2 ,3 , 5 )
85- ot .plot .plot2D_samples_mat (xs ,xt ,Gs ,c = [.5 ,.5 ,1 ])
86- pl .scatter (xs [:,0 ],xs [:,1 ],c = ys ,marker = '+' ,label = 'Source samples' )
87- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' )
82+ pl .subplot (2 , 3 , 5 )
83+ ot .plot .plot2D_samples_mat (xs , xt , Gs , c = [.5 , .5 , 1 ])
84+ pl .scatter (xs [:, 0 ], xs [:, 1 ], c = ys , marker = '+' , label = 'Source samples' )
85+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' , label = 'Target samples' )
8886
89- pl .subplot (2 ,3 ,6 )
90- ot .plot .plot2D_samples_mat (xs ,xt ,Gg ,c = [.5 ,.5 ,1 ])
91- pl .scatter (xs [:,0 ],xs [:,1 ],c = ys ,marker = '+' ,label = 'Source samples' )
92- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' )
87+ pl .subplot (2 , 3 , 6 )
88+ ot .plot .plot2D_samples_mat (xs , xt , Gg , c = [.5 , .5 , 1 ])
89+ pl .scatter (xs [:, 0 ], xs [:, 1 ], c = ys , marker = '+' , label = 'Source samples' )
90+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' , label = 'Target samples' )
91+ pl .tight_layout ()
9392
9493#%% sample interpolation
9594
96- xst0 = n * G0 .dot (xt )
97- xsts = n * Gs .dot (xt )
98- xstg = n * Gg .dot (xt )
99-
100- pl .figure (4 )
101- pl .subplot (2 ,3 ,1 )
102-
95+ xst0 = n * G0 .dot (xt )
96+ xsts = n * Gs .dot (xt )
97+ xstg = n * Gg .dot (xt )
10398
104- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' ,alpha = 0.5 )
105- pl .scatter (xst0 [:,0 ],xst0 [:,1 ],c = ys ,marker = '+' ,label = 'Transp samples' ,s = 30 )
99+ pl .figure (4 , figsize = (8 , 3 ))
100+ pl .subplot (1 , 3 , 1 )
101+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' ,
102+ label = 'Target samples' , alpha = 0.5 )
103+ pl .scatter (xst0 [:, 0 ], xst0 [:, 1 ], c = ys ,
104+ marker = '+' , label = 'Transp samples' , s = 30 )
106105pl .title ('Interp samples' )
107106pl .legend (loc = 0 )
108107
109- pl .subplot (2 , 3 , 2 )
110-
111-
112- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt , marker = 'o' , label = 'Target samples' , alpha = 0.5 )
113- pl . scatter ( xsts [:, 0 ], xsts [:, 1 ], c = ys , marker = '+' ,label = 'Transp samples' ,s = 30 )
108+ pl .subplot (1 , 3 , 2 )
109+ pl . scatter ( xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' ,
110+ label = 'Target samples' , alpha = 0.5 )
111+ pl .scatter (xsts [:, 0 ], xsts [:, 1 ], c = ys ,
112+ marker = '+' , label = 'Transp samples' , s = 30 )
114113pl .title ('Interp samples Sinkhorn' )
115114
116- pl .subplot (2 ,3 ,3 )
117-
118- pl .scatter (xt [:,0 ],xt [:,1 ],c = yt ,marker = 'o' ,label = 'Target samples' ,alpha = 0.5 )
119- pl .scatter (xstg [:,0 ],xstg [:,1 ],c = ys ,marker = '+' ,label = 'Transp samples' ,s = 30 )
120- pl .title ('Interp samples Grouplasso' )
115+ pl .subplot (1 , 3 , 3 )
116+ pl .scatter (xt [:, 0 ], xt [:, 1 ], c = yt , marker = 'o' ,
117+ label = 'Target samples' , alpha = 0.5 )
118+ pl .scatter (xstg [:, 0 ], xstg [:, 1 ], c = ys ,
119+ marker = '+' , label = 'Transp samples' , s = 30 )
120+ pl .title ('Interp samples Grouplasso' )
121+ pl .tight_layout ()
122+ pl .show ()
0 commit comments