From 71f502b063dc5ac4000c5171c75eb217ae110e36 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 21:57:57 +0900 Subject: [PATCH 01/84] First commit From b5c6af6bfbbac5dda016d69ec8bdfd446a850bc1 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 22:00:39 +0900 Subject: [PATCH 02/84] =?UTF-8?q?=E6=97=A2=E5=AD=98=E3=81=AE=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E3=82=92refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 2 +- tutorials/ukr/fitting_saddle_shape.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index b3ed3b8..611cf90 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -90,7 +90,7 @@ def fit(self, nb_epoch=100, verbose=True, eta=0.5, expand_epoch=None): self._done_fit = True return self.history - def calculation_history_of_mapping(self, resolution, size='auto'): + def calculate_history_of_mapping(self, resolution, size='auto'): """ :param resolution: :param size: diff --git a/tutorials/ukr/fitting_saddle_shape.py b/tutorials/ukr/fitting_saddle_shape.py index dad67e2..a6dee2c 100644 --- a/tutorials/ukr/fitting_saddle_shape.py +++ b/tutorials/ukr/fitting_saddle_shape.py @@ -38,7 +38,7 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_) som.fit(nb_epoch=nb_epoch) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.calculation_history_of_mapping(resolution=30) + ukr.calculate_history_of_mapping(resolution=30) fig = plt.figure(figsize=[7, 8]) ax_latent_space_som = fig.add_subplot(2, 2, 1, aspect='equal') From 261abd228943c6e5cc8fc8f7d67753d15596af9a Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 22:38:21 +0900 Subject: [PATCH 03/84] =?UTF-8?q?visualize=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=A8=E3=81=97=E3=81=A6=E4=BD=9C=E6=88=90=E4=B8=AD?= =?UTF-8?q?=E3=80=82=E5=A4=89=E6=95=B0=E3=81=AE=E5=88=9D=E6=9C=9F=E5=8C=96?= =?UTF-8?q?=E3=81=AA=E3=81=A9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 122 +++++++++++++----- 1 file changed, 89 insertions(+), 33 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 611cf90..76585e7 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -27,12 +27,8 @@ def __init__(self, X, n_components, bandwidth_gaussian_kernel=1.0, self.lambda_ = lambda_ - - self._done_fit = False - - def fit(self, nb_epoch=100, verbose=True, eta=0.5, expand_epoch=None): K = self.X @ self.X.T @@ -46,13 +42,11 @@ def fit(self, nb_epoch=100, verbose=True, eta=0.5, expand_epoch=None): self.history['zvar'] = np.zeros((nb_epoch, self.n_components)) self.history['obj_func'] = np.zeros(nb_epoch) - if verbose: bar = tqdm(range(nb_epoch)) else: bar = range(nb_epoch) - for epoch in bar: Delta = self.Z[:, None, :] - self.Z[None, :, :] DistZ = np.sum(np.square(Delta), axis=2) @@ -65,7 +59,7 @@ def fit(self, nb_epoch=100, verbose=True, eta=0.5, expand_epoch=None): R = H * GInv Y = R @ self.X - DeltaYX = Y[:,None,:] - self.X[None, :, :] + DeltaYX = Y[:, None, :] - self.X[None, :, :] Error = Y - self.X obj_func = np.sum(np.square(Error)) / self.n_samples + self.lambda_ * np.sum(np.square(self.Z)) @@ -76,17 +70,14 @@ def fit(self, nb_epoch=100, verbose=True, eta=0.5, expand_epoch=None): self.Z += eta * dFdZ if self.is_compact: - self.Z = np.clip(self.Z,-1.0,1.0) + self.Z = np.clip(self.Z, -1.0, 1.0) else: self.Z -= self.Z.mean(axis=0) - if self.is_save_hisotry: self.history['z'][epoch] = self.Z self.history['obj_func'][epoch] = obj_func - - self._done_fit = True return self.history @@ -134,8 +125,8 @@ def transform(self, Xnew, nb_epoch_trans=100, eta_trans=0.5, verbose=True, const # initialize Znew, using latent variables of observed data Dist_Xnew_X = dist.cdist(Xnew, self.X) - BMS = np.argmin(Dist_Xnew_X, axis=1) # calculate Best Matching Sample - Znew = self.Z[BMS,:] # initialize Znew + BMS = np.argmin(Dist_Xnew_X, axis=1) # calculate Best Matching Sample + Znew = self.Z[BMS, :] # initialize Znew if verbose: bar = tqdm(range(nb_epoch_trans)) @@ -144,25 +135,25 @@ def transform(self, Xnew, nb_epoch_trans=100, eta_trans=0.5, verbose=True, const for epoch in bar: # calculate gradient - Delta = self.Z[None,:,:] - Znew[:,None,:] # shape = (Nnew,N,L) - Dist_Znew_Z = dist.cdist(Znew,self.Z,"sqeuclidean") # shape = (Nnew,N) - H = np.exp(-0.5 * self.precision * Dist_Znew_Z) # shape = (Nnew,N) - G = np.sum(H,axis=1)[:,None] # shape = (Nnew,1) - Ginv = np.reciprocal(G) # shape = (Nnew,1) - R = H * Ginv # shape = (Nnew,N) - F = R @ self.X # shape = (Nnew,D) - - Delta_bar = np.einsum("kn,knl->kl",R,Delta) # (Nnew,N)times(Nnew,N,L)=(Nnew,L) + Delta = self.Z[None, :, :] - Znew[:, None, :] # shape = (Nnew,N,L) + Dist_Znew_Z = dist.cdist(Znew, self.Z, "sqeuclidean") # shape = (Nnew,N) + H = np.exp(-0.5 * self.precision * Dist_Znew_Z) # shape = (Nnew,N) + G = np.sum(H, axis=1)[:, None] # shape = (Nnew,1) + Ginv = np.reciprocal(G) # shape = (Nnew,1) + R = H * Ginv # shape = (Nnew,N) + F = R @ self.X # shape = (Nnew,D) + + Delta_bar = np.einsum("kn,knl->kl", R, Delta) # (Nnew,N)times(Nnew,N,L)=(Nnew,L) # Delta_bar = np.sum(R[:,:,None] * Delta, axis=1) # same calculate - dRdZ = self.precision * R[:, :, None] * (Delta - Delta_bar[:, None, :]) # shape = (Nnew,N,L) + dRdZ = self.precision * R[:, :, None] * (Delta - Delta_bar[:, None, :]) # shape = (Nnew,N,L) - dFdZ = np.einsum("nd,knl->kdl",self.X,dRdZ) # shape = (Nnew,D,L) + dFdZ = np.einsum("nd,knl->kdl", self.X, dRdZ) # shape = (Nnew,D,L) # dFdZ = np.sum(self.X[None,:,:,None]*dRdZ[:,:,None,:],axis=1) # same calculate - dEdZ = 2.0 * np.einsum("kd,kdl->kl",F-Xnew,dFdZ) # shape (Nnew, L) + dEdZ = 2.0 * np.einsum("kd,kdl->kl", F - Xnew, dFdZ) # shape (Nnew, L) # update latent variables Znew -= eta_trans * dEdZ if self.is_compact: - Znew = np.clip(Znew,-1.0,1.0) + Znew = np.clip(Znew, -1.0, 1.0) if constrained: Znew = np.clip(Znew, self.Z.min(axis=0), self.Z.max(axis=0)) @@ -171,19 +162,84 @@ def transform(self, Xnew, nb_epoch_trans=100, eta_trans=0.5, verbose=True, const def inverse_transform(self, Znew): if not self._done_fit: raise ValueError("fit is not done") - if Znew.shape[1]!=self.n_components: + if Znew.shape[1] != self.n_components: raise ValueError("Znew dimension must be {}".format(self.n_components)) - Dist_Znew_Z = dist.cdist(Znew,self.Z,"sqeuclidean") # shape = (Nnew,N) - H = np.exp(-0.5 * self.precision * Dist_Znew_Z) # shape = (Nnew,N) - G = np.sum(H,axis=1)[:,None] # shape = (Nnew,1) - Ginv = np.reciprocal(G) # shape = (Nnew,1) - R = H * Ginv # shape = (Nnew,N) - F = R @ self.X # shape = (Nnew,D) + Dist_Znew_Z = dist.cdist(Znew, self.Z, "sqeuclidean") # shape = (Nnew,N) + H = np.exp(-0.5 * self.precision * Dist_Znew_Z) # shape = (Nnew,N) + G = np.sum(H, axis=1)[:, None] # shape = (Nnew,1) + Ginv = np.reciprocal(G) # shape = (Nnew,1) + R = H * Ginv # shape = (Nnew,N) + F = R @ self.X # shape = (Nnew,D) return F + def visualize(self, resolution=100, label_data=None, label_feature=None, fig_size=None): + # invalid check + if self.n_components != 2: + raise ValueError('Now support only n_components = 2') + + # import necessary library to draw + import matplotlib + matplotlib.use('TkAgg') + import matplotlib.pyplot as plt + from matplotlib.widgets import RadioButtons + + # ---------------------------------- # + # -----initialize variables--------- # + # ---------------------------------- # + + self.latent_space_click_unit = 0 # index of the clicked representative point + + # invalid check + if label_data is None: + self.label_data = np.arange(self.n_samples) + elif isinstance(label_data, list): + self.label_data = label_data + elif isinstance(label_data, np.ndarray): + if np.squeeze(label_data).ndim == 1: + self.label_data = np.squeeze(label_data) + else: + raise ValueError('label_data must be 1d array') + else: + raise ValueError('label_data must be 1d array or list') + + if label_feature is None: + self.label_feature = np.arange(self.n_dimensions) + elif isinstance(label_feature, list): + self.label_feature = label_feature + elif isinstance(label_feature, np.ndarray): + if np.squeeze(label_feature).ndim == 1: + self.label_feature = np.squeeze(label_feature) + else: + raise ValueError('label_feature must be 1d array') + else: + raise ValueError('label_feature must be 1d array or list') + if fig_size is None: + self.fig = plt.figure(figsize=(15, 6)) + else: + self.fig = plt.figure(figsize=fig_size) + self.ax_latent_space = self.fig.add_subplot(1, 2, 1) + self.ax_latent_space.set_title('Latent space') + self.ax_hist = self.fig.add_subplot(1, 2, 2) + self.ax_hist.set_title('Mean of mapping') + + # ---------------------------------- # + # -------------draw map------------- # + # ---------------------------------- # + + self.__draw_latent_space() + self.__draw_hist() + + # connect figure and method defining action when latent space is clicked + self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) + plt.show() + + def __draw_latent_space(self): + self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) + def __onclick_fig(self,event): + pass def create_zeta(zeta_min, zeta_max, latent_dim, resolution): From f3a85b2eaada75456609bb3957b34a509af685f2 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 22:41:00 +0900 Subject: [PATCH 04/84] =?UTF-8?q?=E3=81=8A=E8=A9=A6=E3=81=97=E3=81=AE?= =?UTF-8?q?=E3=81=9F=E3=82=81=E3=81=ABtutorial=20code=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E3=82=8B=E3=80=82=E3=81=A8=E3=82=8A=E3=81=82=E3=81=88=E3=81=9A?= =?UTF-8?q?SOM=E3=81=AE=E5=8B=95=E7=89=A9=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=92=E5=BC=95=E3=81=A3=E5=BC=B5=E3=81=A3=E3=81=A6=E6=9D=A5?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tutorials/ukr/fitting_animal_data.py diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py new file mode 100644 index 0000000..07f9c2c --- /dev/null +++ b/tutorials/ukr/fitting_animal_data.py @@ -0,0 +1,39 @@ +import numpy as np + +import sys +sys.path.append('../../') + +from libs.models.som import SOM +from libs.visualization.som.Grad_norm import Grad_Norm +from libs.datasets.artificial import animal + + +if __name__ == '__main__': + nb_epoch = 50 + resolution = 10 + sigma_max = 2.2 + sigma_min = 0.3 + tau = 50 + latent_dim = 2 + seed = 1 + + title="animal map" + umat_resolution = 100 #U-matrix表示の解像度 + + X, labels = animal.load_data() + + np.random.seed(seed) + + som = SOM(X, latent_dim=latent_dim, resolution=resolution, sigma_max=sigma_max, sigma_min=sigma_min, tau=tau) + som.fit(nb_epoch=nb_epoch) + + Z = som.Z + sigma = som.history['sigma'][-1] + + som_umatrix = Grad_Norm(X=X, + Z=Z, + sigma=sigma, + labels=labels, + title_text=title, + resolution=umat_resolution) + som_umatrix.draw_umatrix() \ No newline at end of file From e381b629c3d09a58af49ac58ff8c698e1ce639e8 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 22:58:57 +0900 Subject: [PATCH 05/84] =?UTF-8?q?=E3=83=81=E3=83=A5=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=83=AA=E3=82=A2=E3=83=AB=E3=82=92=E5=AE=9F=E8=A1=8C=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=82=8B=E5=BD=A2=E3=81=BE=E3=81=A7=E6=9B=B8=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 42 ++++++++++------------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 07f9c2c..f4c1b3f 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -1,39 +1,27 @@ import numpy as np -import sys -sys.path.append('../../') - -from libs.models.som import SOM -from libs.visualization.som.Grad_norm import Grad_Norm +from libs.models.unsupervised_kernel_regression import UnsupervisedKernelRegression as UKR from libs.datasets.artificial import animal - if __name__ == '__main__': - nb_epoch = 50 - resolution = 10 - sigma_max = 2.2 - sigma_min = 0.3 - tau = 50 - latent_dim = 2 - seed = 1 + n_components = 2 + bandwidth = 0.5 + lambda_ = 0.0 + is_compact = True + is_save_history = True - title="animal map" - umat_resolution = 100 #U-matrix表示の解像度 + nb_epoch = 100 + eta = 5.0 + + resolution = 100 X, labels = animal.load_data() + seed = 13 np.random.seed(seed) - som = SOM(X, latent_dim=latent_dim, resolution=resolution, sigma_max=sigma_max, sigma_min=sigma_min, tau=tau) - som.fit(nb_epoch=nb_epoch) - - Z = som.Z - sigma = som.history['sigma'][-1] + ukr = UKR(X, n_components=n_components, bandwidth_gaussian_kernel=bandwidth, + is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_) + ukr.fit(nb_epoch=nb_epoch, eta=eta) - som_umatrix = Grad_Norm(X=X, - Z=Z, - sigma=sigma, - labels=labels, - title_text=title, - resolution=umat_resolution) - som_umatrix.draw_umatrix() \ No newline at end of file + ukr.visualize(resolution=resolution) From ce27717fcfd3ef400512555010712d948787a4ce Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 15 Apr 2020 23:00:08 +0900 Subject: [PATCH 06/84] =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93?= =?UTF-8?q?=E4=B8=8A=E3=81=AB=E3=81=8A=E3=81=91=E3=82=8B=E6=BD=9C=E5=9C=A8?= =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=AE=E6=8F=8F=E7=94=BB=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A1=8C=E3=81=A7=E3=81=8D=E3=82=8B=E3=81=A8=E3=81=93=E3=82=8D?= =?UTF-8?q?=E3=81=BE=E3=81=A7visualize=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=82=92=E3=82=B3=E3=83=BC=E3=83=87=E3=82=A3=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 76585e7..8bf5b56 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -220,7 +220,7 @@ def visualize(self, resolution=100, label_data=None, label_feature=None, fig_siz self.fig = plt.figure(figsize=(15, 6)) else: self.fig = plt.figure(figsize=fig_size) - self.ax_latent_space = self.fig.add_subplot(1, 2, 1) + self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') self.ax_latent_space.set_title('Latent space') self.ax_hist = self.fig.add_subplot(1, 2, 2) self.ax_hist.set_title('Mean of mapping') @@ -238,6 +238,10 @@ def visualize(self, resolution=100, label_data=None, label_feature=None, fig_siz def __draw_latent_space(self): self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) + self.fig.show() + + def __draw_hist(self): + pass def __onclick_fig(self,event): pass From b0239d92051d8e576e0a8dc54566b3b417529be4 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 17 Apr 2020 16:12:08 +0900 Subject: [PATCH 07/84] =?UTF-8?q?=E4=BB=A3=E8=A1=A8=E7=82=B9=E3=81=AE?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=80=81tsom2=5Fviwer=E3=81=AE=5F=5Fon=5Fcli?= =?UTF-8?q?ck=5Ffig=E3=82=92=E3=82=B3=E3=83=94=E3=83=BC=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=A2=E3=82=A6=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 8bf5b56..4650218 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,7 +174,7 @@ def inverse_transform(self, Znew): return F - def visualize(self, resolution=100, label_data=None, label_feature=None, fig_size=None): + def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size=None): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -188,8 +188,11 @@ def visualize(self, resolution=100, label_data=None, label_feature=None, fig_siz # ---------------------------------- # # -----initialize variables--------- # # ---------------------------------- # - - self.latent_space_click_unit = 0 # index of the clicked representative point + if self.is_compact: + self.representative_points = create_zeta(-1.0,1.0,self.n_components,resolution) + else: + raise ValueError('Not support is_compact=False') + self.click_point_latent_space = 0 # index of the clicked representative point # invalid check if label_data is None: @@ -243,7 +246,60 @@ def __draw_latent_space(self): def __draw_hist(self): pass def __onclick_fig(self,event): - pass + if event.xdata is not None: + if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか + # クリックされた座標の取得 + click_coordinates = np.array([event.xdata,event.ydata]) + + self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) # クリックしたところといちばん近いノードがどこかを計算 + + + # if self.map1_t-self.Map1_click_unit==0:#前回と同じところをクリックした or Map2をクリックした + # self.action1=0 + # elif self.map1_t -self.Map1_click_unit !=0:#前回と別のところをクリックした + # self.action1=1 + # + # #t回目→t+1回目 + # self.map1_t=self.Map1_click_unit + # self.map2_t = self.Map2_click_unit + # + # if self.action1==0 and self.action2==0:# map1: marginal map2: marginal + # #各マップのコンポーネントプレーンの計算 + # self.__calc_marginal_comp(1) # Map1_click_unitを元に計算 + # self.__calc_marginal_comp(2) # Map1_click_unitを元に計算 + # #component planeを描画 + # self.__draw_marginal_map1() + # self.__draw_marginal_map2() + # + # elif self.action1==1 and self.action2==0: # map1: marginal map2: conditional + # # 各マップのコンポーネントプレーンの計算 + # self.__calc_conditional_comp(2) # Map1_click_unitを元に計算 + # self.__calc_marginal_comp(1) + # # component planeを描画 + # self.__draw_marginal_map1() + # self.__draw_map1_click_point() + # self.__draw_conditional_map2() + # elif self.action1==0 and self.action2==1: # map1: conditional map2: marginal + # # 各マップのコンポーネントプレーンの計算 + # self.__calc_conditional_comp(1) # Map2_click_unitを元に計算 + # self.__calc_marginal_comp(2) + # # component planeを描画 + # self.__draw_marginal_map2() + # self.__draw_map2_click_point() + # self.__draw_conditional_map1() + # + # elif self.action1==1 and self.action2==1:# map1: conditional map2: conditional + # # 各マップのコンポーネントプレーンの計算 + # self.__calc_conditional_comp(1) # Map1_click_unitを元に計算 + # self.__calc_conditional_comp(2) # Map1_click_unitを元に計算 + # # component planeを描画 + # self.__draw_conditional_map1() + # self.__draw_conditional_map2() + # self.__draw_map1_click_point() + # self.__draw_map2_click_point() + elif event.inaxes == self.Map2.axes: # map2がクリックされた時 + pass + def __calc_nearest_representative_point(self): def create_zeta(zeta_min, zeta_max, latent_dim, resolution): From 086f02d85652fc156dd42d44f5cd48f9b438bf4a Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 19 Apr 2020 13:21:43 +0900 Subject: [PATCH 08/84] =?UTF-8?q?=E4=BB=A3=E8=A1=A8=E7=82=B9=E3=81=8B?= =?UTF-8?q?=E3=82=89=E3=81=9D=E3=81=AE=E5=86=99=E5=83=8F=E5=85=88=E3=82=92?= =?UTF-8?q?=E8=A8=88=E7=AE=97=E3=81=97=E5=A4=89=E6=95=B0=E3=81=AB=E6=A0=BC?= =?UTF-8?q?=E7=B4=8Dand=E3=82=AF=E3=83=AA=E3=83=83=E3=82=AF=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=9F=E5=BA=A7=E6=A8=99=E3=81=AB=E3=82=82=E3=81=A3?= =?UTF-8?q?=E3=81=A8=E3=82=82=E8=BF=91=E3=81=84=E4=BB=A3=E8=A1=A8=E7=82=B9?= =?UTF-8?q?=E3=82=92=E8=A8=88=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 4650218..6aa2ef3 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -191,9 +191,10 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size if self.is_compact: self.representative_points = create_zeta(-1.0,1.0,self.n_components,resolution) else: - raise ValueError('Not support is_compact=False') + raise ValueError('Not support is_compact=False') #create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point + self.representative_mapping = self.inverse_transform(self.representative_points) # invalid check if label_data is None: self.label_data = np.arange(self.n_samples) @@ -299,7 +300,10 @@ def __onclick_fig(self,event): # self.__draw_map2_click_point() elif event.inaxes == self.Map2.axes: # map2がクリックされた時 pass - def __calc_nearest_representative_point(self): + def __calc_nearest_representative_point(self, click_coodinates): + distance = dist.cdist(self.representative_points,click_coodinates.reshape(1,-1)) + index_nearest = np.argmin(distance) + return index_nearest def create_zeta(zeta_min, zeta_max, latent_dim, resolution): From 31a928bc1d78baf0e798fa42e69e22cb2f38ebd7 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 19 Apr 2020 13:47:04 +0900 Subject: [PATCH 09/84] =?UTF-8?q?ax=E3=81=AB=E8=A1=A8=E7=A4=BA=E3=81=99?= =?UTF-8?q?=E3=82=8Bmethod=E3=82=92=E8=A6=8B=E3=82=88=E3=81=86=E8=A6=8B?= =?UTF-8?q?=E3=81=BE=E3=81=AD=E3=81=A7=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 78 +++++++------------ 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6aa2ef3..eda6286 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -226,85 +226,59 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size self.fig = plt.figure(figsize=fig_size) self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') self.ax_latent_space.set_title('Latent space') - self.ax_hist = self.fig.add_subplot(1, 2, 2) - self.ax_hist.set_title('Mean of mapping') + self.ax_features = self.fig.add_subplot(1, 2, 2) + self.ax_features.set_title('Mean of mapping') # ---------------------------------- # # -------------draw map------------- # # ---------------------------------- # self.__draw_latent_space() - self.__draw_hist() + self.__draw_features() # connect figure and method defining action when latent space is clicked self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) plt.show() def __draw_latent_space(self): + self.ax_latent_space.cla() + self.__draw_features() self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) self.fig.show() - def __draw_hist(self): - pass + def __draw_features(self): + self.ax_features.cla() + self.__draw_latent_space() + self.ax_features.bar(self.label_feature,self.clicked_features) + self.ax_features.set_title('Features') + self.fig.show() + def __onclick_fig(self,event): if event.xdata is not None: if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか # クリックされた座標の取得 click_coordinates = np.array([event.xdata,event.ydata]) - self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) # クリックしたところといちばん近いノードがどこかを計算 - - - # if self.map1_t-self.Map1_click_unit==0:#前回と同じところをクリックした or Map2をクリックした - # self.action1=0 - # elif self.map1_t -self.Map1_click_unit !=0:#前回と別のところをクリックした - # self.action1=1 - # - # #t回目→t+1回目 - # self.map1_t=self.Map1_click_unit - # self.map2_t = self.Map2_click_unit - # - # if self.action1==0 and self.action2==0:# map1: marginal map2: marginal - # #各マップのコンポーネントプレーンの計算 - # self.__calc_marginal_comp(1) # Map1_click_unitを元に計算 - # self.__calc_marginal_comp(2) # Map1_click_unitを元に計算 - # #component planeを描画 - # self.__draw_marginal_map1() - # self.__draw_marginal_map2() - # - # elif self.action1==1 and self.action2==0: # map1: marginal map2: conditional - # # 各マップのコンポーネントプレーンの計算 - # self.__calc_conditional_comp(2) # Map1_click_unitを元に計算 - # self.__calc_marginal_comp(1) - # # component planeを描画 - # self.__draw_marginal_map1() - # self.__draw_map1_click_point() - # self.__draw_conditional_map2() - # elif self.action1==0 and self.action2==1: # map1: conditional map2: marginal - # # 各マップのコンポーネントプレーンの計算 - # self.__calc_conditional_comp(1) # Map2_click_unitを元に計算 - # self.__calc_marginal_comp(2) - # # component planeを描画 - # self.__draw_marginal_map2() - # self.__draw_map2_click_point() - # self.__draw_conditional_map1() - # - # elif self.action1==1 and self.action2==1:# map1: conditional map2: conditional - # # 各マップのコンポーネントプレーンの計算 - # self.__calc_conditional_comp(1) # Map1_click_unitを元に計算 - # self.__calc_conditional_comp(2) # Map1_click_unitを元に計算 - # # component planeを描画 - # self.__draw_conditional_map1() - # self.__draw_conditional_map2() - # self.__draw_map1_click_point() - # self.__draw_map2_click_point() - elif event.inaxes == self.Map2.axes: # map2がクリックされた時 + # クリックしたところといちばん近い代表点がどこかを計算 + self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) + + # その代表点の写像先の特徴量を計算 + self.__calc_features() + + # その特徴量の値を描画 + self.__draw_features() + elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 + # not implemented yet pass def __calc_nearest_representative_point(self, click_coodinates): distance = dist.cdist(self.representative_points,click_coodinates.reshape(1,-1)) index_nearest = np.argmin(distance) return index_nearest + def __calc_features(self): + self.clicked_features = self.representative_mapping[self.click_point_latent_space , :] + + def create_zeta(zeta_min, zeta_max, latent_dim, resolution): mesh1d, step = np.linspace(zeta_min, zeta_max, resolution, endpoint=False, retstep=True) From d5f278c852a8838c07a8c3a49e60a4d1bd0b1a38 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 19 Apr 2020 14:03:14 +0900 Subject: [PATCH 10/84] =?UTF-8?q?=E3=82=AB=E3=83=81=E3=82=AB=E3=83=81?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=A8=E3=83=90=E3=83=BC=E3=81=8C=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E3=81=95=E3=82=8C=E3=82=8B=E3=81=A8=E3=81=93=E3=82=8D?= =?UTF-8?q?=E3=81=BE=E3=81=A7=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index eda6286..dcb066c 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -219,6 +219,7 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size raise ValueError('label_feature must be 1d array') else: raise ValueError('label_feature must be 1d array or list') + self.clicked_features = np.zeros_like(self.label_feature) if fig_size is None: self.fig = plt.figure(figsize=(15, 6)) @@ -242,13 +243,13 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size def __draw_latent_space(self): self.ax_latent_space.cla() - self.__draw_features() + # self.__draw_features() self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) self.fig.show() def __draw_features(self): self.ax_features.cla() - self.__draw_latent_space() + # self.__draw_latent_space() self.ax_features.bar(self.label_feature,self.clicked_features) self.ax_features.set_title('Features') self.fig.show() From 223ca573d727e3cd36ef4c9e32cdafcc81f57042 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 15:25:49 +0900 Subject: [PATCH 11/84] =?UTF-8?q?tutorial=E3=81=A7=E5=8B=95=E7=89=A9?= =?UTF-8?q?=E3=81=A8=E7=89=B9=E5=BE=B4=E3=81=AE=E3=83=A9=E3=83=99=E3=83=AB?= =?UTF-8?q?=E3=82=82=E4=B8=8E=E3=81=88=E3=81=A6=E6=8F=8F=E7=94=BB=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index f4c1b3f..b4eb8b3 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -15,7 +15,8 @@ resolution = 100 - X, labels = animal.load_data() + X, labels_animal, labels_feature = animal.load_data(retlabel_animal=True, + retlabel_feature=True) seed = 13 np.random.seed(seed) @@ -24,4 +25,6 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.visualize(resolution=resolution) + ukr.visualize(resolution=resolution, + label_data = labels_animal, + label_feature=labels_feature) From 5f3eb9cc8fdcc870d44028b280eb6f3a397b61c9 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 15:28:12 +0900 Subject: [PATCH 12/84] =?UTF-8?q?=E7=89=B9=E5=BE=B4=E9=87=8F=E3=81=AE?= =?UTF-8?q?=E3=83=A9=E3=83=99=E3=83=AB=E3=82=92=E3=83=90=E3=83=BC=E3=81=AE?= =?UTF-8?q?=E6=A8=AA=E8=BB=B8=E3=81=AE=E3=83=A9=E3=83=99=E3=83=AB=E3=81=AB?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index dcb066c..3f85951 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -252,6 +252,7 @@ def __draw_features(self): # self.__draw_latent_space() self.ax_features.bar(self.label_feature,self.clicked_features) self.ax_features.set_title('Features') + self.ax_features.set_xticklabels(labels=self.label_feature,rotation=270) self.fig.show() def __onclick_fig(self,event): From 7f7a5732574ea733f62598d5991c2948b6fe87f5 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 16:37:53 +0900 Subject: [PATCH 13/84] =?UTF-8?q?=E3=82=AF=E3=83=AA=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=81=AE?= =?UTF-8?q?=E6=8F=8F=E7=94=BB=E3=81=A8=E3=83=A9=E3=83=99=E3=83=AB=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E3=81=AE=E4=B9=B1=E6=95=B0=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 3f85951..8681eff 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -197,7 +197,7 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size self.representative_mapping = self.inverse_transform(self.representative_points) # invalid check if label_data is None: - self.label_data = np.arange(self.n_samples) + self.label_data = label_data elif isinstance(label_data, list): self.label_data = label_data elif isinstance(label_data, np.ndarray): @@ -230,6 +230,8 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size self.ax_features = self.fig.add_subplot(1, 2, 2) self.ax_features.set_title('Mean of mapping') + epsilon = 0.03 * np.abs(self.representative_points.max() - self.representative_points.min()) + self.noise_label = epsilon * (np.random.rand(self.n_samples,self.n_components) * 2.0 - 1.0) # ---------------------------------- # # -------------draw map------------- # # ---------------------------------- # @@ -243,8 +245,15 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size def __draw_latent_space(self): self.ax_latent_space.cla() - # self.__draw_features() self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) + if self.label_data is None: + pass + else: + for z,noise,label in zip(self.Z, self.noise_label,self.label_data): + point_label = z + noise + self.ax_latent_space.text(point_label[0] ,point_label[1],label, + ha='center', va='bottom',color='black') + self.__draw_click_point() self.fig.show() def __draw_features(self): @@ -255,6 +264,11 @@ def __draw_features(self): self.ax_features.set_xticklabels(labels=self.label_feature,rotation=270) self.fig.show() + def __draw_click_point(self): + coordinate = self.representative_points[self.click_point_latent_space] + self.ax_latent_space.plot(coordinate[0],coordinate[1], + ".", color = "black", ms = 20, fillstyle = "none") + def __onclick_fig(self,event): if event.xdata is not None: if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか @@ -268,17 +282,18 @@ def __onclick_fig(self,event): self.__calc_features() # その特徴量の値を描画 + self.__draw_latent_space() self.__draw_features() elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 # not implemented yet pass - def __calc_nearest_representative_point(self, click_coodinates): - distance = dist.cdist(self.representative_points,click_coodinates.reshape(1,-1)) + def __calc_nearest_representative_point(self, click_point): + distance = dist.cdist(self.representative_points, click_point.reshape(1, -1)) index_nearest = np.argmin(distance) return index_nearest def __calc_features(self): - self.clicked_features = self.representative_mapping[self.click_point_latent_space , :] + self.clicked_features = self.representative_mapping[self.click_point_latent_space, :] From f0330013c226324d083d9ed9c8dd95287890335b Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 16:55:47 +0900 Subject: [PATCH 14/84] =?UTF-8?q?=E3=81=BE=E3=81=A0=E3=82=AF=E3=83=AA?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=97=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=88=9D=E6=9C=9F=E7=8A=B6=E6=85=8B=E3=81=AE=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=82=92=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 8681eff..874e4de 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -193,6 +193,8 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size else: raise ValueError('Not support is_compact=False') #create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point + self.clicked_features = self.X.mean(axis=0) + self.is_initial_view = True self.representative_mapping = self.inverse_transform(self.representative_points) # invalid check @@ -219,7 +221,6 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size raise ValueError('label_feature must be 1d array') else: raise ValueError('label_feature must be 1d array or list') - self.clicked_features = np.zeros_like(self.label_feature) if fig_size is None: self.fig = plt.figure(figsize=(15, 6)) @@ -253,14 +254,25 @@ def __draw_latent_space(self): point_label = z + noise self.ax_latent_space.text(point_label[0] ,point_label[1],label, ha='center', va='bottom',color='black') - self.__draw_click_point() + # self.ax_latent_space.set_xlim(self.representative_points[:,0].min(), + # self.representative_points[:,0].max()) + # self.ax_latent_space.set_ylim(self.representative_points[:,1].min(), + # self.representative_points[:,1].max()) + if self.is_initial_view: + pass + else: + self.__draw_click_point() self.fig.show() def __draw_features(self): self.ax_features.cla() # self.__draw_latent_space() self.ax_features.bar(self.label_feature,self.clicked_features) - self.ax_features.set_title('Features') + self.ax_features.set_ylim(self.X.min(),self.X.max() * 1.05) + if self.is_initial_view: + self.ax_features.set_title('mean of data') + else: + self.ax_features.set_title('Features') self.ax_features.set_xticklabels(labels=self.label_feature,rotation=270) self.fig.show() @@ -270,6 +282,7 @@ def __draw_click_point(self): ".", color = "black", ms = 20, fillstyle = "none") def __onclick_fig(self,event): + self.is_initial_view = False if event.xdata is not None: if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか # クリックされた座標の取得 From ee8e4f915dc52b70156e0ae78e892f9d6a06c3dd Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 17:44:42 +0900 Subject: [PATCH 15/84] =?UTF-8?q?bar=E3=81=AE=E3=81=A9=E3=82=8C=E3=81=8B?= =?UTF-8?q?=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=97=E3=81=9F=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=81=A9=E3=81=AE=E3=83=90=E3=83=BC=E3=82=92=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=81=AE=E3=81=8B=E7=89=B9=E5=AE=9A=E3=81=99?= =?UTF-8?q?=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 874e4de..2bec53e 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -193,7 +193,7 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size else: raise ValueError('Not support is_compact=False') #create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point - self.clicked_features = self.X.mean(axis=0) + self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True self.representative_mapping = self.inverse_transform(self.representative_points) @@ -261,13 +261,13 @@ def __draw_latent_space(self): if self.is_initial_view: pass else: - self.__draw_click_point() + self.__draw_click_point_latent_space() self.fig.show() def __draw_features(self): self.ax_features.cla() # self.__draw_latent_space() - self.ax_features.bar(self.label_feature,self.clicked_features) + self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) self.ax_features.set_ylim(self.X.min(),self.X.max() * 1.05) if self.is_initial_view: self.ax_features.set_title('mean of data') @@ -276,7 +276,7 @@ def __draw_features(self): self.ax_features.set_xticklabels(labels=self.label_feature,rotation=270) self.fig.show() - def __draw_click_point(self): + def __draw_click_point_latent_space(self): coordinate = self.representative_points[self.click_point_latent_space] self.ax_latent_space.plot(coordinate[0],coordinate[1], ".", color = "black", ms = 20, fillstyle = "none") @@ -299,14 +299,22 @@ def __onclick_fig(self,event): self.__draw_features() elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 # not implemented yet - pass + print('clicked ax_features') + click_coordinates = np.array([event.xdata, event.ydata]) + for i, bar in enumerate(self.feature_bars): + if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: + print('pushed {}'.format(self.label_feature[i])) + self.selected_feature = i + + + def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.representative_points, click_point.reshape(1, -1)) index_nearest = np.argmin(distance) return index_nearest def __calc_features(self): - self.clicked_features = self.representative_mapping[self.click_point_latent_space, :] + self.clicked_mapping = self.representative_mapping[self.click_point_latent_space, :] From 06b8efd1954a50a5c6aff88f4374d949907eb9e0 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 18:17:37 +0900 Subject: [PATCH 16/84] =?UTF-8?q?select=E3=81=95=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E6=AC=A1=E5=85=83=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=81=A6?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93=E4=B8=8A=E3=81=A7pcolormes?= =?UTF-8?q?h=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E8=A9=A6?= =?UTF-8?q?=E4=BD=9C=20and=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 24 +++++++++++++++++-- tutorials/ukr/fitting_animal_data.py | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 2bec53e..9f9d232 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,7 +174,7 @@ def inverse_transform(self, Znew): return F - def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size=None): + def visualize(self, n_representative_points=30, label_data=None, label_feature=None, fig_size=None): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -188,13 +188,19 @@ def visualize(self, resolution=30, label_data=None, label_feature=None, fig_size # ---------------------------------- # # -----initialize variables--------- # # ---------------------------------- # + if isinstance(n_representative_points,int): + # 代表点の数を潜在空間の次元ごとに格納 + self.n_representative_points = np.ones(self.n_components, dtype='int8') * n_representative_points + else: + raise ValueError('Only support n_reprsentative_point is int') if self.is_compact: - self.representative_points = create_zeta(-1.0,1.0,self.n_components,resolution) + self.representative_points = create_zeta(-1.0, 1.0, self.n_components, n_representative_points) else: raise ValueError('Not support is_compact=False') #create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True + self.selected_feature = None self.representative_mapping = self.inverse_transform(self.representative_points) # invalid check @@ -261,6 +267,13 @@ def __draw_latent_space(self): if self.is_initial_view: pass else: + if self.selected_feature is not None: + values_selected_feature = self.representative_mapping[:,self.selected_feature] + values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) + representative_points_2d = self.__unflatten_representative_array(self.representative_points) + self.ax_latent_space.pcolormesh(representative_points_2d[:,:,0], + representative_points_2d[:,:,1], + values_selected_feature_2d) self.__draw_click_point_latent_space() self.fig.show() @@ -305,6 +318,7 @@ def __onclick_fig(self,event): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: print('pushed {}'.format(self.label_feature[i])) self.selected_feature = i + self.__draw_latent_space() @@ -316,6 +330,12 @@ def __calc_nearest_representative_point(self, click_point): def __calc_features(self): self.clicked_mapping = self.representative_mapping[self.click_point_latent_space, :] + def __unflatten_representative_array(self,representative_array): + if representative_array.shape[0] == np.prod(self.representative_points): + return np.squeeze(representative_array.reshape(np.append(self.n_representative_points,-1))) + else: + raise ValueError('arg shape {} is not consistent'.format(representative_array.shape)) + def create_zeta(zeta_min, zeta_max, latent_dim, resolution): diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index b4eb8b3..673489c 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -25,6 +25,6 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.visualize(resolution=resolution, + ukr.visualize(n_representative_points=resolution, label_data = labels_animal, label_feature=labels_feature) From 3b4ba56f93b84b7324b21aa5650d6fed263e695a Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 20 Apr 2020 18:30:06 +0900 Subject: [PATCH 17/84] =?UTF-8?q?bag=E3=82=92fix=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=A8=E3=82=8A=E3=81=82=E3=81=88=E3=81=9Apcolormesh?= =?UTF-8?q?=E3=81=8C=E8=A1=A8=E7=A4=BA=E3=81=A7=E3=81=8D=E3=82=8B=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 9f9d232..139e9a3 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -252,6 +252,13 @@ def visualize(self, n_representative_points=30, label_data=None, label_feature=N def __draw_latent_space(self): self.ax_latent_space.cla() + if self.selected_feature is not None: + values_selected_feature = self.representative_mapping[:, self.selected_feature] + values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) + representative_points_2d = self.__unflatten_representative_array(self.representative_points) + self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], + representative_points_2d[:, :, 1], + values_selected_feature_2d) self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) if self.label_data is None: pass @@ -267,13 +274,6 @@ def __draw_latent_space(self): if self.is_initial_view: pass else: - if self.selected_feature is not None: - values_selected_feature = self.representative_mapping[:,self.selected_feature] - values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) - representative_points_2d = self.__unflatten_representative_array(self.representative_points) - self.ax_latent_space.pcolormesh(representative_points_2d[:,:,0], - representative_points_2d[:,:,1], - values_selected_feature_2d) self.__draw_click_point_latent_space() self.fig.show() @@ -331,7 +331,7 @@ def __calc_features(self): self.clicked_mapping = self.representative_mapping[self.click_point_latent_space, :] def __unflatten_representative_array(self,representative_array): - if representative_array.shape[0] == np.prod(self.representative_points): + if representative_array.shape[0] == np.prod(self.n_representative_points): return np.squeeze(representative_array.reshape(np.append(self.n_representative_points,-1))) else: raise ValueError('arg shape {} is not consistent'.format(representative_array.shape)) From 8578df5b811dd25288114b9cc17419b82c0a1075 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:21:15 +0900 Subject: [PATCH 18/84] =?UTF-8?q?contour=E3=82=82=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= =?UTF-8?q?and=E5=A4=89=E6=95=B0=E3=81=AErefactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 139e9a3..eed536d 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,7 +174,7 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_representative_points=30, label_data=None, label_feature=None, fig_size=None): + def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_size=None): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -188,15 +188,15 @@ def visualize(self, n_representative_points=30, label_data=None, label_feature=N # ---------------------------------- # # -----initialize variables--------- # # ---------------------------------- # - if isinstance(n_representative_points,int): + if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 - self.n_representative_points = np.ones(self.n_components, dtype='int8') * n_representative_points + self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points else: raise ValueError('Only support n_reprsentative_point is int') if self.is_compact: - self.representative_points = create_zeta(-1.0, 1.0, self.n_components, n_representative_points) + self.representative_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) else: - raise ValueError('Not support is_compact=False') #create_zetaの整備が必要なので実装は後で + raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True @@ -238,7 +238,7 @@ def visualize(self, n_representative_points=30, label_data=None, label_feature=N self.ax_features.set_title('Mean of mapping') epsilon = 0.03 * np.abs(self.representative_points.max() - self.representative_points.min()) - self.noise_label = epsilon * (np.random.rand(self.n_samples,self.n_components) * 2.0 - 1.0) + self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) # ---------------------------------- # # -------------draw map------------- # # ---------------------------------- # @@ -256,17 +256,22 @@ def __draw_latent_space(self): values_selected_feature = self.representative_mapping[:, self.selected_feature] values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) representative_points_2d = self.__unflatten_representative_array(self.representative_points) - self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], - representative_points_2d[:, :, 1], - values_selected_feature_2d) - self.ax_latent_space.scatter(self.Z[:,0],self.Z[:,1]) + pcm = self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], + representative_points_2d[:, :, 1], + values_selected_feature_2d) + ctr = self.ax_latent_space.contour(representative_points_2d[:, :, 0], + representative_points_2d[:, :, 1], + values_selected_feature_2d,6,colors='k') + self.ax_latent_space.clabel(ctr) + # self.fig.colorbar(pcm, ax=self.ax_latent_space) + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1]) if self.label_data is None: pass else: - for z,noise,label in zip(self.Z, self.noise_label,self.label_data): + for z, noise, label in zip(self.Z, self.noise_label, self.label_data): point_label = z + noise - self.ax_latent_space.text(point_label[0] ,point_label[1],label, - ha='center', va='bottom',color='black') + self.ax_latent_space.text(point_label[0], point_label[1], label, + ha='center', va='bottom', color='black') # self.ax_latent_space.set_xlim(self.representative_points[:,0].min(), # self.representative_points[:,0].max()) # self.ax_latent_space.set_ylim(self.representative_points[:,1].min(), @@ -281,25 +286,25 @@ def __draw_features(self): self.ax_features.cla() # self.__draw_latent_space() self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) - self.ax_features.set_ylim(self.X.min(),self.X.max() * 1.05) + self.ax_features.set_ylim(self.X.min(), self.X.max() * 1.05) if self.is_initial_view: self.ax_features.set_title('mean of data') else: self.ax_features.set_title('Features') - self.ax_features.set_xticklabels(labels=self.label_feature,rotation=270) + self.ax_features.set_xticklabels(labels=self.label_feature, rotation=270) self.fig.show() def __draw_click_point_latent_space(self): coordinate = self.representative_points[self.click_point_latent_space] - self.ax_latent_space.plot(coordinate[0],coordinate[1], - ".", color = "black", ms = 20, fillstyle = "none") + self.ax_latent_space.plot(coordinate[0], coordinate[1], + ".", color="black", ms=20, fillstyle="none") - def __onclick_fig(self,event): + def __onclick_fig(self, event): self.is_initial_view = False if event.xdata is not None: if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか # クリックされた座標の取得 - click_coordinates = np.array([event.xdata,event.ydata]) + click_coordinates = np.array([event.xdata, event.ydata]) # クリックしたところといちばん近い代表点がどこかを計算 self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) @@ -312,16 +317,14 @@ def __onclick_fig(self,event): self.__draw_features() elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 # not implemented yet - print('clicked ax_features') + # print('clicked ax_features') click_coordinates = np.array([event.xdata, event.ydata]) for i, bar in enumerate(self.feature_bars): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: - print('pushed {}'.format(self.label_feature[i])) + # print('pushed {}'.format(self.label_feature[i])) self.selected_feature = i self.__draw_latent_space() - - def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.representative_points, click_point.reshape(1, -1)) index_nearest = np.argmin(distance) @@ -330,14 +333,13 @@ def __calc_nearest_representative_point(self, click_point): def __calc_features(self): self.clicked_mapping = self.representative_mapping[self.click_point_latent_space, :] - def __unflatten_representative_array(self,representative_array): - if representative_array.shape[0] == np.prod(self.n_representative_points): - return np.squeeze(representative_array.reshape(np.append(self.n_representative_points,-1))) + def __unflatten_representative_array(self, representative_array): + if representative_array.shape[0] == np.prod(self.n_grid_points): + return np.squeeze(representative_array.reshape(np.append(self.n_grid_points, -1))) else: raise ValueError('arg shape {} is not consistent'.format(representative_array.shape)) - def create_zeta(zeta_min, zeta_max, latent_dim, resolution): mesh1d, step = np.linspace(zeta_min, zeta_max, resolution, endpoint=False, retstep=True) mesh1d += step / 2.0 From de6a05c420eeb9643d88a207723cf7cbe36f9e58 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:22:44 +0900 Subject: [PATCH 19/84] =?UTF-8?q?refactor=E3=81=AB=E4=BC=B4=E3=81=86tutori?= =?UTF-8?q?al=20code=E5=86=85=E3=81=AE=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=20and=20=E5=88=9D=E6=9C=9F=E5=80=A4=E3=82=92?= =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E4=BB=BB=E3=81=9B=E3=81=A7=E3=81=AF?= =?UTF-8?q?=E3=81=AA=E3=81=8Fcode=E5=86=85=E3=81=A7=E6=8C=87=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 673489c..703dfb1 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -2,29 +2,31 @@ from libs.models.unsupervised_kernel_regression import UnsupervisedKernelRegression as UKR from libs.datasets.artificial import animal +from sklearn.utils import check_random_state if __name__ == '__main__': n_components = 2 - bandwidth = 0.5 + bandwidth = 0.4 lambda_ = 0.0 is_compact = True is_save_history = True - nb_epoch = 100 - eta = 5.0 + nb_epoch = 1000 + eta = 0.02 - resolution = 100 + n_grid_points = 100 X, labels_animal, labels_feature = animal.load_data(retlabel_animal=True, retlabel_feature=True) - seed = 13 - np.random.seed(seed) + seed = 12 + random_state = check_random_state(seed) + init = random_state.normal(0.0, bandwidth * 0.1, size=(X.shape[0], n_components)) ukr = UKR(X, n_components=n_components, bandwidth_gaussian_kernel=bandwidth, - is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_) + is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_, init=init) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.visualize(n_representative_points=resolution, - label_data = labels_animal, + ukr.visualize(n_grid_points=n_grid_points, + label_data=labels_animal, label_feature=labels_feature) From 2fff4d8bbe79c36e5d9908e22627f157c9dde859 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:31:54 +0900 Subject: [PATCH 20/84] =?UTF-8?q?=E9=81=B8=E6=8A=9E=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=82=8B=E7=89=B9=E5=BE=B4=E9=87=8F=E3=81=AE?= =?UTF-8?q?bar=E3=81=AE=E8=89=B2=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index eed536d..f0cfa7a 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -286,6 +286,8 @@ def __draw_features(self): self.ax_features.cla() # self.__draw_latent_space() self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) + if self.selected_feature is not None: + self.feature_bars[self.selected_feature].set_color('r') self.ax_features.set_ylim(self.X.min(), self.X.max() * 1.05) if self.is_initial_view: self.ax_features.set_title('mean of data') @@ -297,7 +299,7 @@ def __draw_features(self): def __draw_click_point_latent_space(self): coordinate = self.representative_points[self.click_point_latent_space] self.ax_latent_space.plot(coordinate[0], coordinate[1], - ".", color="black", ms=20, fillstyle="none") + ".", color="red", ms=20, fillstyle="none") def __onclick_fig(self, event): self.is_initial_view = False @@ -324,6 +326,7 @@ def __onclick_fig(self, event): # print('pushed {}'.format(self.label_feature[i])) self.selected_feature = i self.__draw_latent_space() + self.__draw_features() def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.representative_points, click_point.reshape(1, -1)) From 6b7a4730a2808e6d9d08ac46f22c106eaaae01a1 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:32:38 +0900 Subject: [PATCH 21/84] =?UTF-8?q?tutorial=E3=81=AEseed=E5=80=A4=E3=82=92?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 703dfb1..649a4f1 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -19,7 +19,7 @@ X, labels_animal, labels_feature = animal.load_data(retlabel_animal=True, retlabel_feature=True) - seed = 12 + seed = 14 random_state = check_random_state(seed) init = random_state.normal(0.0, bandwidth * 0.1, size=(X.shape[0], n_components)) From e89a9c25cd1db3a98fb5278bc8e80170c9e0ed9e Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:39:09 +0900 Subject: [PATCH 22/84] Refactor, reformat and delete unnecessary row --- libs/models/unsupervised_kernel_regression.py | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index f0cfa7a..ecca6d8 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -183,7 +183,6 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt - from matplotlib.widgets import RadioButtons # ---------------------------------- # # -----initialize variables--------- # @@ -194,7 +193,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s else: raise ValueError('Only support n_reprsentative_point is int') if self.is_compact: - self.representative_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) + self.grid_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) else: raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で self.click_point_latent_space = 0 # index of the clicked representative point @@ -202,7 +201,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s self.is_initial_view = True self.selected_feature = None - self.representative_mapping = self.inverse_transform(self.representative_points) + self.grid_mapping = self.inverse_transform(self.grid_points) # invalid check if label_data is None: self.label_data = label_data @@ -237,7 +236,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s self.ax_features = self.fig.add_subplot(1, 2, 2) self.ax_features.set_title('Mean of mapping') - epsilon = 0.03 * np.abs(self.representative_points.max() - self.representative_points.min()) + epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) # ---------------------------------- # # -------------draw map------------- # @@ -253,17 +252,16 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s def __draw_latent_space(self): self.ax_latent_space.cla() if self.selected_feature is not None: - values_selected_feature = self.representative_mapping[:, self.selected_feature] + values_selected_feature = self.grid_mapping[:, self.selected_feature] values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) - representative_points_2d = self.__unflatten_representative_array(self.representative_points) + representative_points_2d = self.__unflatten_representative_array(self.grid_points) pcm = self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], representative_points_2d[:, :, 1], values_selected_feature_2d) ctr = self.ax_latent_space.contour(representative_points_2d[:, :, 0], representative_points_2d[:, :, 1], - values_selected_feature_2d,6,colors='k') + values_selected_feature_2d, 6, colors='k') self.ax_latent_space.clabel(ctr) - # self.fig.colorbar(pcm, ax=self.ax_latent_space) self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1]) if self.label_data is None: pass @@ -272,10 +270,6 @@ def __draw_latent_space(self): point_label = z + noise self.ax_latent_space.text(point_label[0], point_label[1], label, ha='center', va='bottom', color='black') - # self.ax_latent_space.set_xlim(self.representative_points[:,0].min(), - # self.representative_points[:,0].max()) - # self.ax_latent_space.set_ylim(self.representative_points[:,1].min(), - # self.representative_points[:,1].max()) if self.is_initial_view: pass else: @@ -284,7 +278,6 @@ def __draw_latent_space(self): def __draw_features(self): self.ax_features.cla() - # self.__draw_latent_space() self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) if self.selected_feature is not None: self.feature_bars[self.selected_feature].set_color('r') @@ -297,7 +290,7 @@ def __draw_features(self): self.fig.show() def __draw_click_point_latent_space(self): - coordinate = self.representative_points[self.click_point_latent_space] + coordinate = self.grid_points[self.click_point_latent_space] self.ax_latent_space.plot(coordinate[0], coordinate[1], ".", color="red", ms=20, fillstyle="none") @@ -318,23 +311,20 @@ def __onclick_fig(self, event): self.__draw_latent_space() self.__draw_features() elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 - # not implemented yet - # print('clicked ax_features') click_coordinates = np.array([event.xdata, event.ydata]) for i, bar in enumerate(self.feature_bars): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: - # print('pushed {}'.format(self.label_feature[i])) self.selected_feature = i self.__draw_latent_space() self.__draw_features() def __calc_nearest_representative_point(self, click_point): - distance = dist.cdist(self.representative_points, click_point.reshape(1, -1)) + distance = dist.cdist(self.grid_points, click_point.reshape(1, -1)) index_nearest = np.argmin(distance) return index_nearest def __calc_features(self): - self.clicked_mapping = self.representative_mapping[self.click_point_latent_space, :] + self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] def __unflatten_representative_array(self, representative_array): if representative_array.shape[0] == np.prod(self.n_grid_points): From f162f3bea05bbf817c13ab9f73e20ee2bcdeb94f Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 21 Apr 2020 11:40:20 +0900 Subject: [PATCH 23/84] Refactor --- libs/models/unsupervised_kernel_regression.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index ecca6d8..c112098 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -253,8 +253,8 @@ def __draw_latent_space(self): self.ax_latent_space.cla() if self.selected_feature is not None: values_selected_feature = self.grid_mapping[:, self.selected_feature] - values_selected_feature_2d = self.__unflatten_representative_array(values_selected_feature) - representative_points_2d = self.__unflatten_representative_array(self.grid_points) + values_selected_feature_2d = self.__unflatten_grid_array(values_selected_feature) + representative_points_2d = self.__unflatten_grid_array(self.grid_points) pcm = self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], representative_points_2d[:, :, 1], values_selected_feature_2d) @@ -326,11 +326,11 @@ def __calc_nearest_representative_point(self, click_point): def __calc_features(self): self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] - def __unflatten_representative_array(self, representative_array): - if representative_array.shape[0] == np.prod(self.n_grid_points): - return np.squeeze(representative_array.reshape(np.append(self.n_grid_points, -1))) + def __unflatten_grid_array(self, grid_array): + if grid_array.shape[0] == np.prod(self.n_grid_points): + return np.squeeze(grid_array.reshape(np.append(self.n_grid_points, -1))) else: - raise ValueError('arg shape {} is not consistent'.format(representative_array.shape)) + raise ValueError('arg shape {} is not consistent'.format(grid_array.shape)) def create_zeta(zeta_min, zeta_max, latent_dim, resolution): From 20f02dd23ca5d38a00d615514c1948cd82dd9e79 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 09:05:30 +0900 Subject: [PATCH 24/84] =?UTF-8?q?UKR=E3=81=AE=E5=A4=89=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E5=88=9D=E6=9C=9F=E5=8C=96=E3=82=92=E5=88=A5=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=A8=E3=81=97=E3=81=A6=E5=AE=9A=E7=BE=A9?= =?UTF-8?q?=E3=81=97=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index c112098..c9b79f0 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -187,6 +187,21 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s # ---------------------------------- # # -----initialize variables--------- # # ---------------------------------- # + + self.__initialize_var_to_visualize(n_grid_points,label_data,label_feature,fig_size) + + # ---------------------------------- # + # -------------draw map------------- # + # ---------------------------------- # + + self.__draw_latent_space() + self.__draw_features() + + # connect figure and method defining action when latent space is clicked + self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) + plt.show() + + def __initialize_var_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points @@ -238,16 +253,6 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) - # ---------------------------------- # - # -------------draw map------------- # - # ---------------------------------- # - - self.__draw_latent_space() - self.__draw_features() - - # connect figure and method defining action when latent space is clicked - self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) - plt.show() def __draw_latent_space(self): self.ax_latent_space.cla() From 13393e706340491acb7986e0b58106fa070b9d77 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 09:11:33 +0900 Subject: [PATCH 25/84] Fix --- libs/models/unsupervised_kernel_regression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index c9b79f0..60b28ed 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -202,6 +202,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s plt.show() def __initialize_var_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): + import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points From 44490c51d72a02d3c0269e93632a334f9482b63e Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 09:12:40 +0900 Subject: [PATCH 26/84] Refactor --- libs/models/unsupervised_kernel_regression.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 60b28ed..06a6064 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -188,7 +188,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s # -----initialize variables--------- # # ---------------------------------- # - self.__initialize_var_to_visualize(n_grid_points,label_data,label_feature,fig_size) + self.__initialize_to_visualize(n_grid_points, label_data, label_feature, fig_size) # ---------------------------------- # # -------------draw map------------- # @@ -201,7 +201,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) plt.show() - def __initialize_var_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): + def __initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 From 825ae819a035581b64ac1219d4c68a46396655a0 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 09:15:02 +0900 Subject: [PATCH 27/84] Edit error message --- libs/models/unsupervised_kernel_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 06a6064..6057c52 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -207,7 +207,7 @@ def __initialize_to_visualize(self, n_grid_points, label_data, label_feature, fi # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points else: - raise ValueError('Only support n_reprsentative_point is int') + raise ValueError('Only support n_grid_points is int') if self.is_compact: self.grid_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) else: From 5190eb54316b8ff08e3071a4a56f5f8dcc227838 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 15:55:46 +0900 Subject: [PATCH 28/84] First commit From eab1446a8396cbc8cdfe6a051e7606268d850f0d Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 16:18:00 +0900 Subject: [PATCH 29/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E5=8F=82=E7=85=A7=E3=81=95=E3=82=8C=E3=82=8B=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E6=80=A7=E3=81=AE=E3=81=82=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AE=E5=85=88=E9=A0=AD=E3=82=92=5F=5F=E3=81=8B?= =?UTF-8?q?=E3=82=89=5F=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6057c52..3233bdf 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -188,20 +188,20 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s # -----initialize variables--------- # # ---------------------------------- # - self.__initialize_to_visualize(n_grid_points, label_data, label_feature, fig_size) + self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig_size) # ---------------------------------- # # -------------draw map------------- # # ---------------------------------- # - self.__draw_latent_space() - self.__draw_features() + self._draw_latent_space() + self._draw_features() # connect figure and method defining action when latent space is clicked self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) plt.show() - def __initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): + def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 @@ -255,7 +255,7 @@ def __initialize_to_visualize(self, n_grid_points, label_data, label_feature, fi epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) - def __draw_latent_space(self): + def _draw_latent_space(self): self.ax_latent_space.cla() if self.selected_feature is not None: values_selected_feature = self.grid_mapping[:, self.selected_feature] @@ -282,7 +282,7 @@ def __draw_latent_space(self): self.__draw_click_point_latent_space() self.fig.show() - def __draw_features(self): + def _draw_features(self): self.ax_features.cla() self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) if self.selected_feature is not None: @@ -314,15 +314,15 @@ def __onclick_fig(self, event): self.__calc_features() # その特徴量の値を描画 - self.__draw_latent_space() - self.__draw_features() + self._draw_latent_space() + self._draw_features() elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 click_coordinates = np.array([event.xdata, event.ydata]) for i, bar in enumerate(self.feature_bars): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: self.selected_feature = i - self.__draw_latent_space() - self.__draw_features() + self._draw_latent_space() + self._draw_features() def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.grid_points, click_point.reshape(1, -1)) From 5dc565a23ddff3a09fae0c4457ae9e4ceb4248c9 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 16:22:45 +0900 Subject: [PATCH 30/84] Delete unnecessary comments --- libs/models/unsupervised_kernel_regression.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 3233bdf..29f910d 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -184,16 +184,8 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s matplotlib.use('TkAgg') import matplotlib.pyplot as plt - # ---------------------------------- # - # -----initialize variables--------- # - # ---------------------------------- # - self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig_size) - # ---------------------------------- # - # -------------draw map------------- # - # ---------------------------------- # - self._draw_latent_space() self._draw_features() From 08be578b13f599bff0e365b88df4351806156d9c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 16:52:04 +0900 Subject: [PATCH 31/84] =?UTF-8?q?=E6=8F=8F=E7=94=BB=E3=81=AE=E7=B4=B0?= =?UTF-8?q?=E3=81=8B=E3=81=84=E6=9D=A1=E4=BB=B6=E3=81=AA=E3=81=A9=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=81=E7=A0=B4=E5=A3=8A=E7=9A=84=E3=81=AA?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=81=93=E3=81=A8=E3=81=AF=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 29f910d..93aebde 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -208,6 +208,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True self.selected_feature = None + self.grid_values_to_draw = None self.grid_mapping = self.inverse_transform(self.grid_points) # invalid check @@ -249,16 +250,17 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig def _draw_latent_space(self): self.ax_latent_space.cla() - if self.selected_feature is not None: - values_selected_feature = self.grid_mapping[:, self.selected_feature] - values_selected_feature_2d = self.__unflatten_grid_array(values_selected_feature) - representative_points_2d = self.__unflatten_grid_array(self.grid_points) - pcm = self.ax_latent_space.pcolormesh(representative_points_2d[:, :, 0], - representative_points_2d[:, :, 1], - values_selected_feature_2d) - ctr = self.ax_latent_space.contour(representative_points_2d[:, :, 0], - representative_points_2d[:, :, 1], - values_selected_feature_2d, 6, colors='k') + if self.grid_values_to_draw is not None: + #self.grid_values_to_draw = self.grid_mapping[:, self.selected_feature] + # To draw by pcolormesh and contour, reshape arrays like grid + grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) + grid_points_3d = self.__unflatten_grid_array(self.grid_points) + pcm = self.ax_latent_space.pcolormesh(grid_points_3d[:, :, 0], + grid_points_3d[:, :, 1], + grid_values_to_draw_3d) + ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], + grid_points_3d[:, :, 1], + grid_values_to_draw_3d, 6, colors='k') self.ax_latent_space.clabel(ctr) self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1]) if self.label_data is None: @@ -313,6 +315,7 @@ def __onclick_fig(self, event): for i, bar in enumerate(self.feature_bars): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: self.selected_feature = i + self.grid_values_to_draw = self.grid_mapping[:,i] self._draw_latent_space() self._draw_features() From 0234a6c92f2f97da8f4e5964a4978c2cda28211c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 17:17:54 +0900 Subject: [PATCH 32/84] =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E5=8D=98=E4=BD=8D=E3=81=AB=E5=88=86?= =?UTF-8?q?=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 93aebde..bcd2cee 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -204,7 +204,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig self.grid_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) else: raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で - self.click_point_latent_space = 0 # index of the clicked representative point + self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True self.selected_feature = None @@ -243,7 +243,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') self.ax_latent_space.set_title('Latent space') self.ax_features = self.fig.add_subplot(1, 2, 2) - self.ax_features.set_title('Mean of mapping') + self.ax_features.set_title('Mean of data') epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) @@ -270,7 +270,7 @@ def _draw_latent_space(self): point_label = z + noise self.ax_latent_space.text(point_label[0], point_label[1], label, ha='center', va='bottom', color='black') - if self.is_initial_view: + if self.click_point_latent_space is None: pass else: self.__draw_click_point_latent_space() @@ -282,7 +282,7 @@ def _draw_features(self): if self.selected_feature is not None: self.feature_bars[self.selected_feature].set_color('r') self.ax_features.set_ylim(self.X.min(), self.X.max() * 1.05) - if self.is_initial_view: + if self.click_point_latent_space is None: self.ax_features.set_title('mean of data') else: self.ax_features.set_title('Features') @@ -297,36 +297,35 @@ def __draw_click_point_latent_space(self): def __onclick_fig(self, event): self.is_initial_view = False if event.xdata is not None: + # クリックされた座標の取得 + click_coordinates = np.array([event.xdata, event.ydata]) if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか - # クリックされた座標の取得 - click_coordinates = np.array([event.xdata, event.ydata]) + self.__set_feature_bar_from_latent_space(click_coordinates) + self._draw_latent_space() + self._draw_features() + elif event.inaxes == self.ax_features.axes: # 特徴量のバーがクリックされたかどうか + self.__set_latent_space_from_feature_bar(click_coordinates) + self._draw_latent_space() + self._draw_features() - # クリックしたところといちばん近い代表点がどこかを計算 - self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) + def __set_feature_bar_from_latent_space(self, click_coordinates): + # クリックしたところといちばん近い代表点がどこかを計算 + self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) - # その代表点の写像先の特徴量を計算 - self.__calc_features() + # その代表点の写像先の特徴量を計算 + self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] - # その特徴量の値を描画 - self._draw_latent_space() - self._draw_features() - elif event.inaxes == self.ax_features.axes: # map2がクリックされた時 - click_coordinates = np.array([event.xdata, event.ydata]) - for i, bar in enumerate(self.feature_bars): - if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: - self.selected_feature = i - self.grid_values_to_draw = self.grid_mapping[:,i] - self._draw_latent_space() - self._draw_features() + def __set_latent_space_from_feature_bar(self, click_coordinates): + for i, bar in enumerate(self.feature_bars): + if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: + self.selected_feature = i + self.grid_values_to_draw = self.grid_mapping[:, i] def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.grid_points, click_point.reshape(1, -1)) index_nearest = np.argmin(distance) return index_nearest - def __calc_features(self): - self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] - def __unflatten_grid_array(self, grid_array): if grid_array.shape[0] == np.prod(self.n_grid_points): return np.squeeze(grid_array.reshape(np.append(self.n_grid_points, -1))) From d94a1ae8ca4d20b865b55230316e377bfb8745c0 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 17:20:17 +0900 Subject: [PATCH 33/84] Refactor --- libs/models/unsupervised_kernel_regression.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index bcd2cee..5e71685 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -242,8 +242,8 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig self.fig = plt.figure(figsize=fig_size) self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') self.ax_latent_space.set_title('Latent space') - self.ax_features = self.fig.add_subplot(1, 2, 2) - self.ax_features.set_title('Mean of data') + self.ax_feature_bars = self.fig.add_subplot(1, 2, 2) + self.ax_feature_bars.set_title('Mean of data') epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) @@ -277,16 +277,16 @@ def _draw_latent_space(self): self.fig.show() def _draw_features(self): - self.ax_features.cla() - self.feature_bars = self.ax_features.bar(self.label_feature, self.clicked_mapping) + self.ax_feature_bars.cla() + self.feature_bars = self.ax_feature_bars.bar(self.label_feature, self.clicked_mapping) if self.selected_feature is not None: self.feature_bars[self.selected_feature].set_color('r') - self.ax_features.set_ylim(self.X.min(), self.X.max() * 1.05) + self.ax_feature_bars.set_ylim(self.X.min(), self.X.max() * 1.05) if self.click_point_latent_space is None: - self.ax_features.set_title('mean of data') + self.ax_feature_bars.set_title('mean of data') else: - self.ax_features.set_title('Features') - self.ax_features.set_xticklabels(labels=self.label_feature, rotation=270) + self.ax_feature_bars.set_title('Features') + self.ax_feature_bars.set_xticklabels(labels=self.label_feature, rotation=270) self.fig.show() def __draw_click_point_latent_space(self): @@ -303,7 +303,7 @@ def __onclick_fig(self, event): self.__set_feature_bar_from_latent_space(click_coordinates) self._draw_latent_space() self._draw_features() - elif event.inaxes == self.ax_features.axes: # 特徴量のバーがクリックされたかどうか + elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか self.__set_latent_space_from_feature_bar(click_coordinates) self._draw_latent_space() self._draw_features() From 07a10e079fac379862ba8dc95ed5207eff5b1c0c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 17:30:14 +0900 Subject: [PATCH 34/84] =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=AE=E5=88=9D?= =?UTF-8?q?=E6=9C=9F=E5=8C=96=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=A7?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89figure=E3=82=92=E4=BB=A3?= =?UTF-8?q?=E5=85=A5=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 5e71685..6efe842 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -193,7 +193,8 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) plt.show() - def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig_size): + def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, + fig_size, ax_latent_space=None, ax_feature_bars=None): import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 @@ -236,14 +237,18 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig else: raise ValueError('label_feature must be 1d array or list') - if fig_size is None: - self.fig = plt.figure(figsize=(15, 6)) + if ax_latent_space is None and ax_feature_bars is None: + if fig_size is None: + self.fig = plt.figure(figsize=(15, 6)) + else: + self.fig = plt.figure(figsize=fig_size) + self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') + self.ax_latent_space.set_title('Latent space') + self.ax_feature_bars = self.fig.add_subplot(1, 2, 2) + self.ax_feature_bars.set_title('Mean of data') else: - self.fig = plt.figure(figsize=fig_size) - self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') - self.ax_latent_space.set_title('Latent space') - self.ax_feature_bars = self.fig.add_subplot(1, 2, 2) - self.ax_feature_bars.set_title('Mean of data') + self.ax_latent_space = ax_latent_space + self.ax_feature_bars = ax_feature_bars epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) From fe3d1eb3eb1792a65676f0120e91d7879710933b Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 17:52:34 +0900 Subject: [PATCH 35/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89figur?= =?UTF-8?q?e=E3=82=84ax=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3=E3=80=82?= =?UTF-8?q?tutorial=20code=E3=81=A7=E5=AE=9F=E8=A1=8C=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=81=93=E3=81=A8=E3=82=92=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 12 ++++++++---- tutorials/ukr/fitting_animal_data.py | 14 +++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6efe842..35b6c93 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,7 +174,7 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_size=None): + def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -184,7 +184,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s matplotlib.use('TkAgg') import matplotlib.pyplot as plt - self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig_size) + self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() self._draw_features() @@ -194,7 +194,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig_s plt.show() def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, - fig_size, ax_latent_space=None, ax_feature_bars=None): + fig, fig_size, ax_latent_space, ax_feature_bars): import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 @@ -237,11 +237,15 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, else: raise ValueError('label_feature must be 1d array or list') - if ax_latent_space is None and ax_feature_bars is None: + if fig is None: if fig_size is None: self.fig = plt.figure(figsize=(15, 6)) else: self.fig = plt.figure(figsize=fig_size) + else: + self.fig = fig + + if ax_latent_space is None and ax_feature_bars is None: self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') self.ax_latent_space.set_title('Latent space') self.ax_feature_bars = self.fig.add_subplot(1, 2, 2) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 649a4f1..1259859 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -1,9 +1,11 @@ -import numpy as np - from libs.models.unsupervised_kernel_regression import UnsupervisedKernelRegression as UKR from libs.datasets.artificial import animal from sklearn.utils import check_random_state +import matplotlib +matplotlib.use('TkAgg') +import matplotlib.pyplot as plt + if __name__ == '__main__': n_components = 2 bandwidth = 0.4 @@ -27,6 +29,12 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_, init=init) ukr.fit(nb_epoch=nb_epoch, eta=eta) + fig = plt.figure(figsize=[14,7]) + ax_latent_space = fig.add_subplot(121,aspect='equal') + ax_feature_bars = fig.add_subplot(122) ukr.visualize(n_grid_points=n_grid_points, label_data=labels_animal, - label_feature=labels_feature) + label_feature=labels_feature, + fig=fig, + ax_latent_space=ax_latent_space, + ax_feature_bars=ax_feature_bars) From deceb522c823db1fdad0bc2a6e7a3abea15e8900 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 22 Apr 2020 19:15:01 +0900 Subject: [PATCH 36/84] =?UTF-8?q?tutorial=E3=82=92=E5=85=83=E3=81=AB?= =?UTF-8?q?=E6=88=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 1259859..22d2ab1 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -29,12 +29,6 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_, init=init) ukr.fit(nb_epoch=nb_epoch, eta=eta) - fig = plt.figure(figsize=[14,7]) - ax_latent_space = fig.add_subplot(121,aspect='equal') - ax_feature_bars = fig.add_subplot(122) ukr.visualize(n_grid_points=n_grid_points, label_data=labels_animal, - label_feature=labels_feature, - fig=fig, - ax_latent_space=ax_latent_space, - ax_feature_bars=ax_feature_bars) + label_feature=labels_feature) From 14bac9a57c5348197eb1cee28999f04a594aa79a Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 23 Apr 2020 11:35:05 +0900 Subject: [PATCH 37/84] Refractor --- libs/models/unsupervised_kernel_regression.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 35b6c93..5b5d65a 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -309,22 +309,22 @@ def __onclick_fig(self, event): # クリックされた座標の取得 click_coordinates = np.array([event.xdata, event.ydata]) if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか - self.__set_feature_bar_from_latent_space(click_coordinates) + self._set_feature_bar_from_latent_space(click_coordinates) self._draw_latent_space() self._draw_features() elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか - self.__set_latent_space_from_feature_bar(click_coordinates) + self._set_latent_space_from_feature_bar(click_coordinates) self._draw_latent_space() self._draw_features() - def __set_feature_bar_from_latent_space(self, click_coordinates): + def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) # その代表点の写像先の特徴量を計算 self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] - def __set_latent_space_from_feature_bar(self, click_coordinates): + def _set_latent_space_from_feature_bar(self, click_coordinates): for i, bar in enumerate(self.feature_bars): if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: self.selected_feature = i From 8ed6c71ad970322a15f570fc77ba4f0bacbab21b Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 23 Apr 2020 12:15:32 +0900 Subject: [PATCH 38/84] =?UTF-8?q?Refactor=20and=20=E5=BE=8C=E3=81=8B?= =?UTF-8?q?=E3=82=89=E3=82=B3=E3=83=BC=E3=83=89=E8=A6=8B=E3=82=84=E3=81=99?= =?UTF-8?q?=E3=81=84=E3=82=88=E3=81=86=E3=81=AB=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AE=E5=AE=9A=E7=BE=A9=E3=81=AE=E9=A0=86=E7=95=AA?= =?UTF-8?q?=E3=82=92=E5=85=A5=E3=82=8C=E6=9B=BF=E3=81=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 5b5d65a..f1b5c4e 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -187,7 +187,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=N self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() - self._draw_features() + self._draw_feature_bars() # connect figure and method defining action when latent space is clicked self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) @@ -257,6 +257,33 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) + def __onclick_fig(self, event): + self.is_initial_view = False + if event.xdata is not None: + # クリックされた座標の取得 + click_coordinates = np.array([event.xdata, event.ydata]) + if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか + self._set_feature_bar_from_latent_space(click_coordinates) + self._draw_latent_space() + self._draw_feature_bars() + elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか + self._set_latent_space_from_feature_bar(click_coordinates) + self._draw_latent_space() + self._draw_feature_bars() + + def _set_feature_bar_from_latent_space(self, click_coordinates): + # クリックしたところといちばん近い代表点がどこかを計算 + self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) + + # その代表点の写像先の特徴量を計算 + self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] + + def _set_latent_space_from_feature_bar(self, click_coordinates): + for i, bar in enumerate(self.feature_bars): + if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: + self.selected_feature = i + self.grid_values_to_draw = self.grid_mapping[:, i] + def _draw_latent_space(self): self.ax_latent_space.cla() if self.grid_values_to_draw is not None: @@ -285,7 +312,7 @@ def _draw_latent_space(self): self.__draw_click_point_latent_space() self.fig.show() - def _draw_features(self): + def _draw_feature_bars(self): self.ax_feature_bars.cla() self.feature_bars = self.ax_feature_bars.bar(self.label_feature, self.clicked_mapping) if self.selected_feature is not None: @@ -303,32 +330,6 @@ def __draw_click_point_latent_space(self): self.ax_latent_space.plot(coordinate[0], coordinate[1], ".", color="red", ms=20, fillstyle="none") - def __onclick_fig(self, event): - self.is_initial_view = False - if event.xdata is not None: - # クリックされた座標の取得 - click_coordinates = np.array([event.xdata, event.ydata]) - if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか - self._set_feature_bar_from_latent_space(click_coordinates) - self._draw_latent_space() - self._draw_features() - elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか - self._set_latent_space_from_feature_bar(click_coordinates) - self._draw_latent_space() - self._draw_features() - - def _set_feature_bar_from_latent_space(self, click_coordinates): - # クリックしたところといちばん近い代表点がどこかを計算 - self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) - - # その代表点の写像先の特徴量を計算 - self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] - - def _set_latent_space_from_feature_bar(self, click_coordinates): - for i, bar in enumerate(self.feature_bars): - if click_coordinates[0] > bar._x0 and click_coordinates[0] < bar._x1: - self.selected_feature = i - self.grid_values_to_draw = self.grid_mapping[:, i] def __calc_nearest_representative_point(self, click_point): distance = dist.cdist(self.grid_points, click_point.reshape(1, -1)) From c5f998c90bc85d8127e555e1fbaf395821c586cf Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 23 Apr 2020 14:54:06 +0900 Subject: [PATCH 39/84] =?UTF-8?q?plt=E3=81=AEimport=E3=81=AE=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index f1b5c4e..6874632 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -195,7 +195,6 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=N def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars): - import matplotlib.pyplot as plt if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points @@ -238,6 +237,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, raise ValueError('label_feature must be 1d array or list') if fig is None: + import matplotlib.pyplot as plt if fig_size is None: self.fig = plt.figure(figsize=(15, 6)) else: From 4f4130707cb9bae612159f2914bc0ebf6d1b66d4 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 23 Apr 2020 16:48:50 +0900 Subject: [PATCH 40/84] =?UTF-8?q?tutorial=E3=81=AE=E4=BD=99=E5=88=86?= =?UTF-8?q?=E3=81=AAimport=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 22d2ab1..85acb3e 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -2,10 +2,6 @@ from libs.datasets.artificial import animal from sklearn.utils import check_random_state -import matplotlib -matplotlib.use('TkAgg') -import matplotlib.pyplot as plt - if __name__ == '__main__': n_components = 2 bandwidth = 0.4 From 77e76d00f3ce86f29c74a69c0074650d75cf5c41 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 13:07:58 +0900 Subject: [PATCH 41/84] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E8=AA=AD?= =?UTF-8?q?=E3=81=BF=E3=82=84=E3=81=99=E3=81=8F=E3=81=AA=E3=82=8B=E3=81=93?= =?UTF-8?q?=E3=81=A8=E3=82=92=E9=A1=98=E3=81=A3=E3=81=A6=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E5=AE=9A=E7=BE=A9=E3=81=AE=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6874632..bbfbe60 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -193,6 +193,20 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=N self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) plt.show() + def __onclick_fig(self, event): + self.is_initial_view = False + if event.xdata is not None: + # クリックされた座標の取得 + click_coordinates = np.array([event.xdata, event.ydata]) + if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか + self._set_feature_bar_from_latent_space(click_coordinates) + self._draw_latent_space() + self._draw_feature_bars() + elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか + self._set_latent_space_from_feature_bar(click_coordinates) + self._draw_latent_space() + self._draw_feature_bars() + def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars): if isinstance(n_grid_points, int): @@ -257,19 +271,6 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) - def __onclick_fig(self, event): - self.is_initial_view = False - if event.xdata is not None: - # クリックされた座標の取得 - click_coordinates = np.array([event.xdata, event.ydata]) - if event.inaxes == self.ax_latent_space.axes: # 潜在空間をクリックしたかどうか - self._set_feature_bar_from_latent_space(click_coordinates) - self._draw_latent_space() - self._draw_feature_bars() - elif event.inaxes == self.ax_feature_bars.axes: # 特徴量のバーがクリックされたかどうか - self._set_latent_space_from_feature_bar(click_coordinates) - self._draw_latent_space() - self._draw_feature_bars() def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 From 41f6ffc88c08a1d030e6a82caaee18d9fa063152 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 13:24:47 +0900 Subject: [PATCH 42/84] Tweak position of defining variables and comment --- libs/models/unsupervised_kernel_regression.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index bbfbe60..7db2531 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -175,11 +175,8 @@ def inverse_transform(self, Znew): return F def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): - # invalid check - if self.n_components != 2: - raise ValueError('Now support only n_components = 2') - # import necessary library to draw + # import library to draw import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt @@ -209,6 +206,10 @@ def __onclick_fig(self, event): def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars): + # invalid check + if self.n_components != 2: + raise ValueError('Now support only n_components = 2') + if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points @@ -218,14 +219,8 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.grid_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) else: raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で - self.click_point_latent_space = None # index of the clicked representative point - self.clicked_mapping = self.X.mean(axis=0) - self.is_initial_view = True - self.selected_feature = None - self.grid_values_to_draw = None self.grid_mapping = self.inverse_transform(self.grid_points) - # invalid check if label_data is None: self.label_data = label_data elif isinstance(label_data, list): @@ -250,6 +245,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, else: raise ValueError('label_feature must be 1d array or list') + # set variables if fig is None: import matplotlib.pyplot as plt if fig_size is None: @@ -268,6 +264,12 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.ax_latent_space = ax_latent_space self.ax_feature_bars = ax_feature_bars + self.click_point_latent_space = None # index of the clicked representative point + self.clicked_mapping = self.X.mean(axis=0) + self.is_initial_view = True + self.selected_feature = None + self.grid_values_to_draw = None + epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) From ff41f781b1631327159381bd8c8680f0f540c9c7 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 17:44:57 +0900 Subject: [PATCH 43/84] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=81=A7=E6=BD=9C=E5=9C=A8=E5=A4=89=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=83=A9=E3=83=99=E3=83=AB=E3=82=92=E3=83=9E=E3=82=A6=E3=82=B9?= =?UTF-8?q?=E3=82=AA=E3=83=BC=E3=83=90=E3=83=BC=E3=81=97=E3=81=9F=E6=99=82?= =?UTF-8?q?=E3=81=AB=E8=A1=A8=E7=A4=BA=E3=81=A7=E3=81=8D=E3=82=8B=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=E3=82=92=E8=BF=BD=E5=8A=A0=EF=BC=88=E6=9C=AA=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 67 +++++++++++++++---- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 7db2531..33b6d18 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,20 +174,24 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): + def visualize(self, n_grid_points=30, label_data=None, label_feature=None, is_show_all_label_data=False, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): # import library to draw import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt - self._initialize_to_visualize(n_grid_points, label_data, label_feature, fig, fig_size, ax_latent_space, ax_feature_bars) + self._initialize_to_visualize(n_grid_points, label_data, label_feature, is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() self._draw_feature_bars() # connect figure and method defining action when latent space is clicked self.fig.canvas.mpl_connect('button_press_event', self.__onclick_fig) + # connect figure and method defining action when mouse over + if self.label_data is not None and self.is_show_all_label_data is False: + self.fig.canvas.mpl_connect('motion_notify_event', self.__mouse_over_fig) + # self.fig.canvas.mpl_connect('axes_leave_event', self.__mouse_leave_fig) plt.show() def __onclick_fig(self, event): @@ -204,7 +208,20 @@ def __onclick_fig(self, event): self._draw_latent_space() self._draw_feature_bars() - def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, + def __mouse_over_fig(self, event): + if event.xdata is not None: + # クリックされた座標の取得 + over_coordinates = np.array([event.xdata, event.ydata]) + if event.inaxes == self.ax_latent_space.axes: + self._set_shown_label_in_latent_space(over_coordinates) + self._draw_latent_space() + self._draw_feature_bars() + + elif event.inaxes == self.ax_feature_bars: + pass + + + def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars): # invalid check if self.n_components != 2: @@ -245,6 +262,8 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, else: raise ValueError('label_feature must be 1d array or list') + self.is_show_all_label_data = is_show_all_label_data + # set variables if fig is None: import matplotlib.pyplot as plt @@ -269,6 +288,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.is_initial_view = True self.selected_feature = None self.grid_values_to_draw = None + self.threshold_radius_show = np.abs(self.grid_points.max() - self.grid_points.min()) * 0.05 epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) @@ -276,7 +296,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 - self.click_point_latent_space = self.__calc_nearest_representative_point(click_coordinates) + self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) # その代表点の写像先の特徴量を計算 self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] @@ -287,6 +307,13 @@ def _set_latent_space_from_feature_bar(self, click_coordinates): self.selected_feature = i self.grid_values_to_draw = self.grid_mapping[:, i] + def _set_shown_label_in_latent_space(self, click_coordinates): + index, dist = self.__calc_nearest_latent_variable(click_coordinates,retdist=True) + if dist <= self.threshold_radius_show: + self.index_data_label_shown = index + else: + self.index_data_label_shown = None + def _draw_latent_space(self): self.ax_latent_space.cla() if self.grid_values_to_draw is not None: @@ -305,10 +332,18 @@ def _draw_latent_space(self): if self.label_data is None: pass else: - for z, noise, label in zip(self.Z, self.noise_label, self.label_data): - point_label = z + noise - self.ax_latent_space.text(point_label[0], point_label[1], label, - ha='center', va='bottom', color='black') + if self.is_show_all_label_data: + for z, noise, label in zip(self.Z, self.noise_label, self.label_data): + point_label = z + noise + self.ax_latent_space.text(point_label[0], point_label[1], label, + ha='center', va='bottom', color='black') + else: + if self.index_data_label_shown is not None: + point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] + self.ax_latent_space.text(point_label[0], point_label[1], label, + ha='center', va='bottom', color='black') + else: + pass if self.click_point_latent_space is None: pass else: @@ -333,12 +368,20 @@ def __draw_click_point_latent_space(self): self.ax_latent_space.plot(coordinate[0], coordinate[1], ".", color="red", ms=20, fillstyle="none") - - def __calc_nearest_representative_point(self, click_point): - distance = dist.cdist(self.grid_points, click_point.reshape(1, -1)) - index_nearest = np.argmin(distance) + def __calc_nearest_grid_point(self, click_coordinates): + distance = dist.cdist(self.grid_points, click_coordinates[None,:]) + index_nearest = np.argmin(distance.ravel()) return index_nearest + def __calc_nearest_latent_variable(self, click_coordinates, retdist=False): + distance = dist.cdist(self.Z, click_coordinates[None,:], metric='euclidean') + index_nearest = np.argmin(distance.ravel()) + dist_min = distance.min() + if retdist: + return index_nearest, dist_min + else: + return index_nearest + def __unflatten_grid_array(self, grid_array): if grid_array.shape[0] == np.prod(self.n_grid_points): return np.squeeze(grid_array.reshape(np.append(self.n_grid_points, -1))) From 549ba1b1d8190f2e94e07dab478dcb866863a970 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 18:16:08 +0900 Subject: [PATCH 44/84] Fix bugs, enable to change colormap and reformat --- libs/models/unsupervised_kernel_regression.py | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 33b6d18..57e467f 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,14 +174,16 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, label_data=None, label_feature=None, is_show_all_label_data=False, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): + def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, is_show_all_label_data=False, + fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): # import library to draw import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt - self._initialize_to_visualize(n_grid_points, label_data, label_feature, is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars) + self._initialize_to_visualize(n_grid_points, cmap, label_data, label_feature, + is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() self._draw_feature_bars() @@ -220,8 +222,8 @@ def __mouse_over_fig(self, event): elif event.inaxes == self.ax_feature_bars: pass - - def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, is_show_all_label_data, + def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_feature, + is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars): # invalid check if self.n_components != 2: @@ -237,7 +239,6 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, is_ else: raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で - self.grid_mapping = self.inverse_transform(self.grid_points) if label_data is None: self.label_data = label_data elif isinstance(label_data, list): @@ -283,17 +284,19 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, is_ self.ax_latent_space = ax_latent_space self.ax_feature_bars = ax_feature_bars + self.cmap = cmap + self.grid_mapping = self.inverse_transform(self.grid_points) self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True self.selected_feature = None self.grid_values_to_draw = None self.threshold_radius_show = np.abs(self.grid_points.max() - self.grid_points.min()) * 0.05 + self.index_data_label_shown = None epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) - def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) @@ -308,7 +311,7 @@ def _set_latent_space_from_feature_bar(self, click_coordinates): self.grid_values_to_draw = self.grid_mapping[:, i] def _set_shown_label_in_latent_space(self, click_coordinates): - index, dist = self.__calc_nearest_latent_variable(click_coordinates,retdist=True) + index, dist = self.__calc_nearest_latent_variable(click_coordinates, retdist=True) if dist <= self.threshold_radius_show: self.index_data_label_shown = index else: @@ -317,13 +320,14 @@ def _set_shown_label_in_latent_space(self, click_coordinates): def _draw_latent_space(self): self.ax_latent_space.cla() if self.grid_values_to_draw is not None: - #self.grid_values_to_draw = self.grid_mapping[:, self.selected_feature] + # self.grid_values_to_draw = self.grid_mapping[:, self.selected_feature] # To draw by pcolormesh and contour, reshape arrays like grid grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) pcm = self.ax_latent_space.pcolormesh(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], - grid_values_to_draw_3d) + grid_values_to_draw_3d, + cmap=self.cmap) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') @@ -339,9 +343,15 @@ def _draw_latent_space(self): ha='center', va='bottom', color='black') else: if self.index_data_label_shown is not None: - point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] - self.ax_latent_space.text(point_label[0], point_label[1], label, - ha='center', va='bottom', color='black') + # point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] + # label = self.label_data[self.index_data_label_shown] + self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], + self.Z[self.index_data_label_shown, 1], + self.label_data[self.index_data_label_shown], + ha='center', va='bottom', color='black', + bbox=dict(boxstyle='square', + ec=(1., 0.5, 0.5), + fc=(1., 0.8, 0.8))) else: pass if self.click_point_latent_space is None: @@ -369,12 +379,12 @@ def __draw_click_point_latent_space(self): ".", color="red", ms=20, fillstyle="none") def __calc_nearest_grid_point(self, click_coordinates): - distance = dist.cdist(self.grid_points, click_coordinates[None,:]) + distance = dist.cdist(self.grid_points, click_coordinates[None, :]) index_nearest = np.argmin(distance.ravel()) return index_nearest def __calc_nearest_latent_variable(self, click_coordinates, retdist=False): - distance = dist.cdist(self.Z, click_coordinates[None,:], metric='euclidean') + distance = dist.cdist(self.Z, click_coordinates[None, :], metric='euclidean') index_nearest = np.argmin(distance.ravel()) dist_min = distance.min() if retdist: From f296c30cbc03b7e14e85675cf6b35f4d83be3bb7 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 18:20:43 +0900 Subject: [PATCH 45/84] =?UTF-8?q?=E5=8B=95=E7=89=A9=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=AE=E3=83=81=E3=83=A5=E3=83=BC=E3=83=88=E3=83=AA?= =?UTF-8?q?=E3=82=A2=E3=83=AB=E3=81=A7cmap=E3=82=92=E6=8C=87=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 85acb3e..56e4c02 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -26,5 +26,7 @@ ukr.fit(nb_epoch=nb_epoch, eta=eta) ukr.visualize(n_grid_points=n_grid_points, + cmap='GnBu', label_data=labels_animal, - label_feature=labels_feature) + label_feature=labels_feature, + is_show_all_label_data=True) From 15c04d800de79150ffe4ebc19cfa43777d1a0f27 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 24 Apr 2020 20:22:09 +0900 Subject: [PATCH 46/84] =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E3=81=AEscatter=E3=81=AE=E5=A4=A7=E3=81=8D=E3=81=95=E3=82=92s?= =?UTF-8?q?=3D10=E3=81=AB=EF=BC=88=E3=83=87=E3=83=95=E3=82=A9=E3=83=AB?= =?UTF-8?q?=E3=83=88=E3=81=8C20=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 57e467f..07a6dc5 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -332,7 +332,7 @@ def _draw_latent_space(self): grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') self.ax_latent_space.clabel(ctr) - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1]) + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], s=10) if self.label_data is None: pass else: From ed58f67e51a220fe4dbf5eabb5ddb64ce3459331 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 26 Apr 2020 18:06:00 +0900 Subject: [PATCH 47/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E4=BB=A3=E8=A1=A8=E7=82=B9=E3=81=AE=E5=BA=A7=E6=A8=99=E3=82=92?= =?UTF-8?q?=E4=B8=8E=E3=81=88=E3=82=89=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 07a6dc5..28b37f7 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -235,7 +235,7 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur else: raise ValueError('Only support n_grid_points is int') if self.is_compact: - self.grid_points = create_zeta(-1.0, 1.0, self.n_components, n_grid_points) + self._set_grid_points(create_zeta(-1.0, 1.0, self.n_components, n_grid_points)) else: raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で @@ -285,7 +285,6 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur self.ax_feature_bars = ax_feature_bars self.cmap = cmap - self.grid_mapping = self.inverse_transform(self.grid_points) self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True @@ -297,6 +296,9 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) + def _set_grid_points(self, grid_points): + self.grid_points = grid_points + self.grid_mapping = self.inverse_transform(self.grid_points) def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) From 60d077bf5d910ae7f24e2ec1104aae646053a756 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 26 Apr 2020 18:15:38 +0900 Subject: [PATCH 48/84] =?UTF-8?q?=E4=BB=A3=E8=A1=A8=E7=82=B9=E3=81=AB?= =?UTF-8?q?=E9=96=A2=E9=80=A3=E3=81=99=E3=82=8B=E5=A4=89=E6=95=B0=E3=82=92?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=81=99=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E5=88=A5=E9=80=94=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 28b37f7..7013479 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -232,12 +232,12 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur if isinstance(n_grid_points, int): # 代表点の数を潜在空間の次元ごとに格納 self.n_grid_points = np.ones(self.n_components, dtype='int8') * n_grid_points + if self.is_compact: + self._set_grid(create_zeta(-1.0, 1.0, self.n_components, n_grid_points), self.n_grid_points) + else: + raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で else: raise ValueError('Only support n_grid_points is int') - if self.is_compact: - self._set_grid_points(create_zeta(-1.0, 1.0, self.n_components, n_grid_points)) - else: - raise ValueError('Not support is_compact=False') # create_zetaの整備が必要なので実装は後で if label_data is None: self.label_data = label_data @@ -290,15 +290,19 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur self.is_initial_view = True self.selected_feature = None self.grid_values_to_draw = None - self.threshold_radius_show = np.abs(self.grid_points.max() - self.grid_points.min()) * 0.05 self.index_data_label_shown = None + def _set_grid(self, grid_points, n_grid_points): + self.grid_points = grid_points + self.grid_mapping = self.inverse_transform(self.grid_points) + if grid_points.shape[0] == np.prod(n_grid_points): + self.n_grid_points = n_grid_points + else: + raise ValueError('grid_points shape and n_grid_points one are not consistent') + self.threshold_radius_show = np.abs(self.grid_points.max() - self.grid_points.min()) * 0.05 epsilon = 0.03 * np.abs(self.grid_points.max() - self.grid_points.min()) self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) - def _set_grid_points(self, grid_points): - self.grid_points = grid_points - self.grid_mapping = self.inverse_transform(self.grid_points) def _set_feature_bar_from_latent_space(self, click_coordinates): # クリックしたところといちばん近い代表点がどこかを計算 self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) From 5fb9a1f211615fdae9f23af5c12a2e5e3a588833 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 26 Apr 2020 18:49:40 +0900 Subject: [PATCH 49/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93=E3=81=AB=E6=8F=8F=E7=94=BB?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=80=A4=E3=82=92set=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 7013479..38b51e4 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -323,6 +323,9 @@ def _set_shown_label_in_latent_space(self, click_coordinates): else: self.index_data_label_shown = None + def _set_grid_values_to_draw(self, grid_values): + self.grid_values_to_draw = grid_values + def _draw_latent_space(self): self.ax_latent_space.cla() if self.grid_values_to_draw is not None: From b0096ebc1c9fe8d6e42e9d13b1f22bf38cd6802c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 26 Apr 2020 18:55:30 +0900 Subject: [PATCH 50/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93=E3=81=AE=E5=80=A4=E3=82=92?= =?UTF-8?q?=E4=BB=A3=E5=85=A5=E3=81=95=E3=82=8C=E3=81=9F=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=80=81=E3=83=92=E3=82=B9=E3=83=88=E3=82=B0=E3=83=A9=E3=83=A0?= =?UTF-8?q?=E3=81=AE=E6=96=B9=E3=81=A7=E3=81=AF=E4=BD=95=E3=82=82=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E3=81=97=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=82=92=E5=88=9D=E6=9C=9F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 38b51e4..2a4d3b9 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -325,6 +325,7 @@ def _set_shown_label_in_latent_space(self, click_coordinates): def _set_grid_values_to_draw(self, grid_values): self.grid_values_to_draw = grid_values + self.selected_feature = None def _draw_latent_space(self): self.ax_latent_space.cla() From be0d484174f0838e679c409871de2fbb5c9fceef Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 26 Apr 2020 19:08:16 +0900 Subject: [PATCH 51/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89cmap?= =?UTF-8?q?=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8B=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 2a4d3b9..4dda43a 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -327,6 +327,9 @@ def _set_grid_values_to_draw(self, grid_values): self.grid_values_to_draw = grid_values self.selected_feature = None + def _set_cmap(self, cmap): + self.cmap = cmap + def _draw_latent_space(self): self.ax_latent_space.cla() if self.grid_values_to_draw is not None: From ee11cc26691b7bb31acecfb74544a2a7e4ecf9e4 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 21:19:51 +0900 Subject: [PATCH 52/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89ax?= =?UTF-8?q?=E3=81=AE=E3=82=BF=E3=82=A4=E3=83=88=E3=83=AB=E3=82=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 4dda43a..7f15724 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -174,7 +174,8 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, is_show_all_label_data=False, + def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, + title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): # import library to draw @@ -183,7 +184,8 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= import matplotlib.pyplot as plt self._initialize_to_visualize(n_grid_points, cmap, label_data, label_feature, - is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars) + title_latent_space, title_feature_bars, is_show_all_label_data, + fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() self._draw_feature_bars() @@ -223,7 +225,7 @@ def __mouse_over_fig(self, event): pass def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_feature, - is_show_all_label_data, + title_latent_space, title_feature_bars, is_show_all_label_data, fig, fig_size, ax_latent_space, ax_feature_bars): # invalid check if self.n_components != 2: @@ -263,6 +265,16 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur else: raise ValueError('label_feature must be 1d array or list') + if title_latent_space is None: + self.title_latent_space = 'Latent space' + else: + self.title_latent_space = title_latent_space + + if title_feature_bars is None: + self.title_feature_bars = 'Feature bars' + else: + self.title_latent_space = title_feature_bars + self.is_show_all_label_data = is_show_all_label_data # set variables From e8fa3323ddba215edccb3c1973923c85f226bf57 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 21:20:06 +0900 Subject: [PATCH 53/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89ax?= =?UTF-8?q?=E3=81=AE=E3=82=BF=E3=82=A4=E3=83=88=E3=83=AB=E3=82=92=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 7f15724..7aea8f3 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -344,6 +344,9 @@ def _set_cmap(self, cmap): def _draw_latent_space(self): self.ax_latent_space.cla() + self.ax_latent_space.set_title(self.title_latent_space) + + # Draw color using self.grid_values_to_draw by pcolormesh and contour if self.grid_values_to_draw is not None: # self.grid_values_to_draw = self.grid_mapping[:, self.selected_feature] # To draw by pcolormesh and contour, reshape arrays like grid @@ -357,7 +360,11 @@ def _draw_latent_space(self): grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') self.ax_latent_space.clabel(ctr) + + # Plot latent variables self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], s=10) + + # Write label if self.label_data is None: pass else: @@ -379,10 +386,13 @@ def _draw_latent_space(self): fc=(1., 0.8, 0.8))) else: pass + + # Draw clicked coordinates if self.click_point_latent_space is None: pass else: self.__draw_click_point_latent_space() + self.fig.show() def _draw_feature_bars(self): @@ -392,9 +402,10 @@ def _draw_feature_bars(self): self.feature_bars[self.selected_feature].set_color('r') self.ax_feature_bars.set_ylim(self.X.min(), self.X.max() * 1.05) if self.click_point_latent_space is None: - self.ax_feature_bars.set_title('mean of data') + title = self.title_feature_bars+'(Dataset mean)' else: - self.ax_feature_bars.set_title('Features') + title = self.title_feature_bars + self.ax_feature_bars.set_title(title) self.ax_feature_bars.set_xticklabels(labels=self.label_feature, rotation=270) self.fig.show() From 0a360f5540677fb37bcb154c207f171f51032259 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 21:28:14 +0900 Subject: [PATCH 54/84] Fix --- libs/models/unsupervised_kernel_regression.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 7aea8f3..f06084d 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -273,7 +273,7 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur if title_feature_bars is None: self.title_feature_bars = 'Feature bars' else: - self.title_latent_space = title_feature_bars + self.title_feature_bars = title_feature_bars self.is_show_all_label_data = is_show_all_label_data @@ -289,9 +289,7 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur if ax_latent_space is None and ax_feature_bars is None: self.ax_latent_space = self.fig.add_subplot(1, 2, 1, aspect='equal') - self.ax_latent_space.set_title('Latent space') self.ax_feature_bars = self.fig.add_subplot(1, 2, 2) - self.ax_feature_bars.set_title('Mean of data') else: self.ax_latent_space = ax_latent_space self.ax_feature_bars = ax_feature_bars @@ -402,7 +400,7 @@ def _draw_feature_bars(self): self.feature_bars[self.selected_feature].set_color('r') self.ax_feature_bars.set_ylim(self.X.min(), self.X.max() * 1.05) if self.click_point_latent_space is None: - title = self.title_feature_bars+'(Dataset mean)' + title = self.title_feature_bars + ' (Dataset mean)' else: title = self.title_feature_bars self.ax_feature_bars.set_title(title) From 6cf16a8164773ecb6b8540b3f7f4de000bc825b6 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 21:28:44 +0900 Subject: [PATCH 55/84] =?UTF-8?q?tutorial=E3=81=A7ax=E3=81=AE=E3=82=BF?= =?UTF-8?q?=E3=82=A4=E3=83=88=E3=83=AB=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 56e4c02..279149c 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -29,4 +29,6 @@ cmap='GnBu', label_data=labels_animal, label_feature=labels_feature, + title_latent_space='Animal map (latent space)', + title_feature_bars='Animal feature', is_show_all_label_data=True) From 71e669029be300b90c83738b2a84b2111f4026fb Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 22:07:54 +0900 Subject: [PATCH 56/84] =?UTF-8?q?ax=E3=81=AE=E3=82=BF=E3=82=A4=E3=83=88?= =?UTF-8?q?=E3=83=AB=E3=82=92=E8=A8=AD=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index f06084d..beaa291 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -340,6 +340,10 @@ def _set_grid_values_to_draw(self, grid_values): def _set_cmap(self, cmap): self.cmap = cmap + def _set_titles(self, title_latent_space, title_feature_bars): + self.title_latent_space = title_latent_space + self.title_feature_bars = title_feature_bars + def _draw_latent_space(self): self.ax_latent_space.cla() self.ax_latent_space.set_title(self.title_latent_space) From ae868f5aadbfa4e7ced358c6c5f5fbfb99aea5d7 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 29 Apr 2020 22:57:36 +0900 Subject: [PATCH 57/84] =?UTF-8?q?feature=20bar=E3=81=AE=E3=83=A9=E3=83=99?= =?UTF-8?q?=E3=83=AB=E3=81=AEfontsize=E3=82=92=E5=B0=8F=E3=81=95=E3=81=8F?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index beaa291..fb1b056 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -408,7 +408,7 @@ def _draw_feature_bars(self): else: title = self.title_feature_bars self.ax_feature_bars.set_title(title) - self.ax_feature_bars.set_xticklabels(labels=self.label_feature, rotation=270) + self.ax_feature_bars.set_xticklabels(labels=self.label_feature, fontsize=8, rotation=270) self.fig.show() def __draw_click_point_latent_space(self): From 31a694408364d7e1694f4f9219a0a79278f60591 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 07:44:49 +0900 Subject: [PATCH 58/84] =?UTF-8?q?=E5=88=A9=E7=94=A8=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=AA=E3=81=84=E5=A4=89=E6=95=B0=E3=81=AE=E7=A2=BA?= =?UTF-8?q?=E4=BF=9D=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index fb1b056..2fa6333 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -354,10 +354,10 @@ def _draw_latent_space(self): # To draw by pcolormesh and contour, reshape arrays like grid grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) - pcm = self.ax_latent_space.pcolormesh(grid_points_3d[:, :, 0], - grid_points_3d[:, :, 1], - grid_values_to_draw_3d, - cmap=self.cmap) + self.ax_latent_space.pcolormesh(grid_points_3d[:, :, 0], + grid_points_3d[:, :, 1], + grid_values_to_draw_3d, + cmap=self.cmap) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') From 5343e0fb496c712630e3504b4ab7628646bc31e6 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 10:28:58 +0900 Subject: [PATCH 59/84] add description of visualize method --- libs/models/unsupervised_kernel_regression.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 2fa6333..1cdfab8 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -177,6 +177,39 @@ def inverse_transform(self, Znew): def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): + """Visualize fit model interactively. + The dataset can be visualized in an exploratory way using the latent variables and the mapping estimated by UKR. + When an arbitrary coordinate on the latent space is specified, the corresponding feature is displayed as a bar. + Also, if a bar of a particular feature is specified, the latent space is colored by its value. + + Note: This method uses TkAgg as a backend. Therefore, if matplotlib is imported beforehand + and a different backend is specified, it will not work. + :param n_grid_points: int, optional, default = None + Number of representative points of discretization of the latent space needed for the drawing. + :param cmap: str, optional, default = None + Colormap to color the latent space. It conforms to the matplotlib color map. + :param label_data: array of shape (n_data, ), optional. default = None + The labels corresponds rows of the dataset X. + :param label_feature: array of shape (n_features, ), optional. default = None + The labels corresponds columns of the dataset X. + :param title_latent_space: str, optional, default = None + The title of axis to visualize the latent space + :param title_feature_bars: str, optional, default = None + The title of axis to visualize bars of features + :param is_show_all_label_data: bool, optional, default = False + When True the labels of the data is always shown. + When False the label is only shown when the cursor overlaps the corresponding latent variable. + :param fig: matplotlib.figure.Figure, default = True + The figure to visualize. + It is assigned only when you want to specify a figure to visualize. + :param fig_size: (float, float), optional, default = None + The size of figure. + :param ax_latent_space: matplotlib.axes._subplots.AxesSubplot, optional, default = False + The axis to visualize latent space. + :param ax_feature_bars: matplotlib.axes._subplots.AxesSubplot, optional, default = False + The axis to visualize feature_bars. + :return: None + """ # import library to draw import matplotlib From b9d118d371efe9099a26d0fdfbbf94fa36e51659 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 18:18:04 +0900 Subject: [PATCH 60/84] Update libs/models/unsupervised_kernel_regression.py Co-authored-by: tsuno <30923698+tsuno0829@users.noreply.github.com> --- libs/models/unsupervised_kernel_regression.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 1cdfab8..2d08ec5 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -412,13 +412,15 @@ def _draw_latent_space(self): if self.index_data_label_shown is not None: # point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] # label = self.label_data[self.index_data_label_shown] - self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], + from matplotlib import patheffects as path_effects + text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], self.Z[self.index_data_label_shown, 1], self.label_data[self.index_data_label_shown], ha='center', va='bottom', color='black', bbox=dict(boxstyle='square', ec=(1., 0.5, 0.5), fc=(1., 0.8, 0.8))) + text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()]) else: pass From 57cfe533953fe8c37bd414c4b41d9a9d6cc6f2ef Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 19:07:46 +0900 Subject: [PATCH 61/84] =?UTF-8?q?=E3=82=AA=E3=83=BC=E3=83=90=E3=83=BC?= =?UTF-8?q?=E3=83=A9=E3=83=83=E3=83=97=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF=E3=83=A9=E3=83=99=E3=83=AB?= =?UTF-8?q?=E3=82=84contour=E3=82=82=E7=99=BD=E6=9E=A0=E3=81=A7=E7=B8=81?= =?UTF-8?q?=E5=8F=96=E3=82=8A=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 2d08ec5..620c4cf 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -378,12 +378,13 @@ def _set_titles(self, title_latent_space, title_feature_bars): self.title_feature_bars = title_feature_bars def _draw_latent_space(self): + import matplotlib.pyplot as plt + from matplotlib import patheffects as path_effects self.ax_latent_space.cla() self.ax_latent_space.set_title(self.title_latent_space) # Draw color using self.grid_values_to_draw by pcolormesh and contour if self.grid_values_to_draw is not None: - # self.grid_values_to_draw = self.grid_mapping[:, self.selected_feature] # To draw by pcolormesh and contour, reshape arrays like grid grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) @@ -394,7 +395,11 @@ def _draw_latent_space(self): ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') - self.ax_latent_space.clabel(ctr) + plt.setp(ctr.collections, path_effects=[path_effects.Stroke(linewidth=2, foreground='white'), + path_effects.Normal()]) + clbls = self.ax_latent_space.clabel(ctr) + plt.setp(clbls, path_effects=[path_effects.Stroke(linewidth=1, foreground='white'), + path_effects.Normal()]) # Plot latent variables self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], s=10) @@ -406,13 +411,14 @@ def _draw_latent_space(self): if self.is_show_all_label_data: for z, noise, label in zip(self.Z, self.noise_label, self.label_data): point_label = z + noise - self.ax_latent_space.text(point_label[0], point_label[1], label, + text = self.ax_latent_space.text(point_label[0], point_label[1], label, ha='center', va='bottom', color='black') + text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), + path_effects.Normal()]) else: if self.index_data_label_shown is not None: # point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] # label = self.label_data[self.index_data_label_shown] - from matplotlib import patheffects as path_effects text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], self.Z[self.index_data_label_shown, 1], self.label_data[self.index_data_label_shown], @@ -420,7 +426,9 @@ def _draw_latent_space(self): bbox=dict(boxstyle='square', ec=(1., 0.5, 0.5), fc=(1., 0.8, 0.8))) - text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()]) + text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), + path_effects.Normal()] + ) else: pass From d1df31965e84e06639e1b24a2f132087ade517b9 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 19:13:28 +0900 Subject: [PATCH 62/84] =?UTF-8?q?is=5Fshow=5Fall=5Flabel=5Fdata=3DFalse?= =?UTF-8?q?=E3=81=AE=E6=99=82=E3=81=AEbbox=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 620c4cf..fe3dc7b 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -422,10 +422,7 @@ def _draw_latent_space(self): text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], self.Z[self.index_data_label_shown, 1], self.label_data[self.index_data_label_shown], - ha='center', va='bottom', color='black', - bbox=dict(boxstyle='square', - ec=(1., 0.5, 0.5), - fc=(1., 0.8, 0.8))) + ha='center', va='bottom', color='black') text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()] ) From 6a4c7b41aa8bade8aa2dd95f63b75c2c70e76990 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 29 May 2020 19:14:32 +0900 Subject: [PATCH 63/84] Reformat --- libs/models/unsupervised_kernel_regression.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index fe3dc7b..2df2689 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -412,7 +412,7 @@ def _draw_latent_space(self): for z, noise, label in zip(self.Z, self.noise_label, self.label_data): point_label = z + noise text = self.ax_latent_space.text(point_label[0], point_label[1], label, - ha='center', va='bottom', color='black') + ha='center', va='bottom', color='black') text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()]) else: @@ -420,9 +420,9 @@ def _draw_latent_space(self): # point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] # label = self.label_data[self.index_data_label_shown] text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], - self.Z[self.index_data_label_shown, 1], - self.label_data[self.index_data_label_shown], - ha='center', va='bottom', color='black') + self.Z[self.index_data_label_shown, 1], + self.label_data[self.index_data_label_shown], + ha='center', va='bottom', color='black') text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), path_effects.Normal()] ) From a7c5852d94cc56157dc785883f7ecd9c2a59fdfc Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 2 Jun 2020 21:04:28 +0900 Subject: [PATCH 64/84] =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93?= =?UTF-8?q?=E3=81=AE=E6=8F=8F=E7=94=BB=E3=82=92pcolormesh=E3=81=8B?= =?UTF-8?q?=E3=82=89imshow=E3=81=AB=E5=A4=89=E6=9B=B4=E3=81=97=E3=82=AA?= =?UTF-8?q?=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=ABinterpolation?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 2df2689..8a13333 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -176,7 +176,7 @@ def inverse_transform(self, Znew): def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, - fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): + interpolation=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): """Visualize fit model interactively. The dataset can be visualized in an exploratory way using the latent variables and the mapping estimated by UKR. When an arbitrary coordinate on the latent space is specified, the corresponding feature is displayed as a bar. @@ -199,6 +199,8 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= :param is_show_all_label_data: bool, optional, default = False When True the labels of the data is always shown. When False the label is only shown when the cursor overlaps the corresponding latent variable. + :param interpolation: str, optional, default = None + Interpolation method by imshow. :param fig: matplotlib.figure.Figure, default = True The figure to visualize. It is assigned only when you want to specify a figure to visualize. @@ -218,7 +220,7 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= self._initialize_to_visualize(n_grid_points, cmap, label_data, label_feature, title_latent_space, title_feature_bars, is_show_all_label_data, - fig, fig_size, ax_latent_space, ax_feature_bars) + interpolation, fig, fig_size, ax_latent_space, ax_feature_bars) self._draw_latent_space() self._draw_feature_bars() @@ -259,7 +261,7 @@ def __mouse_over_fig(self, event): def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_feature, title_latent_space, title_feature_bars, is_show_all_label_data, - fig, fig_size, ax_latent_space, ax_feature_bars): + interpolation, fig, fig_size, ax_latent_space, ax_feature_bars): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -328,6 +330,7 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur self.ax_feature_bars = ax_feature_bars self.cmap = cmap + self.interpolation = interpolation self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True @@ -388,10 +391,30 @@ def _draw_latent_space(self): # To draw by pcolormesh and contour, reshape arrays like grid grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) - self.ax_latent_space.pcolormesh(grid_points_3d[:, :, 0], - grid_points_3d[:, :, 1], - grid_values_to_draw_3d, - cmap=self.cmap) + # set coordinate of axis + any_index = 0 + if grid_points_3d[any_index, 0, 0] < grid_points_3d[any_index, -1, 0]: + coordinate_ax_left = grid_points_3d[any_index, 0, 0] + coordinate_ax_right = grid_points_3d[any_index, -1, 0] + else: + coordinate_ax_left = grid_points_3d[any_index, -1, 0] + coordinate_ax_right = grid_points_3d[any_index, 0, 0] + grid_values_to_draw_3d = np.flip(grid_values_to_draw_3d, axis=1).copy() + + if grid_points_3d[-1, any_index, 1] < grid_points_3d[0, any_index, 1]: + coordinate_ax_bottom = grid_points_3d[-1, any_index, 1] + coordinate_ax_top = grid_points_3d[0, any_index, 1] + else: + coordinate_ax_bottom = grid_points_3d[0, any_index, 1] + coordinate_ax_top = grid_points_3d[-1, any_index, 1] + grid_values_to_draw_3d = np.flip(grid_values_to_draw_3d, axis=0).copy() + self.ax_latent_space.imshow(grid_values_to_draw_3d, + extent=[coordinate_ax_left, + coordinate_ax_right, + coordinate_ax_bottom, + coordinate_ax_top], + interpolation=self.interpolation, + cmap=self.cmap) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_draw_3d, 6, colors='k') From 2db9df9d40613e9f010ddfa2d878bea490f3769c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 2 Jun 2020 21:24:04 +0900 Subject: [PATCH 65/84] =?UTF-8?q?=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=97?= =?UTF-8?q?=E3=83=88=E5=86=85=E3=81=A7=E5=AE=9A=E7=BE=A9=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9Fcreate=5Fzeta=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=97somf=E3=81=AEcreate=5Fzeta=E3=82=92=E5=88=A9?= =?UTF-8?q?=E7=94=A8=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 8a13333..142ce5a 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -1,6 +1,7 @@ import numpy as np import scipy.spatial.distance as dist from tqdm import tqdm +from ..tools.create_zeta import create_zeta class UnsupervisedKernelRegression(object): @@ -499,15 +500,3 @@ def __unflatten_grid_array(self, grid_array): else: raise ValueError('arg shape {} is not consistent'.format(grid_array.shape)) - -def create_zeta(zeta_min, zeta_max, latent_dim, resolution): - mesh1d, step = np.linspace(zeta_min, zeta_max, resolution, endpoint=False, retstep=True) - mesh1d += step / 2.0 - if latent_dim == 1: - Zeta = mesh1d - elif latent_dim == 2: - Zeta = np.meshgrid(mesh1d, mesh1d) - else: - raise ValueError("invalid latent dim {}".format(latent_dim)) - Zeta = np.dstack(Zeta).reshape(-1, latent_dim) - return Zeta From 618c954a1b55d321adc677597ec5a14870f25660 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 2 Jun 2020 21:27:19 +0900 Subject: [PATCH 66/84] =?UTF-8?q?tutorial=E3=82=82=E3=83=A2=E3=83=87?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E5=A4=89=E6=9B=B4=E3=81=AB=E5=90=88=E3=82=8F?= =?UTF-8?q?=E3=81=9B=E3=81=A6=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 279149c..d38e3a5 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -12,8 +12,6 @@ nb_epoch = 1000 eta = 0.02 - n_grid_points = 100 - X, labels_animal, labels_feature = animal.load_data(retlabel_animal=True, retlabel_feature=True) @@ -25,10 +23,11 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_, init=init) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.visualize(n_grid_points=n_grid_points, + ukr.visualize(n_grid_points=100, cmap='GnBu', label_data=labels_animal, label_feature=labels_feature, title_latent_space='Animal map (latent space)', title_feature_bars='Animal feature', + interpolation='spline16', is_show_all_label_data=True) From cf19cb7e4daa898133ab0cb0b85222363dfe41fe Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Fri, 5 Jun 2020 08:41:05 +0900 Subject: [PATCH 67/84] =?UTF-8?q?contour=E3=81=8C=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E5=8F=8D=E8=BB=A2=E3=81=95=E3=82=8C=E3=82=8B=E3=83=90=E3=82=B0?= =?UTF-8?q?=E3=82=92fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 142ce5a..0fe4153 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -390,7 +390,8 @@ def _draw_latent_space(self): # Draw color using self.grid_values_to_draw by pcolormesh and contour if self.grid_values_to_draw is not None: # To draw by pcolormesh and contour, reshape arrays like grid - grid_values_to_draw_3d = self.__unflatten_grid_array(self.grid_values_to_draw) + grid_values_to_imshow = self.__unflatten_grid_array(self.grid_values_to_draw) + grid_values_to_contour = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) # set coordinate of axis any_index = 0 @@ -400,7 +401,7 @@ def _draw_latent_space(self): else: coordinate_ax_left = grid_points_3d[any_index, -1, 0] coordinate_ax_right = grid_points_3d[any_index, 0, 0] - grid_values_to_draw_3d = np.flip(grid_values_to_draw_3d, axis=1).copy() + grid_values_to_imshow = np.flip(grid_values_to_imshow, axis=1).copy() if grid_points_3d[-1, any_index, 1] < grid_points_3d[0, any_index, 1]: coordinate_ax_bottom = grid_points_3d[-1, any_index, 1] @@ -408,8 +409,8 @@ def _draw_latent_space(self): else: coordinate_ax_bottom = grid_points_3d[0, any_index, 1] coordinate_ax_top = grid_points_3d[-1, any_index, 1] - grid_values_to_draw_3d = np.flip(grid_values_to_draw_3d, axis=0).copy() - self.ax_latent_space.imshow(grid_values_to_draw_3d, + grid_values_to_imshow = np.flip(grid_values_to_imshow, axis=0).copy() + self.ax_latent_space.imshow(grid_values_to_imshow, extent=[coordinate_ax_left, coordinate_ax_right, coordinate_ax_bottom, @@ -418,7 +419,7 @@ def _draw_latent_space(self): cmap=self.cmap) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], - grid_values_to_draw_3d, 6, colors='k') + grid_values_to_contour, 6, colors='k') plt.setp(ctr.collections, path_effects=[path_effects.Stroke(linewidth=2, foreground='white'), path_effects.Normal()]) clbls = self.ax_latent_space.clabel(ctr) From 3f0aa26f4485c467378584b0a7e5ca748ce8dba9 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Sun, 7 Jun 2020 13:56:54 +0900 Subject: [PATCH 68/84] =?UTF-8?q?visualize=E3=81=AEdocstring=E3=82=92Numpy?= =?UTF-8?q?=20style=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 0fe4153..9c266cb 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -185,33 +185,39 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= Note: This method uses TkAgg as a backend. Therefore, if matplotlib is imported beforehand and a different backend is specified, it will not work. - :param n_grid_points: int, optional, default = None + + Parameters + ---------- + n_grid_points: int, optional, default = None Number of representative points of discretization of the latent space needed for the drawing. - :param cmap: str, optional, default = None + cmap: str, optional, default = None Colormap to color the latent space. It conforms to the matplotlib color map. - :param label_data: array of shape (n_data, ), optional. default = None + label_data: array of shape (n_data, ), optional. default = None The labels corresponds rows of the dataset X. - :param label_feature: array of shape (n_features, ), optional. default = None + label_feature: array of shape (n_features, ), optional. default = None The labels corresponds columns of the dataset X. - :param title_latent_space: str, optional, default = None + title_latent_space: str, optional, default = None The title of axis to visualize the latent space - :param title_feature_bars: str, optional, default = None + title_feature_bars: str, optional, default = None The title of axis to visualize bars of features - :param is_show_all_label_data: bool, optional, default = False + is_show_all_label_data: bool, optional, default = False When True the labels of the data is always shown. When False the label is only shown when the cursor overlaps the corresponding latent variable. - :param interpolation: str, optional, default = None + interpolation: str, optional, default = None Interpolation method by imshow. - :param fig: matplotlib.figure.Figure, default = True + fig: matplotlib.figure.Figure, default = True The figure to visualize. It is assigned only when you want to specify a figure to visualize. - :param fig_size: (float, float), optional, default = None + fig_size: (float, float), optional, default = None The size of figure. - :param ax_latent_space: matplotlib.axes._subplots.AxesSubplot, optional, default = False + ax_latent_space: matplotlib.axes._subplots.AxesSubplot, optional, default = False The axis to visualize latent space. - :param ax_feature_bars: matplotlib.axes._subplots.AxesSubplot, optional, default = False + ax_feature_bars: matplotlib.axes._subplots.AxesSubplot, optional, default = False The axis to visualize feature_bars. - :return: None + + Returns + ------- + No returns """ # import library to draw From 3f8f269905d93313fe76a8f8f057580ce0580d84 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 9 Jun 2020 18:08:41 +0900 Subject: [PATCH 69/84] =?UTF-8?q?=E5=BC=95=E6=95=B0=E3=81=AB=E3=82=AB?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=83=9E=E3=83=83=E3=83=97=E3=81=AE=E4=B8=AD?= =?UTF-8?q?=E9=96=93=E3=81=AE=E8=89=B2=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=80=A4=E3=82=920=E3=81=AB=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 9c266cb..42406a3 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -177,7 +177,8 @@ def inverse_transform(self, Znew): def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, - interpolation=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): + interpolation=None, is_middle_color_zero=False, + fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): """Visualize fit model interactively. The dataset can be visualized in an exploratory way using the latent variables and the mapping estimated by UKR. When an arbitrary coordinate on the latent space is specified, the corresponding feature is displayed as a bar. @@ -205,6 +206,10 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= When False the label is only shown when the cursor overlaps the corresponding latent variable. interpolation: str, optional, default = None Interpolation method by imshow. + is_middle_color_zero: bool, optional, default = False + If `True`, the value corresponding to middle color in the colormap is fixed at 0. + If the data is normalized to zero mean and it is important whether it is higher or lower than the mean, + setting to `True` makes the coloring easier to read. fig: matplotlib.figure.Figure, default = True The figure to visualize. It is assigned only when you want to specify a figure to visualize. @@ -225,9 +230,19 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= matplotlib.use('TkAgg') import matplotlib.pyplot as plt - self._initialize_to_visualize(n_grid_points, cmap, label_data, label_feature, - title_latent_space, title_feature_bars, is_show_all_label_data, - interpolation, fig, fig_size, ax_latent_space, ax_feature_bars) + self._initialize_to_visualize(n_grid_points=n_grid_points, + cmap=cmap, + label_data=label_data, + label_feature=label_feature, + title_latent_space=title_latent_space, + title_feature_bars=title_feature_bars, + is_show_all_label_data=is_show_all_label_data, + interpolation=interpolation, + is_middle_color_zero=is_middle_color_zero, + fig=fig, + fig_size=fig_size, + ax_latent_space=ax_latent_space, + ax_feature_bars=ax_feature_bars) self._draw_latent_space() self._draw_feature_bars() @@ -268,7 +283,8 @@ def __mouse_over_fig(self, event): def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_feature, title_latent_space, title_feature_bars, is_show_all_label_data, - interpolation, fig, fig_size, ax_latent_space, ax_feature_bars): + interpolation, is_middle_color_zero, + fig, fig_size, ax_latent_space, ax_feature_bars): # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -338,6 +354,7 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur self.cmap = cmap self.interpolation = interpolation + self.is_middle_color_zero = is_middle_color_zero self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True @@ -395,10 +412,21 @@ def _draw_latent_space(self): # Draw color using self.grid_values_to_draw by pcolormesh and contour if self.grid_values_to_draw is not None: - # To draw by pcolormesh and contour, reshape arrays like grid + # set vmin and vmax + if self.is_middle_color_zero: + max_grid_value = self.grid_values_to_draw.max() + min_grid_value = self.grid_values_to_draw.min() + vmin = -max(abs(max_grid_value), abs(min_grid_value)) + vmax = max(abs(max_grid_value), abs(min_grid_value)) + else: + vmin = None + vmax = None + + # To draw by imshow and contour, reshape arrays like grid grid_values_to_imshow = self.__unflatten_grid_array(self.grid_values_to_draw) grid_values_to_contour = self.__unflatten_grid_array(self.grid_values_to_draw) grid_points_3d = self.__unflatten_grid_array(self.grid_points) + # set coordinate of axis any_index = 0 if grid_points_3d[any_index, 0, 0] < grid_points_3d[any_index, -1, 0]: @@ -422,7 +450,9 @@ def _draw_latent_space(self): coordinate_ax_bottom, coordinate_ax_top], interpolation=self.interpolation, - cmap=self.cmap) + cmap=self.cmap, + vmin=vmin, + vmax=vmax) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_contour, 6, colors='k') From c86ca1a7a2bbd641b0beb0663a323a13a9e2a223 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 9 Jun 2020 18:20:14 +0900 Subject: [PATCH 70/84] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=82=92=E3=83=81=E3=83=A5=E3=83=BC=E3=83=88=E3=83=AA?= =?UTF-8?q?=E3=82=A2=E3=83=AB=E3=81=AB=E3=82=82=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tutorials/ukr/fitting_animal_data.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index d38e3a5..1b9ea6e 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -30,4 +30,5 @@ title_latent_space='Animal map (latent space)', title_feature_bars='Animal feature', interpolation='spline16', + is_middle_color_zero=True, is_show_all_label_data=True) From 625e87e266ebeff542171edb98244dc43f80e4be Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Wed, 10 Jun 2020 23:04:21 +0900 Subject: [PATCH 71/84] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=81=A8=E3=81=97=E3=81=A6=E6=BD=9C=E5=9C=A8=E5=A4=89?= =?UTF-8?q?=E6=95=B0=E3=81=AEscatter=E3=81=AEcmap=E3=82=84color,=20marker?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 31 ++++++++++++++----- tutorials/ukr/fitting_animal_data.py | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 42406a3..5dace78 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -175,7 +175,8 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature=None, + def visualize(self, n_grid_points=30, cmap_imshow=None, cmap_z=None, color_z=None, marker_z=None, + label_data=None, label_feature=None, title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, interpolation=None, is_middle_color_zero=False, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): @@ -191,8 +192,14 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= ---------- n_grid_points: int, optional, default = None Number of representative points of discretization of the latent space needed for the drawing. - cmap: str, optional, default = None + cmap_imshow: str, optional, default = None Colormap to color the latent space. It conforms to the matplotlib color map. + cmap_z: str, optional, default = None + Colormap to color latent variable in latent space. It conforms to the matplotlib color map. + color_z: color, sequence, or sequence of color, optional, default = None + The marker color of latent variable z. It conforms to the matplotlib. + marker_z: MarkerStyle, optional, default = None + The marker style of latent variable z. It conforms to the matplotlib. label_data: array of shape (n_data, ), optional. default = None The labels corresponds rows of the dataset X. label_feature: array of shape (n_features, ), optional. default = None @@ -231,7 +238,10 @@ def visualize(self, n_grid_points=30, cmap=None, label_data=None, label_feature= import matplotlib.pyplot as plt self._initialize_to_visualize(n_grid_points=n_grid_points, - cmap=cmap, + cmap_imshow=cmap_imshow, + cmap_z=cmap_z, + color_z=color_z, + marker_z=marker_z, label_data=label_data, label_feature=label_feature, title_latent_space=title_latent_space, @@ -281,7 +291,8 @@ def __mouse_over_fig(self, event): elif event.inaxes == self.ax_feature_bars: pass - def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_feature, + def _initialize_to_visualize(self, n_grid_points, cmap_imshow, cmap_z, color_z, marker_z, + label_data, label_feature, title_latent_space, title_feature_bars, is_show_all_label_data, interpolation, is_middle_color_zero, fig, fig_size, ax_latent_space, ax_feature_bars): @@ -352,7 +363,10 @@ def _initialize_to_visualize(self, n_grid_points, cmap, label_data, label_featur self.ax_latent_space = ax_latent_space self.ax_feature_bars = ax_feature_bars - self.cmap = cmap + self.cmap_imshow = cmap_imshow + self.cmap_z = cmap_z + self.color_z = color_z + self.marker_z = marker_z self.interpolation = interpolation self.is_middle_color_zero = is_middle_color_zero self.click_point_latent_space = None # index of the clicked representative point @@ -398,7 +412,7 @@ def _set_grid_values_to_draw(self, grid_values): self.selected_feature = None def _set_cmap(self, cmap): - self.cmap = cmap + self.cmap_imshow = cmap def _set_titles(self, title_latent_space, title_feature_bars): self.title_latent_space = title_latent_space @@ -450,7 +464,7 @@ def _draw_latent_space(self): coordinate_ax_bottom, coordinate_ax_top], interpolation=self.interpolation, - cmap=self.cmap, + cmap=self.cmap_imshow, vmin=vmin, vmax=vmax) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], @@ -463,7 +477,8 @@ def _draw_latent_space(self): path_effects.Normal()]) # Plot latent variables - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], s=10) + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], + s=10, c=self.color_z, cmap=self.cmap_z, marker=self.marker_z) # Write label if self.label_data is None: diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 1b9ea6e..572484e 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -24,7 +24,7 @@ ukr.fit(nb_epoch=nb_epoch, eta=eta) ukr.visualize(n_grid_points=100, - cmap='GnBu', + cmap_imshow='GnBu', label_data=labels_animal, label_feature=labels_feature, title_latent_space='Animal map (latent space)', From 3ff65582ec9408e167ba79431325ed92275791ca Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 11 Jun 2020 10:41:26 +0900 Subject: [PATCH 72/84] =?UTF-8?q?imshow=E3=82=84scatter=E3=81=AB=E9=96=A2?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E7=B4=B0=E3=81=8B=E3=81=8F=E4=B8=8E=E3=81=88=E3=82=8B?= =?UTF-8?q?=E3=81=AE=E3=81=A7=E3=81=AF=E3=81=AA=E3=81=8F=EF=BC=8C=E8=BE=9E?= =?UTF-8?q?=E6=9B=B8=E3=81=A8=E3=81=97=E3=81=A6=E6=B8=A1=E3=81=99=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 81 +++++++++---------- testfunc.py | 9 +++ tutorials/ukr/fitting_animal_data.py | 23 +++--- 3 files changed, 63 insertions(+), 50 deletions(-) create mode 100644 testfunc.py diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 5dace78..e18f454 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -175,10 +175,10 @@ def inverse_transform(self, Znew): return F - def visualize(self, n_grid_points=30, cmap_imshow=None, cmap_z=None, color_z=None, marker_z=None, - label_data=None, label_feature=None, - title_latent_space=None, title_feature_bars=None, is_show_all_label_data=False, - interpolation=None, is_middle_color_zero=False, + def visualize(self, n_grid_points=30, label_data=None, label_feature=None, + is_show_all_label_data=False, is_middle_color_zero=False, + params_imshow=None, params_scatter=None, + title_latent_space=None, title_feature_bars=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): """Visualize fit model interactively. The dataset can be visualized in an exploratory way using the latent variables and the mapping estimated by UKR. @@ -192,31 +192,27 @@ def visualize(self, n_grid_points=30, cmap_imshow=None, cmap_z=None, color_z=Non ---------- n_grid_points: int, optional, default = None Number of representative points of discretization of the latent space needed for the drawing. - cmap_imshow: str, optional, default = None - Colormap to color the latent space. It conforms to the matplotlib color map. - cmap_z: str, optional, default = None - Colormap to color latent variable in latent space. It conforms to the matplotlib color map. - color_z: color, sequence, or sequence of color, optional, default = None - The marker color of latent variable z. It conforms to the matplotlib. - marker_z: MarkerStyle, optional, default = None - The marker style of latent variable z. It conforms to the matplotlib. label_data: array of shape (n_data, ), optional. default = None The labels corresponds rows of the dataset X. label_feature: array of shape (n_features, ), optional. default = None The labels corresponds columns of the dataset X. - title_latent_space: str, optional, default = None - The title of axis to visualize the latent space - title_feature_bars: str, optional, default = None - The title of axis to visualize bars of features is_show_all_label_data: bool, optional, default = False When True the labels of the data is always shown. When False the label is only shown when the cursor overlaps the corresponding latent variable. - interpolation: str, optional, default = None - Interpolation method by imshow. is_middle_color_zero: bool, optional, default = False If `True`, the value corresponding to middle color in the colormap is fixed at 0. If the data is normalized to zero mean and it is important whether it is higher or lower than the mean, setting to `True` makes the coloring easier to read. + params_imshow: dict, optional, default = None + The dictionary of kwargs in imshow used for drawing the value of the feature in the latent space. + See the official document of matplotlib.pyplot.imshow for more information on available arguments. + params_scatter: dict, optional, default = None + The dictionary of kwargs in scatter used for plotting latent variables in the latent space. + See the official document of matplotlib.pyplot.imshow for more information on available arguments. + title_latent_space: str, optional, default = None + The title of axis to visualize the latent space + title_feature_bars: str, optional, default = None + The title of axis to visualize bars of features fig: matplotlib.figure.Figure, default = True The figure to visualize. It is assigned only when you want to specify a figure to visualize. @@ -238,16 +234,13 @@ def visualize(self, n_grid_points=30, cmap_imshow=None, cmap_z=None, color_z=Non import matplotlib.pyplot as plt self._initialize_to_visualize(n_grid_points=n_grid_points, - cmap_imshow=cmap_imshow, - cmap_z=cmap_z, - color_z=color_z, - marker_z=marker_z, + params_imshow=params_imshow, + params_scatter=params_scatter, label_data=label_data, label_feature=label_feature, title_latent_space=title_latent_space, title_feature_bars=title_feature_bars, is_show_all_label_data=is_show_all_label_data, - interpolation=interpolation, is_middle_color_zero=is_middle_color_zero, fig=fig, fig_size=fig_size, @@ -291,11 +284,12 @@ def __mouse_over_fig(self, event): elif event.inaxes == self.ax_feature_bars: pass - def _initialize_to_visualize(self, n_grid_points, cmap_imshow, cmap_z, color_z, marker_z, - label_data, label_feature, - title_latent_space, title_feature_bars, is_show_all_label_data, - interpolation, is_middle_color_zero, + def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, + is_show_all_label_data, is_middle_color_zero, + params_imshow, params_scatter, + title_latent_space, title_feature_bars, fig, fig_size, ax_latent_space, ax_feature_bars): + # invalid check if self.n_components != 2: raise ValueError('Now support only n_components = 2') @@ -334,6 +328,21 @@ def _initialize_to_visualize(self, n_grid_points, cmap_imshow, cmap_z, color_z, else: raise ValueError('label_feature must be 1d array or list') + if params_imshow is None: + self.params_imshow = {} + elif isinstance(params_imshow, dict): + self.params_imshow = params_imshow + else: + raise ValueError('invalid params_imshow={}'.format(params_imshow)) + + if params_scatter is None: + self.params_scatter = {} + params_scatter['s'] = 10 + elif isinstance(params_scatter, dict): + self.params_scatter = params_scatter + else: + raise ValueError('invalid params_scatter={}'.format(params_scatter)) + if title_latent_space is None: self.title_latent_space = 'Latent space' else: @@ -363,11 +372,6 @@ def _initialize_to_visualize(self, n_grid_points, cmap_imshow, cmap_z, color_z, self.ax_latent_space = ax_latent_space self.ax_feature_bars = ax_feature_bars - self.cmap_imshow = cmap_imshow - self.cmap_z = cmap_z - self.color_z = color_z - self.marker_z = marker_z - self.interpolation = interpolation self.is_middle_color_zero = is_middle_color_zero self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) @@ -412,7 +416,7 @@ def _set_grid_values_to_draw(self, grid_values): self.selected_feature = None def _set_cmap(self, cmap): - self.cmap_imshow = cmap + self.params_imshow['cmap'] = cmap def _set_titles(self, title_latent_space, title_feature_bars): self.title_latent_space = title_latent_space @@ -463,10 +467,9 @@ def _draw_latent_space(self): coordinate_ax_right, coordinate_ax_bottom, coordinate_ax_top], - interpolation=self.interpolation, - cmap=self.cmap_imshow, vmin=vmin, - vmax=vmax) + vmax=vmax, + **self.params_imshow) ctr = self.ax_latent_space.contour(grid_points_3d[:, :, 0], grid_points_3d[:, :, 1], grid_values_to_contour, 6, colors='k') @@ -477,8 +480,7 @@ def _draw_latent_space(self): path_effects.Normal()]) # Plot latent variables - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], - s=10, c=self.color_z, cmap=self.cmap_z, marker=self.marker_z) + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], **self.params_scatter) # Write label if self.label_data is None: @@ -493,8 +495,6 @@ def _draw_latent_space(self): path_effects.Normal()]) else: if self.index_data_label_shown is not None: - # point_label = self.Z[self.index_data_label_shown,:] + self.noise_label[self.index_data_label_shown,:] - # label = self.label_data[self.index_data_label_shown] text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], self.Z[self.index_data_label_shown, 1], self.label_data[self.index_data_label_shown], @@ -551,4 +551,3 @@ def __unflatten_grid_array(self, grid_array): return np.squeeze(grid_array.reshape(np.append(self.n_grid_points, -1))) else: raise ValueError('arg shape {} is not consistent'.format(grid_array.shape)) - diff --git a/testfunc.py b/testfunc.py new file mode 100644 index 0000000..7869113 --- /dev/null +++ b/testfunc.py @@ -0,0 +1,9 @@ +def func_default(arg1=1, arg2=2, arg3=3): + print(arg1) + print(arg2) + print(arg3) + +none_dict = {} +print('dict type is {}'.format(type(none_dict))) +print(isinstance(none_dict, dict)) +func_default(4, arg2=2, **none_dict) \ No newline at end of file diff --git a/tutorials/ukr/fitting_animal_data.py b/tutorials/ukr/fitting_animal_data.py index 572484e..977f6b0 100644 --- a/tutorials/ukr/fitting_animal_data.py +++ b/tutorials/ukr/fitting_animal_data.py @@ -23,12 +23,17 @@ is_compact=is_compact, is_save_history=is_save_history, lambda_=lambda_, init=init) ukr.fit(nb_epoch=nb_epoch, eta=eta) - ukr.visualize(n_grid_points=100, - cmap_imshow='GnBu', - label_data=labels_animal, - label_feature=labels_feature, - title_latent_space='Animal map (latent space)', - title_feature_bars='Animal feature', - interpolation='spline16', - is_middle_color_zero=True, - is_show_all_label_data=True) + params_imshow = {'cmap': 'BrBG', + 'interpolation': 'spline16'} + params_scatter = {'s': 30, 'marker': 'x'} + ukr.visualize( + n_grid_points=100, + label_data=labels_animal, + label_feature=labels_feature, + is_middle_color_zero=True, + is_show_all_label_data=True, + params_imshow=params_imshow, + params_scatter=params_scatter, + title_latent_space='Animal map (latent space)', + title_feature_bars='Animal feature' + ) From f86f1a11e6b5fea1394a3753c80e3bb52254e5ca Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 11 Jun 2020 11:19:23 +0900 Subject: [PATCH 73/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89imsho?= =?UTF-8?q?w=E3=81=AEcmap=E3=81=AE=E3=81=BF=E3=82=92=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=82=92=E5=BB=83=E3=81=97=EF=BC=8Cimshow=E3=82=84scatter?= =?UTF-8?q?=E3=81=AE=E4=BB=BB=E6=84=8F=E3=81=AE=E3=83=91=E3=83=A9=E3=83=A1?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index e18f454..5ca1e36 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -415,8 +415,17 @@ def _set_grid_values_to_draw(self, grid_values): self.grid_values_to_draw = grid_values self.selected_feature = None - def _set_cmap(self, cmap): - self.params_imshow['cmap'] = cmap + def set_params_imshow(self, params: dict): + if isinstance(params, dict): + self.params_imshow.update(params) + else: + raise ValueError('invalid params={}') + + def set_params_scatter(self, params: dict): + if isinstance(params, dict): + self.params_scatter.update(params) + else: + raise ValueError('invalid params={}') def _set_titles(self, title_latent_space, title_feature_bars): self.title_latent_space = title_latent_space From ecb787c2ee4d6de395e072a0cb12a3e2d529ece0 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 11 Jun 2020 14:34:50 +0900 Subject: [PATCH 74/84] Fix bugs --- libs/models/unsupervised_kernel_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 5ca1e36..306930e 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -337,7 +337,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, if params_scatter is None: self.params_scatter = {} - params_scatter['s'] = 10 + self.params_scatter['s'] = 10 elif isinstance(params_scatter, dict): self.params_scatter = params_scatter else: From a654be667addb97c8a597ab28afc9cdd94076d4c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 11 Jun 2020 16:47:45 +0900 Subject: [PATCH 75/84] =?UTF-8?q?=E5=90=84=E3=83=87=E3=83=BC=E3=82=BF?= =?UTF-8?q?=E3=81=AB=E7=95=B0=E3=81=AA=E3=82=8Bmarker=E3=82=92=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 306930e..80711d5 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -176,7 +176,7 @@ def inverse_transform(self, Znew): return F def visualize(self, n_grid_points=30, label_data=None, label_feature=None, - is_show_all_label_data=False, is_middle_color_zero=False, + marker=None, is_show_all_label_data=False, is_middle_color_zero=False, params_imshow=None, params_scatter=None, title_latent_space=None, title_feature_bars=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): @@ -196,6 +196,8 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, The labels corresponds rows of the dataset X. label_feature: array of shape (n_features, ), optional. default = None The labels corresponds columns of the dataset X. + marker: MarkerStyle or its sequence, optional, default = None + The marker of scatter. It allows multiple markers. is_show_all_label_data: bool, optional, default = False When True the labels of the data is always shown. When False the label is only shown when the cursor overlaps the corresponding latent variable. @@ -285,7 +287,7 @@ def __mouse_over_fig(self, event): pass def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, - is_show_all_label_data, is_middle_color_zero, + marker, is_show_all_label_data, is_middle_color_zero, params_imshow, params_scatter, title_latent_space, title_feature_bars, fig, fig_size, ax_latent_space, ax_feature_bars): @@ -328,6 +330,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, else: raise ValueError('label_feature must be 1d array or list') + if params_imshow is None: self.params_imshow = {} elif isinstance(params_imshow, dict): @@ -343,6 +346,14 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, else: raise ValueError('invalid params_scatter={}'.format(params_scatter)) + if isinstance(marker, str): + self.params_scatter['marker'] = marker + self.multiple_marker = None + elif isinstance(marker, (list, tuple, np.ndarray)): + self.multiple_marker = np.array(marker) + else: + raise ValueError('invalid marker={}'.format(marker)) + if title_latent_space is None: self.title_latent_space = 'Latent space' else: @@ -489,8 +500,18 @@ def _draw_latent_space(self): path_effects.Normal()]) # Plot latent variables - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], **self.params_scatter) - + if self.multiple_marker is None: + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], **self.params_scatter) + else: + unique_markers = np.unique(self.multiple_marker) + for marker in unique_markers: + mask = (self.multiple_marker == marker) + self.ax_latent_space.scatter( + self.Z[mask, 0], + self.Z[mask, 1], + marker=marker, + **self.params_scatter + ) # Write label if self.label_data is None: pass From b96ea397d1ce61fb994a53b50f7ef9413737ee6c Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 16 Jul 2020 18:27:48 +0900 Subject: [PATCH 76/84] =?UTF-8?q?marker=E3=81=AE=E3=83=87=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=AB=E3=83=88=E5=80=A4=E3=81=AB=E8=AA=A4=E3=82=8A?= =?UTF-8?q?=E3=81=8C=E3=81=82=E3=81=A3=E3=81=9F=E3=81=9F=E3=82=81=E3=81=93?= =?UTF-8?q?=E3=82=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 80711d5..6ae1402 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -176,7 +176,7 @@ def inverse_transform(self, Znew): return F def visualize(self, n_grid_points=30, label_data=None, label_feature=None, - marker=None, is_show_all_label_data=False, is_middle_color_zero=False, + marker='.', is_show_all_label_data=False, is_middle_color_zero=False, params_imshow=None, params_scatter=None, title_latent_space=None, title_feature_bars=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): @@ -247,7 +247,8 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, fig=fig, fig_size=fig_size, ax_latent_space=ax_latent_space, - ax_feature_bars=ax_feature_bars) + ax_feature_bars=ax_feature_bars, + marker=marker) self._draw_latent_space() self._draw_feature_bars() From bc180b0eb4e2c1288dcaedd17164c1eee0072961 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 18 Aug 2020 14:48:13 +0900 Subject: [PATCH 77/84] =?UTF-8?q?=E3=83=87=E3=83=BC=E3=82=BF=E3=81=AEscatt?= =?UTF-8?q?er=E3=81=AEalpha=E3=82=92=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6ae1402..27e154d 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -391,6 +391,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.selected_feature = None self.grid_values_to_draw = None self.index_data_label_shown = None + self.alpha_scatter = np.ones(self.n_samples) def _set_grid(self, grid_points, n_grid_points): self.grid_points = grid_points @@ -439,6 +440,9 @@ def set_params_scatter(self, params: dict): else: raise ValueError('invalid params={}') + def set_alpha_scatter(self, alpha): + self.alpha_scatter = alpha + def _set_titles(self, title_latent_space, title_feature_bars): self.title_latent_space = title_latent_space self.title_feature_bars = title_feature_bars @@ -502,7 +506,9 @@ def _draw_latent_space(self): # Plot latent variables if self.multiple_marker is None: - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], **self.params_scatter) + self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], + alpha=self.alpha_scatter, + **self.params_scatter) else: unique_markers = np.unique(self.multiple_marker) for marker in unique_markers: @@ -511,6 +517,7 @@ def _draw_latent_space(self): self.Z[mask, 0], self.Z[mask, 1], marker=marker, + alpha=self.alpha_scatter[mask], **self.params_scatter ) # Write label From 88698b3e6cf8001c81176227c0c3c99d9a9a166e Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 18 Aug 2020 15:46:44 +0900 Subject: [PATCH 78/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B=E6=BD=9C=E5=9C=A8=E5=A4=89?= =?UTF-8?q?=E6=95=B0=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8Bmask?= =?UTF-8?q?=E9=85=8D=E5=88=97=E3=82=92=E5=B0=8E=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 27e154d..3179be3 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -391,7 +391,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.selected_feature = None self.grid_values_to_draw = None self.index_data_label_shown = None - self.alpha_scatter = np.ones(self.n_samples) + self.mask_latent_variables = np.full(self.n_samples, True, bool) def _set_grid(self, grid_points, n_grid_points): self.grid_points = grid_points @@ -440,8 +440,8 @@ def set_params_scatter(self, params: dict): else: raise ValueError('invalid params={}') - def set_alpha_scatter(self, alpha): - self.alpha_scatter = alpha + def set_mask_latent_variables(self, mask): + self.mask_latent_variables = mask def _set_titles(self, title_latent_space, title_feature_bars): self.title_latent_space = title_latent_space @@ -506,18 +506,18 @@ def _draw_latent_space(self): # Plot latent variables if self.multiple_marker is None: - self.ax_latent_space.scatter(self.Z[:, 0], self.Z[:, 1], - alpha=self.alpha_scatter, + self.ax_latent_space.scatter(self.Z[self.mask_latent_variables, 0], + self.Z[self.mask_latent_variables, 1], **self.params_scatter) else: unique_markers = np.unique(self.multiple_marker) for marker in unique_markers: mask = (self.multiple_marker == marker) + mask = self.mask_latent_variables & mask self.ax_latent_space.scatter( self.Z[mask, 0], self.Z[mask, 1], marker=marker, - alpha=self.alpha_scatter[mask], **self.params_scatter ) # Write label @@ -525,7 +525,9 @@ def _draw_latent_space(self): pass else: if self.is_show_all_label_data: - for z, noise, label in zip(self.Z, self.noise_label, self.label_data): + for z, noise, label in zip(self.Z[self.mask_latent_variables], + self.noise_label[self.mask_latent_variables], + self.label_data[self.mask_latent_variables]): point_label = z + noise text = self.ax_latent_space.text(point_label[0], point_label[1], label, ha='center', va='bottom', color='black') @@ -533,13 +535,14 @@ def _draw_latent_space(self): path_effects.Normal()]) else: if self.index_data_label_shown is not None: - text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], - self.Z[self.index_data_label_shown, 1], - self.label_data[self.index_data_label_shown], - ha='center', va='bottom', color='black') - text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), - path_effects.Normal()] - ) + if self.mask_latent_variables[self.mask_latent_variables]: + text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], + self.Z[self.index_data_label_shown, 1], + self.label_data[self.index_data_label_shown], + ha='center', va='bottom', color='black') + text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='white'), + path_effects.Normal()] + ) else: pass From 189bd43d13508515859592314ea84e4c0884a064 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 18 Aug 2020 16:15:13 +0900 Subject: [PATCH 79/84] fix bug and adjust range of figure --- libs/models/unsupervised_kernel_regression.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 3179be3..8b9be7a 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -452,6 +452,8 @@ def _draw_latent_space(self): from matplotlib import patheffects as path_effects self.ax_latent_space.cla() self.ax_latent_space.set_title(self.title_latent_space) + self.ax_latent_space.set_xlim(self.Z[:, 0].min() * 1.05, self.Z[:, 0].max() * 1.05) + self.ax_latent_space.set_ylim(self.Z[:, 1].min() * 1.05, self.Z[:, 1].max() * 1.05) # Draw color using self.grid_values_to_draw by pcolormesh and contour if self.grid_values_to_draw is not None: @@ -535,7 +537,7 @@ def _draw_latent_space(self): path_effects.Normal()]) else: if self.index_data_label_shown is not None: - if self.mask_latent_variables[self.mask_latent_variables]: + if self.mask_latent_variables[self.index_data_label_shown]: text = self.ax_latent_space.text(self.Z[self.index_data_label_shown, 0], self.Z[self.index_data_label_shown, 1], self.label_data[self.index_data_label_shown], From ea07a4cf760a343bdbc9ffe1119e9fc230607bea Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 18 Aug 2020 18:29:51 +0900 Subject: [PATCH 80/84] =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93?= =?UTF-8?q?=E4=B8=8A=E3=81=AE=E3=82=AF=E3=83=AA=E3=83=83=E3=82=AF=E6=B8=88?= =?UTF-8?q?=E3=81=BF=E3=81=AE=E7=82=B9=E3=82=92=E3=82=82=E3=81=86=E4=B8=80?= =?UTF-8?q?=E5=BA=A6=E3=82=AF=E3=83=AA=E3=83=83=E3=82=AF=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=81=A8=E8=A7=A3=E9=99=A4=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 8b9be7a..41da556 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -405,11 +405,24 @@ def _set_grid(self, grid_points, n_grid_points): self.noise_label = epsilon * (np.random.rand(self.n_samples, self.n_components) * 2.0 - 1.0) def _set_feature_bar_from_latent_space(self, click_coordinates): - # クリックしたところといちばん近い代表点がどこかを計算 - self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) + if self.click_point_latent_space is not None: + previous_coordinate = self.grid_points[self.click_point_latent_space] + dist = np.sqrt(np.sum(np.square(previous_coordinate - click_coordinates))) + epsilon = 0.02 * np.abs(self.grid_points.max() - self.grid_points.min()) + if dist < epsilon: + is_uncondition = True + else: + is_uncondition = False + + if is_uncondition: + self.click_point_latent_space = None + self.clicked_mapping = self.X.mean(axis=0) + else: + # クリックしたところといちばん近い代表点がどこかを計算 + self.click_point_latent_space = self.__calc_nearest_grid_point(click_coordinates) - # その代表点の写像先の特徴量を計算 - self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] + # その代表点の写像先の特徴量を計算 + self.clicked_mapping = self.grid_mapping[self.click_point_latent_space, :] def _set_latent_space_from_feature_bar(self, click_coordinates): for i, bar in enumerate(self.feature_bars): @@ -552,7 +565,9 @@ def _draw_latent_space(self): if self.click_point_latent_space is None: pass else: - self.__draw_click_point_latent_space() + coordinate = self.grid_points[self.click_point_latent_space] + self.ax_latent_space.plot(coordinate[0], coordinate[1], + ".", color="red", ms=20, fillstyle="none") self.fig.show() @@ -570,11 +585,6 @@ def _draw_feature_bars(self): self.ax_feature_bars.set_xticklabels(labels=self.label_feature, fontsize=8, rotation=270) self.fig.show() - def __draw_click_point_latent_space(self): - coordinate = self.grid_points[self.click_point_latent_space] - self.ax_latent_space.plot(coordinate[0], coordinate[1], - ".", color="red", ms=20, fillstyle="none") - def __calc_nearest_grid_point(self, click_coordinates): distance = dist.cdist(self.grid_points, click_coordinates[None, :]) index_nearest = np.argmin(distance.ravel()) From 07168e759fb27820ce743577090595b1afbb5fb8 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Tue, 18 Aug 2020 18:44:45 +0900 Subject: [PATCH 81/84] Fix bugs --- libs/models/unsupervised_kernel_regression.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 41da556..1c00da6 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -411,6 +411,8 @@ def _set_feature_bar_from_latent_space(self, click_coordinates): epsilon = 0.02 * np.abs(self.grid_points.max() - self.grid_points.min()) if dist < epsilon: is_uncondition = True + else: + is_uncondition = False else: is_uncondition = False From 802891e6f8a5ca345730a8b2be6e12fecf6bb0df Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Thu, 20 Aug 2020 21:05:02 +0900 Subject: [PATCH 82/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89x?= =?UTF-8?q?=E5=8D=B0=E3=81=A7plot=E3=81=99=E3=82=8B=E7=82=B9=E3=82=92?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 1c00da6..6b4c61f 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -392,6 +392,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.grid_values_to_draw = None self.index_data_label_shown = None self.mask_latent_variables = np.full(self.n_samples, True, bool) + self.cross_points = None def _set_grid(self, grid_points, n_grid_points): self.grid_points = grid_points @@ -458,9 +459,14 @@ def set_params_scatter(self, params: dict): def set_mask_latent_variables(self, mask): self.mask_latent_variables = mask - def _set_titles(self, title_latent_space, title_feature_bars): - self.title_latent_space = title_latent_space - self.title_feature_bars = title_feature_bars + def set_titles(self, title_latent_space=None, title_feature_bars=None): + if title_latent_space is not None: + self.title_latent_space = title_latent_space + if title_feature_bars is not None: + self.title_feature_bars = title_feature_bars + + def set_scatter_cross(self, coordinates): + self.cross_points = coordinates def _draw_latent_space(self): import matplotlib.pyplot as plt @@ -571,6 +577,13 @@ def _draw_latent_space(self): self.ax_latent_space.plot(coordinate[0], coordinate[1], ".", color="red", ms=20, fillstyle="none") + if self.cross_points is None: + pass + else: + self.ax_latent_space.scatter(self.cross_points.reshape(-1, self.n_components)[:, 0], + self.cross_points.reshape(-1, self.n_components)[:, 1], + marker='x', color='k', s=20) + self.fig.show() def _draw_feature_bars(self): From d951201bdaf54a72012e4ef9c2e58dd68cd38c46 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 7 Sep 2020 10:53:06 +0900 Subject: [PATCH 83/84] =?UTF-8?q?latent=20space=E3=81=AE=E8=BB=B8=E3=81=AE?= =?UTF-8?q?=E3=83=A9=E3=83=99=E3=83=AB=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 6b4c61f..859e738 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -177,6 +177,7 @@ def inverse_transform(self, Znew): def visualize(self, n_grid_points=30, label_data=None, label_feature=None, marker='.', is_show_all_label_data=False, is_middle_color_zero=False, + is_show_ticks_latent_space=False, params_imshow=None, params_scatter=None, title_latent_space=None, title_feature_bars=None, fig=None, fig_size=None, ax_latent_space=None, ax_feature_bars=None): @@ -205,6 +206,8 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, If `True`, the value corresponding to middle color in the colormap is fixed at 0. If the data is normalized to zero mean and it is important whether it is higher or lower than the mean, setting to `True` makes the coloring easier to read. + is_show_ticks_latent_space: bool, optional, default = False + When True the ticks and its label of latent space is shown. params_imshow: dict, optional, default = None The dictionary of kwargs in imshow used for drawing the value of the feature in the latent space. See the official document of matplotlib.pyplot.imshow for more information on available arguments. @@ -244,6 +247,7 @@ def visualize(self, n_grid_points=30, label_data=None, label_feature=None, title_feature_bars=title_feature_bars, is_show_all_label_data=is_show_all_label_data, is_middle_color_zero=is_middle_color_zero, + is_show_ticks_latent_space=is_show_ticks_latent_space, fig=fig, fig_size=fig_size, ax_latent_space=ax_latent_space, @@ -289,6 +293,7 @@ def __mouse_over_fig(self, event): def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, marker, is_show_all_label_data, is_middle_color_zero, + is_show_ticks_latent_space, params_imshow, params_scatter, title_latent_space, title_feature_bars, fig, fig_size, ax_latent_space, ax_feature_bars): @@ -385,6 +390,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.ax_feature_bars = ax_feature_bars self.is_middle_color_zero = is_middle_color_zero + self.is_show_ticks_latent_space = is_show_ticks_latent_space self.click_point_latent_space = None # index of the clicked representative point self.clicked_mapping = self.X.mean(axis=0) self.is_initial_view = True @@ -584,6 +590,16 @@ def _draw_latent_space(self): self.cross_points.reshape(-1, self.n_components)[:, 1], marker='x', color='k', s=20) + if not self.is_show_ticks_latent_space: + self.ax_latent_space.tick_params(labelbottom=False, + labelleft=False, + labelright=False, + labeltop=False) + self.ax_latent_space.tick_params(bottom=False, + left=False, + right=False, + top=False) + self.fig.show() def _draw_feature_bars(self): From b4ab7a01861573f27129b014a63edafa3bdf9fa5 Mon Sep 17 00:00:00 2001 From: Ryuji Watanabe Date: Mon, 7 Sep 2020 14:34:50 +0900 Subject: [PATCH 84/84] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=81=8B=E3=82=89?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=A9=BA=E9=96=93=E3=81=AB=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/models/unsupervised_kernel_regression.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libs/models/unsupervised_kernel_regression.py b/libs/models/unsupervised_kernel_regression.py index 859e738..0ff22b7 100644 --- a/libs/models/unsupervised_kernel_regression.py +++ b/libs/models/unsupervised_kernel_regression.py @@ -399,6 +399,7 @@ def _initialize_to_visualize(self, n_grid_points, label_data, label_feature, self.index_data_label_shown = None self.mask_latent_variables = np.full(self.n_samples, True, bool) self.cross_points = None + self.comment_latent_space = None def _set_grid(self, grid_points, n_grid_points): self.grid_points = grid_points @@ -474,6 +475,10 @@ def set_titles(self, title_latent_space=None, title_feature_bars=None): def set_scatter_cross(self, coordinates): self.cross_points = coordinates + + def set_comment_in_latent_space(self, comment: str): + self.comment_latent_space = comment + def _draw_latent_space(self): import matplotlib.pyplot as plt from matplotlib import patheffects as path_effects @@ -590,6 +595,14 @@ def _draw_latent_space(self): self.cross_points.reshape(-1, self.n_components)[:, 1], marker='x', color='k', s=20) + if self.comment_latent_space is not None: + self.ax_latent_space.text(1.0, 0.0, + self.comment_latent_space, + horizontalalignment='right', + verticalalignment='top', + transform=self.ax_latent_space.transAxes + ) + if not self.is_show_ticks_latent_space: self.ax_latent_space.tick_params(labelbottom=False, labelleft=False,