def test_frob_norm(self): """ Check Frobenius norm is calculated correctly whether the full or partial PCA is computed. """ true_frob_norm = np.linalg.norm(self.X_cent, ord='fro') pca = PCA(n_components=None).fit(self.X) self.assertTrue(np.allclose(pca.frob_norm_, true_frob_norm)) # TODO: this is failing, it could be a numerical issue. pca = PCA(n_components=3).fit(self.X) self.assertTrue(np.allclose(pca.frob_norm_, true_frob_norm))
def test_reconstruction(self): """ We can reconstruct the original data matrix exactly from the full reconstruction. """ pca = PCA().fit(self.X) self.assertTrue(np.allclose(self.X, pca.predict_reconstruction()))
def svals(data, joint): init = 0 last = 0 previous_step = 0 for i in PCA().fit(data).svals_: if last - i < 0.2 * previous_step: init += 1 previous_step = last - i last = i return max(init, joint)
def test_centering(self): """ Make sure PCA computes the correct centers. Also check center=False works correctly. """ self.assertTrue(np.allclose(self.pca.m_, self.X.mean(axis=0))) # no centering pca = PCA(n_components=4, center=False).fit(self.X) self.assertTrue(pca.m_ is None) Z = np.random.normal(size=(20, self.X.shape[1])) V = pca.loadings_.values self.assertTrue(np.allclose(pca.predict_scores(Z), np.dot(Z, V)))
def setUp(self): n = 100 d = 20 n_components = 10 obs_names = ['sample_{}'.format(i) for i in range(n)] var_names = ['var_{}'.format(i) for i in range(d)] X = pd.DataFrame(np.random.normal(size=(n, d)), index=obs_names, columns=var_names) X_cent = X - X.mean(axis=0) pca = PCA(n_components=n_components).fit(X) # store these for testing self.n = n self.d = d self.n_components = n_components self.obs_names = obs_names self.var_names = var_names self.X = X self.X_cent = X_cent self.pca = pca
from jive.AJIVE import AJIVE from jive.PCA import PCA from jive.ajive_fig2 import generate_data_ajive_fig2 from jive.viz.block_visualization import data_block_heatmaps, jive_full_estimate_heatmaps import matplotlib.pyplot as plt X, Y = generate_data_ajive_fig2() plt.figure(figsize=[6.5, 3]) data_block_heatmaps({'x': X, 'y': Y}) plt.savefig('figures/data_heatmaps.png', bbox_inches='tight') plt.close() # determine initial signal ranks by inspecting scree plots plt.figure(figsize=[8.4, 3]) plt.subplot(1, 2, 1) PCA().fit(X).plot_scree() plt.subplot(1, 2, 2) PCA().fit(Y).plot_scree() plt.savefig('figures/scree_plots.png', bbox_inches='tight') plt.close() ajive = AJIVE(init_signal_ranks={'x': 2, 'y': 3}) ajive.fit(blocks={'x': X, 'y': Y}) plt.figure(figsize=[6.5, 12]) jive_full_estimate_heatmaps(ajive.get_full_block_estimates(), blocks={ 'x': X, 'y': Y }) plt.savefig('figures/jive_estimate_heatmaps.png', bbox_inches='tight')