From 25ef32ff892fba105a4a116a804b1e4f08ae57cd Mon Sep 17 00:00:00 2001 From: Alexandre Gramfort Date: Wed, 12 Jul 2017 22:51:17 +0200 Subject: more --- examples/plot_OTDA_2D.py | 114 ++++++++++++++++---------------- examples/plot_OTDA_classes.py | 129 +++++++++++++++++++------------------ examples/plot_OTDA_color_images.py | 115 +++++++++++++++++---------------- examples/plot_OT_2D_samples.py | 52 +++++++-------- examples/plot_OT_L1_vs_L2.py | 80 +++++++++++------------ 5 files changed, 247 insertions(+), 243 deletions(-) (limited to 'examples') diff --git a/examples/plot_OTDA_2D.py b/examples/plot_OTDA_2D.py index a1fb804..1bda59c 100644 --- a/examples/plot_OTDA_2D.py +++ b/examples/plot_OTDA_2D.py @@ -11,110 +11,112 @@ import matplotlib.pylab as pl import ot - #%% parameters -n=150 # nb bins +n = 150 # nb bins -xs,ys=ot.datasets.get_data_classif('3gauss',n) -xt,yt=ot.datasets.get_data_classif('3gauss2',n) +xs, ys = ot.datasets.get_data_classif('3gauss', n) +xt, yt = ot.datasets.get_data_classif('3gauss2', n) -a,b = ot.unif(n),ot.unif(n) +a, b = ot.unif(n), ot.unif(n) # loss matrix -M=ot.dist(xs,xt) -#M/=M.max() +M = ot.dist(xs, xt) +# M/=M.max() #%% plot samples pl.figure(1) - -pl.subplot(2,2,1) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') +pl.subplot(2, 2, 1) +pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') pl.legend(loc=0) pl.title('Source distributions') -pl.subplot(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') +pl.subplot(2, 2, 2) +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') pl.legend(loc=0) pl.title('target distributions') pl.figure(2) -pl.imshow(M,interpolation='nearest') +pl.imshow(M, interpolation='nearest') pl.title('Cost matrix M') #%% OT estimation # EMD -G0=ot.emd(a,b,M) +G0 = ot.emd(a, b, M) # sinkhorn -lambd=1e-1 -Gs=ot.sinkhorn(a,b,M,lambd) +lambd = 1e-1 +Gs = ot.sinkhorn(a, b, M, lambd) # Group lasso regularization -reg=1e-1 -eta=1e0 -Gg=ot.da.sinkhorn_lpl1_mm(a,ys.astype(np.int),b,M,reg,eta) +reg = 1e-1 +eta = 1e0 +Gg = ot.da.sinkhorn_lpl1_mm(a, ys.astype(np.int), b, M, reg, eta) #%% visu matrices pl.figure(3) -pl.subplot(2,3,1) -pl.imshow(G0,interpolation='nearest') +pl.subplot(2, 3, 1) +pl.imshow(G0, interpolation='nearest') pl.title('OT matrix ') -pl.subplot(2,3,2) -pl.imshow(Gs,interpolation='nearest') +pl.subplot(2, 3, 2) +pl.imshow(Gs, interpolation='nearest') pl.title('OT matrix Sinkhorn') -pl.subplot(2,3,3) -pl.imshow(Gg,interpolation='nearest') +pl.subplot(2, 3, 3) +pl.imshow(Gg, interpolation='nearest') pl.title('OT matrix Group lasso') -pl.subplot(2,3,4) -ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') +pl.subplot(2, 3, 4) +ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) +pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.subplot(2,3,5) -ot.plot.plot2D_samples_mat(xs,xt,Gs,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') +pl.subplot(2, 3, 5) +ot.plot.plot2D_samples_mat(xs, xt, Gs, c=[.5, .5, 1]) +pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.subplot(2,3,6) -ot.plot.plot2D_samples_mat(xs,xt,Gg,c=[.5,.5,1]) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') +pl.subplot(2, 3, 6) +ot.plot.plot2D_samples_mat(xs, xt, Gg, c=[.5, .5, 1]) +pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') +pl.tight_layout() #%% sample interpolation -xst0=n*G0.dot(xt) -xsts=n*Gs.dot(xt) -xstg=n*Gg.dot(xt) - -pl.figure(4) -pl.subplot(2,3,1) - +xst0 = n * G0.dot(xt) +xsts = n * Gs.dot(xt) +xstg = n * Gg.dot(xt) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xst0[:,0],xst0[:,1],c=ys,marker='+',label='Transp samples',s=30) +pl.figure(4, figsize=(8, 3)) +pl.subplot(1, 3, 1) +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +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,3,2) - - -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xsts[:,0],xsts[:,1],c=ys,marker='+',label='Transp samples',s=30) +pl.subplot(1, 3, 2) +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +pl.scatter(xsts[:, 0], xsts[:, 1], c=ys, + marker='+', label='Transp samples', s=30) pl.title('Interp samples Sinkhorn') -pl.subplot(2,3,3) - -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples',alpha=0.5) -pl.scatter(xstg[:,0],xstg[:,1],c=ys,marker='+',label='Transp samples',s=30) -pl.title('Interp samples Grouplasso') \ No newline at end of file +pl.subplot(1, 3, 3) +pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +pl.scatter(xstg[:, 0], xstg[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.title('Interp samples Grouplasso') +pl.tight_layout() +pl.show() diff --git a/examples/plot_OTDA_classes.py b/examples/plot_OTDA_classes.py index 089b45b..4d3846a 100644 --- a/examples/plot_OTDA_classes.py +++ b/examples/plot_OTDA_classes.py @@ -10,29 +10,25 @@ 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) - +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) +pl.figure(1, figsize=(6.4, 3)) -pl.subplot(2,2,1) -pl.scatter(xs[:,0],xs[:,1],c=ys,marker='+',label='Source samples') +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(2,2,2) -pl.scatter(xt[:,0],xt[:,1],c=yt,marker='o',label='Target samples') +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') @@ -40,73 +36,78 @@ 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 - +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() +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() - +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() - +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 -pl.figure(4,(15,8)) -param_img={'interpolation':'nearest','cmap':'jet'} +param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} -pl.subplot(2,4,1) -pl.imshow(da_emd.G,**param_img) +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,2) -pl.imshow(da_entrop.G,**param_img) -pl.title('OT matrix sinkhorn') - -pl.subplot(2,4,3) -pl.imshow(da_lpl1.G,**param_img) -pl.title('OT matrix non-convex Group Lasso') - -pl.subplot(2,4,4) -pl.imshow(da_l1l2.G,**param_img) -pl.title('OT matrix Group Lasso') +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.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 Sinkhorn') - -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 non-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 Group Lasso') \ No newline at end of file +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() diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py index 68eee44..a8861c6 100644 --- a/examples/plot_OTDA_color_images.py +++ b/examples/plot_OTDA_color_images.py @@ -4,142 +4,143 @@ OT for domain adaptation with image color adaptation [6] ======================================================== -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. +[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). +Regularized discrete optimal transport. +SIAM Journal on Imaging Sciences, 7(3), 1853-1882. """ import numpy as np -import scipy.ndimage as spi +from scipy import ndimage import matplotlib.pylab as pl import ot #%% Loading images -I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 -I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 +I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 +I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 #%% Plot images -pl.figure(1) +pl.figure(1, figsize=(6.4, 3)) -pl.subplot(1,2,1) +pl.subplot(1, 2, 1) pl.imshow(I1) +pl.axis('off') pl.title('Image 1') -pl.subplot(1,2,2) +pl.subplot(1, 2, 2) pl.imshow(I2) +pl.axis('off') pl.title('Image 2') pl.show() #%% Image conversion and dataset generation + def im2mat(I): """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) + return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) + -def mat2im(X,shape): +def mat2im(X, shape): """Converts back a matrix to an image""" return X.reshape(shape) -X1=im2mat(I1) -X2=im2mat(I2) +X1 = im2mat(I1) +X2 = im2mat(I2) # training samples -nb=1000 -idx1=np.random.randint(X1.shape[0],size=(nb,)) -idx2=np.random.randint(X2.shape[0],size=(nb,)) +nb = 1000 +idx1 = np.random.randint(X1.shape[0], size=(nb,)) +idx2 = np.random.randint(X2.shape[0], size=(nb,)) -xs=X1[idx1,:] -xt=X2[idx2,:] +xs = X1[idx1, :] +xt = X2[idx2, :] #%% Plot image distributions -pl.figure(2,(10,5)) +pl.figure(2, figsize=(6.4, 3)) -pl.subplot(1,2,1) -pl.scatter(xs[:,0],xs[:,2],c=xs) -pl.axis([0,1,0,1]) +pl.subplot(1, 2, 1) +pl.scatter(xs[:, 0], xs[:, 2], c=xs) +pl.axis([0, 1, 0, 1]) pl.xlabel('Red') pl.ylabel('Blue') pl.title('Image 1') -pl.subplot(1,2,2) -#pl.imshow(I2) -pl.scatter(xt[:,0],xt[:,2],c=xt) -pl.axis([0,1,0,1]) +pl.subplot(1, 2, 2) +pl.scatter(xt[:, 0], xt[:, 2], c=xt) +pl.axis([0, 1, 0, 1]) pl.xlabel('Red') pl.ylabel('Blue') pl.title('Image 2') - -pl.show() - - +pl.tight_layout() #%% domain adaptation between images # LP problem -da_emd=ot.da.OTDA() # init class -da_emd.fit(xs,xt) # fit distributions - +da_emd = ot.da.OTDA() # init class +da_emd.fit(xs, xt) # fit distributions # sinkhorn regularization -lambd=1e-1 -da_entrop=ot.da.OTDA_sinkhorn() -da_entrop.fit(xs,xt,reg=lambd) - - +lambd = 1e-1 +da_entrop = ot.da.OTDA_sinkhorn() +da_entrop.fit(xs, xt, reg=lambd) #%% prediction between images (using out of sample prediction as in [6]) -X1t=da_emd.predict(X1) -X2t=da_emd.predict(X2,-1) +X1t = da_emd.predict(X1) +X2t = da_emd.predict(X2, -1) - -X1te=da_entrop.predict(X1) -X2te=da_entrop.predict(X2,-1) +X1te = da_entrop.predict(X1) +X2te = da_entrop.predict(X2, -1) def minmax(I): - return np.minimum(np.maximum(I,0),1) + return np.clip(I, 0, 1) -I1t=minmax(mat2im(X1t,I1.shape)) -I2t=minmax(mat2im(X2t,I2.shape)) +I1t = minmax(mat2im(X1t, I1.shape)) +I2t = minmax(mat2im(X2t, I2.shape)) -I1te=minmax(mat2im(X1te,I1.shape)) -I2te=minmax(mat2im(X2te,I2.shape)) +I1te = minmax(mat2im(X1te, I1.shape)) +I2te = minmax(mat2im(X2te, I2.shape)) #%% plot all images -pl.figure(2,(10,8)) - -pl.subplot(2,3,1) +pl.figure(2, figsize=(8, 4)) +pl.subplot(2, 3, 1) pl.imshow(I1) +pl.axis('off') pl.title('Image 1') -pl.subplot(2,3,2) +pl.subplot(2, 3, 2) pl.imshow(I1t) +pl.axis('off') pl.title('Image 1 Adapt') - -pl.subplot(2,3,3) +pl.subplot(2, 3, 3) pl.imshow(I1te) +pl.axis('off') pl.title('Image 1 Adapt (reg)') -pl.subplot(2,3,4) - +pl.subplot(2, 3, 4) pl.imshow(I2) +pl.axis('off') pl.title('Image 2') -pl.subplot(2,3,5) +pl.subplot(2, 3, 5) pl.imshow(I2t) +pl.axis('off') pl.title('Image 2 Adapt') - -pl.subplot(2,3,6) +pl.subplot(2, 3, 6) pl.imshow(I2te) +pl.axis('off') pl.title('Image 2 Adapt (reg)') +pl.tight_layout() pl.show() diff --git a/examples/plot_OT_2D_samples.py b/examples/plot_OT_2D_samples.py index 3a93591..75ed7db 100644 --- a/examples/plot_OT_2D_samples.py +++ b/examples/plot_OT_2D_samples.py @@ -8,7 +8,7 @@ """ import numpy as np -import matplotlib.pylab as plt +import matplotlib.pylab as pl import ot #%% parameters and data generation @@ -32,31 +32,31 @@ M /= M.max() #%% plot samples -plt.figure(1) -plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') -plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') -plt.legend(loc=0) -plt.title('Source and target distributions') +pl.figure(1) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.legend(loc=0) +pl.title('Source and target distributions') -plt.figure(2) -plt.imshow(M, interpolation='nearest') -plt.title('Cost matrix M') +pl.figure(2) +pl.imshow(M, interpolation='nearest') +pl.title('Cost matrix M') #%% EMD G0 = ot.emd(a, b, M) -plt.figure(3) -plt.imshow(G0, interpolation='nearest') -plt.title('OT matrix G0') +pl.figure(3) +pl.imshow(G0, interpolation='nearest') +pl.title('OT matrix G0') -plt.figure(4) +pl.figure(4) ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) -plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') -plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') -plt.legend(loc=0) -plt.title('OT matrix with samples') +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.legend(loc=0) +pl.title('OT matrix with samples') #%% sinkhorn @@ -66,15 +66,15 @@ lambd = 5e-4 Gs = ot.sinkhorn(a, b, M, lambd) -plt.figure(5) -plt.imshow(Gs, interpolation='nearest') -plt.title('OT matrix sinkhorn') +pl.figure(5) +pl.imshow(Gs, interpolation='nearest') +pl.title('OT matrix sinkhorn') -plt.figure(6) +pl.figure(6) ot.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1]) -plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') -plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') -plt.legend(loc=0) -plt.title('OT matrix Sinkhorn with samples') +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') +pl.legend(loc=0) +pl.title('OT matrix Sinkhorn with samples') -plt.show() +pl.show() diff --git a/examples/plot_OT_L1_vs_L2.py b/examples/plot_OT_L1_vs_L2.py index e11d6ad..86d902b 100644 --- a/examples/plot_OT_L1_vs_L2.py +++ b/examples/plot_OT_L1_vs_L2.py @@ -12,7 +12,7 @@ https://arxiv.org/pdf/1706.07650.pdf """ import numpy as np -import matplotlib.pylab as plt +import matplotlib.pylab as pl import ot #%% parameters and data generation @@ -55,58 +55,58 @@ for data in range(2): #%% plot samples - plt.figure(1 + 3 * data, figsize=(7, 3)) - plt.clf() - plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - plt.axis('equal') - plt.title('Source and traget distributions') + pl.figure(1 + 3 * data, figsize=(7, 3)) + pl.clf() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + pl.title('Source and traget distributions') - plt.figure(2 + 3 * data, figsize=(7, 3)) + pl.figure(2 + 3 * data, figsize=(7, 3)) - plt.subplot(1, 3, 1) - plt.imshow(M1, interpolation='nearest') - plt.title('Euclidean cost') + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') - plt.subplot(1, 3, 2) - plt.imshow(M2, interpolation='nearest') - plt.title('Squared Euclidean cost') + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') + pl.title('Squared Euclidean cost') - plt.subplot(1, 3, 3) - plt.imshow(Mp, interpolation='nearest') - plt.title('Sqrt Euclidean cost') - plt.tight_layout() + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') + pl.title('Sqrt Euclidean cost') + pl.tight_layout() #%% EMD G1 = ot.emd(a, b, M1) G2 = ot.emd(a, b, M2) Gp = ot.emd(a, b, Mp) - plt.figure(3 + 3 * data, figsize=(7, 3)) + pl.figure(3 + 3 * data, figsize=(7, 3)) - plt.subplot(1, 3, 1) + pl.subplot(1, 3, 1) ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) - plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - plt.axis('equal') - # plt.legend(loc=0) - plt.title('OT Euclidean') + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT Euclidean') - plt.subplot(1, 3, 2) + pl.subplot(1, 3, 2) ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) - plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - plt.axis('equal') - # plt.legend(loc=0) - plt.title('OT squared Euclidean') + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT squared Euclidean') - plt.subplot(1, 3, 3) + pl.subplot(1, 3, 3) ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) - plt.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - plt.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - plt.axis('equal') - # plt.legend(loc=0) - plt.title('OT sqrt Euclidean') - plt.tight_layout() - -plt.show() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + # pl.legend(loc=0) + pl.title('OT sqrt Euclidean') + pl.tight_layout() + +pl.show() -- cgit v1.2.3