diff options
author | Slasnista <stan.chambon@gmail.com> | 2017-08-29 13:34:34 +0200 |
---|---|---|
committer | Slasnista <stan.chambon@gmail.com> | 2017-08-29 13:34:34 +0200 |
commit | 65de6fc9add57b95b8968e1e75fe1af342f81d01 (patch) | |
tree | 1b973cb5314af46a28060c477840903bf3fbf4ac /examples | |
parent | a29e22db4772ebc4a8266c917e2e662f624c6baa (diff) |
pass on examples | introduced RandomState
Diffstat (limited to 'examples')
-rw-r--r-- | examples/da/plot_otda_classes.py | 20 | ||||
-rw-r--r-- | examples/da/plot_otda_color_images.py | 19 | ||||
-rw-r--r-- | examples/da/plot_otda_d2.py | 12 | ||||
-rw-r--r-- | examples/da/plot_otda_mapping.py | 21 | ||||
-rw-r--r-- | examples/da/plot_otda_mapping_colors_images.py | 9 |
5 files changed, 59 insertions, 22 deletions
diff --git a/examples/da/plot_otda_classes.py b/examples/da/plot_otda_classes.py index 6870fa4..ec57a37 100644 --- a/examples/da/plot_otda_classes.py +++ b/examples/da/plot_otda_classes.py @@ -15,19 +15,23 @@ approaches currently supported in POT. # License: MIT License import matplotlib.pylab as pl -import numpy as np import ot -np.random.seed(42) -# number of source and target points to generate -ns = 150 -nt = 150 +############################################################################## +# generate data +############################################################################## + +n_source_samples = 150 +n_target_samples = 150 + +Xs, ys = ot.datasets.get_data_classif('3gauss', n_source_samples) +Xt, yt = ot.datasets.get_data_classif('3gauss2', n_target_samples) -Xs, ys = ot.datasets.get_data_classif('3gauss', ns) -Xt, yt = ot.datasets.get_data_classif('3gauss2', nt) +############################################################################## # Instantiate the different transport algorithms and fit them +############################################################################## # EMD Transport ot_emd = ot.da.EMDTransport() @@ -52,6 +56,7 @@ transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) transp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs) + ############################################################################## # Fig 1 : plots source and target samples ############################################################################## @@ -72,6 +77,7 @@ pl.legend(loc=0) pl.title('Target samples') pl.tight_layout() + ############################################################################## # Fig 2 : plot optimal couplings and transported samples ############################################################################## diff --git a/examples/da/plot_otda_color_images.py b/examples/da/plot_otda_color_images.py index 805d0b0..3984afb 100644 --- a/examples/da/plot_otda_color_images.py +++ b/examples/da/plot_otda_color_images.py @@ -22,7 +22,8 @@ from scipy import ndimage import matplotlib.pylab as pl import ot -np.random.seed(42) + +r = np.random.RandomState(42) def im2mat(I): @@ -39,6 +40,10 @@ def minmax(I): return np.clip(I, 0, 1) +############################################################################## +# generate data +############################################################################## + # Loading images I1 = ndimage.imread('../../data/ocean_day.jpg').astype(np.float64) / 256 I2 = ndimage.imread('../../data/ocean_sunset.jpg').astype(np.float64) / 256 @@ -48,12 +53,17 @@ 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,)) +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) Xs = X1[idx1, :] Xt = X2[idx2, :] + +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + # EMDTransport ot_emd = ot.da.EMDTransport() ot_emd.fit(Xs=Xs, Xt=Xt) @@ -75,6 +85,7 @@ I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) + ############################################################################## # plot original image ############################################################################## @@ -91,6 +102,7 @@ pl.imshow(I2) pl.axis('off') pl.title('Image 2') + ############################################################################## # scatter plot of colors ############################################################################## @@ -112,6 +124,7 @@ pl.ylabel('Blue') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot new images ############################################################################## diff --git a/examples/da/plot_otda_d2.py b/examples/da/plot_otda_d2.py index 8833eb2..3daa0a6 100644 --- a/examples/da/plot_otda_d2.py +++ b/examples/da/plot_otda_d2.py @@ -19,10 +19,12 @@ of what the transport methods are doing. # License: MIT License import matplotlib.pylab as pl -import numpy as np import ot -np.random.seed(42) + +############################################################################## +# generate data +############################################################################## n_samples_source = 150 n_samples_target = 150 @@ -33,7 +35,10 @@ Xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target) # Cost matrix M = ot.dist(Xs, Xt, metric='sqeuclidean') + +############################################################################## # Instantiate the different transport algorithms and fit them +############################################################################## # EMD Transport ot_emd = ot.da.EMDTransport() @@ -52,6 +57,7 @@ transp_Xs_emd = ot_emd.transform(Xs=Xs) transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) + ############################################################################## # Fig 1 : plots source and target samples + matrix of pairwise distance ############################################################################## @@ -78,6 +84,7 @@ pl.yticks([]) pl.title('Matrix of pairwise distances') pl.tight_layout() + ############################################################################## # Fig 2 : plots optimal couplings for the different methods ############################################################################## @@ -127,6 +134,7 @@ pl.yticks([]) pl.title('Main coupling coefficients\nSinkhornLpl1Transport') pl.tight_layout() + ############################################################################## # Fig 3 : plot transported samples ############################################################################## diff --git a/examples/da/plot_otda_mapping.py b/examples/da/plot_otda_mapping.py index aea7f09..09d2cb4 100644 --- a/examples/da/plot_otda_mapping.py +++ b/examples/da/plot_otda_mapping.py @@ -23,25 +23,31 @@ import matplotlib.pylab as pl import ot -np.random.seed(42) - ############################################################################## -# generate +# generate data ############################################################################## -n = 100 # nb samples in source and target datasets +n_source_samples = 100 +n_target_samples = 100 theta = 2 * np.pi / 20 noise_level = 0.1 -Xs, ys = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) -Xs_new, _ = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) + +Xs, ys = ot.datasets.get_data_classif( + 'gaussrot', n_source_samples, nz=noise_level) +Xs_new, _ = ot.datasets.get_data_classif( + 'gaussrot', n_source_samples, nz=noise_level) Xt, yt = ot.datasets.get_data_classif( - 'gaussrot', n, theta=theta, nz=noise_level) + 'gaussrot', n_target_samples, theta=theta, nz=noise_level) # one of the target mode changes its variance (no linear mapping) Xt[yt == 2] *= 3 Xt = Xt + 4 +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + # MappingTransport with linear kernel ot_mapping_linear = ot.da.MappingTransport( kernel="linear", mu=1e0, eta=1e-8, bias=True, @@ -80,6 +86,7 @@ pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') pl.legend(loc=0) pl.title('Source and target distributions') + ############################################################################## # plot transported samples ############################################################################## diff --git a/examples/da/plot_otda_mapping_colors_images.py b/examples/da/plot_otda_mapping_colors_images.py index 6c024ea..a628b05 100644 --- a/examples/da/plot_otda_mapping_colors_images.py +++ b/examples/da/plot_otda_mapping_colors_images.py @@ -23,7 +23,7 @@ from scipy import ndimage import matplotlib.pylab as pl import ot -np.random.seed(42) +r = np.random.RandomState(42) def im2mat(I): @@ -54,8 +54,8 @@ 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,)) +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) Xs = X1[idx1, :] Xt = X2[idx2, :] @@ -91,6 +91,7 @@ ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt) X1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) + ############################################################################## # plot original images ############################################################################## @@ -107,6 +108,7 @@ pl.axis('off') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot pixel values distribution ############################################################################## @@ -128,6 +130,7 @@ pl.ylabel('Blue') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot transformed images ############################################################################## |