Takes a covariance matrix (np.ndarray) as input.
(self, cov)
| 641 | self.mean = None |
| 642 | |
| 643 | def train_pca(self, cov): |
| 644 | """ |
| 645 | Takes a covariance matrix (np.ndarray) as input. |
| 646 | """ |
| 647 | d, v = np.linalg.eigh(cov) |
| 648 | eps = d.max() * 1e-5 |
| 649 | n_0 = (d < eps).sum() |
| 650 | if n_0 > 0: |
| 651 | d[d < eps] = eps |
| 652 | |
| 653 | # total energy |
| 654 | totenergy = d.sum() |
| 655 | |
| 656 | # sort eigenvectors with eigenvalues order |
| 657 | idx = np.argsort(d)[::-1][:self.dim] |
| 658 | d = d[idx] |
| 659 | v = v[:, idx] |
| 660 | |
| 661 | print("keeping %.2f %% of the energy" % (d.sum() / totenergy * 100.0)) |
| 662 | |
| 663 | # for the whitening |
| 664 | d = np.diag(1. / d**self.whit) |
| 665 | |
| 666 | # principal components |
| 667 | self.dvt = np.dot(d, v.T) |
| 668 | |
| 669 | def apply(self, x): |
| 670 | # input is from numpy |