def test_PCA_scorer_component(self): pca = PCA() for i in range(1, len(self.zoo.domain.attributes) + 1): pca.component = i scores = pca.score_data(self.zoo) self.assertEqual(scores.shape, (pca.component, len(self.zoo.domain.attributes)))
def test_PCA_scorer(self): data = self.iris pca = PCA(preprocessors=[Normalize()]) pca.component = 1 scores = pca.score_data(data) self.assertEqual(scores.shape[1], len(data.domain.attributes)) self.assertEqual(['petal length', 'petal width'], sorted([data.domain.attributes[i].name for i in np.argsort(scores[0])[-2:]])) self.assertEqual([round(s, 4) for s in scores[0]], [0.5224, 0.2634, 0.5813, 0.5656])
def _get_pca(self): pca_projector = PCA(n_components=2) pca_projector.component = 2 pca_projector.preprocessors = PCA.preprocessors + [Normalize()] pca = pca_projector(self.data) variance_ratio = pca.explained_variance_ratio_ cumulative = np.cumsum(variance_ratio) self._pca = pca if not np.isfinite(cumulative[-1]): self.Warning.trivial_components() coords = pca(self.data).X valid_mask = ~np.isnan(coords).any(axis=1) # scale axes max_radius = np.min([np.abs(np.min(coords, axis=0)), np.max(coords, axis=0)]) axes = pca.components_.T.copy() axes *= max_radius / np.max(np.linalg.norm(axes, axis=1)) return valid_mask, coords, axes