def plot_latent(self, cur_epoch=''): # Generating latent space print('Generating latent space ...') latent_en = self.encode(self.data_plot.x) pca = PCA(n_components=2) latent_pca = latent_en if latent_en.shape[1] == 2 else latent_en[:, 0:2] if latent_en.shape[1]==3 else pca.fit_transform(latent_en) print('Latent space dimensions: {}'.format(latent_pca.shape)) print('Plotting latent space ...') latent_space = self.config.log_dir + '/latent2d/{} latent epoch {}.jpg'.format(self.config.log_dir.split('/')[-1:][0], cur_epoch) self.latent_space_files.append(latent_space) plot_dataset(latent_pca.compute(), y=self.data_plot.labels, save=latent_space) if latent_en.shape[1] >= 3: pca = PCA(n_components=3) latent_pca = latent_en if latent_en.shape[1]==3 else pca.fit_transform(latent_en) print('latent space dimensions: {}'.format(latent_pca.shape)) print('Plotting latent space ...') latent_space = self.config.log_dir + '/latent3d/{} latent_3d epoch {}.jpg'.format(self.config.log_dir.split('/')[-1:][0], cur_epoch) self.latent_space3d_files.append(latent_space) plot_dataset3d(latent_pca.compute(), y=self.data_plot.labels, save=latent_space) del latent_pca, latent_en gc.collect()
def do_pca(g, n_comp): """ Perform a PCA on the genetic array and return n_comp of it :param g: Genotype array :param n_comp: Number of components sought :return: components array """ pca = PCA(n_components=n_comp) pca = pca.fit_transform(g) return pca
def do_pca(g, n_comp): """ Perform a PCA on the genetic array and return n_comp of it :param g: Genotype array :param n_comp: Number of components sought :return: components array """ cache = Chest(available_memory=available_memory, path=os.getcwd()) pca = PCA(n_components=n_comp) pca = pca.fit_transform(g) return pca.compute(cache=cache)
def generate_samples(self, data, session, cur_epoch=''): # Generating W space print('Generating W space ...') w_en = self.encode(data.x) pca = PCA(n_components=2) W_pca = pca.fit_transform(w_en) print('W space dimensions: {}'.format(W_pca.shape)) print('Ploting W space ...') w_space = self.summary_dir + '/{} W space in epoch {}.jpg'.format( self.summary_dir.split('/')[-1:][0], cur_epoch) self.w_space_files.append(w_space) plot_dataset(W_pca.compute(), y=data.labels, save=w_space) pca = PCA(n_components=3) W_pca = pca.fit_transform(w_en) print('W space dimensions: {}'.format(W_pca.shape)) print('Ploting W space ...') w_space = self.summary_dir + '/{} W space 3d in epoch {}.jpg'.format( self.summary_dir.split('/')[-1:][0], cur_epoch) self.w_space3d_files.append(w_space) plot_dataset3d(W_pca.compute(), y=data.labels, save=w_space) del W_pca, w_en gc.collect() # Generating Samples print('Generating Samples ...') x_recons_l = self.reconst(data.samples) recons_file = self.summary_dir + '/{} samples generation in epoch {}.jpg'.format( self.summary_dir.split('/')[-1:][0], cur_epoch) self.recons_files.append(recons_file) plot_samples(x_recons_l, scale=10, save=recons_file) del x_recons_l gc.collect()
def plot_latent(self, cur_epoch=''): # Generating latent space if self.latent_data is None: self.generate_latent(self.data_train, self.session, self.config.ntrain_batches) pca = PCA(n_components=2) latent_pca = self.latent_data['latent'] if self.latent_data['latent'].shape[1] == 2 \ else self.latent_data['latent'][:, 0:2] if self.latent_data['latent'].shape[1]==3 \ else pca.fit_transform(self.latent_data['latent']) print('Latent space dimensions: {}'.format(latent_pca.shape)) print('Plotting latent space ...') latent_space = self.config.log_dir + '/latent2d/{} latent epoch {}.jpg'.format( self.config.log_dir.split('/')[-1:][0], cur_epoch) plot_dataset( latent_pca.compute(), y=self.latent_data['label'][:, self.latent_data['y_index']].compute(), save=latent_space) if self.latent_data['latent'].shape[1] >= 3: pca = PCA(n_components=3) latent_pca = self.latent_data['latent'] if self.latent_data['latent'].shape[1]==3 \ else pca.fit_transform(self.latent_data['latent']) print('latent space dimensions: {}'.format(latent_pca.shape)) print('Plotting latent space ...') latent_space = self.config.log_dir + '/latent3d/{} latent_3d epoch {}.jpg'.format( self.config.log_dir.split('/')[-1:][0], cur_epoch) plot_dataset3d(latent_pca.compute(), y=self.latent_data['label'] [:, self.latent_data['y_index']].compute(), save=latent_space) del latent_pca gc.collect()
def pca(dataset: DataSet, num_components: int): pca = PCA(n_components=num_components) reduced = pca.fit_transform(dataset.feature_vector) components = pca.components_ return DataSet(reduced), components