diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2020-07-24 19:51:32 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2020-07-24 19:51:32 +0200 |
commit | 9c0dfa59598968ab5be656aac5c4f6f79c50dd61 (patch) | |
tree | 93c58b8a6c296ce02d029cc1885743e924284090 | |
parent | 49f34e4d60b25797c04c4a6a903748e3cb4b992f (diff) |
Simplify and clarify PCA code.
-rw-r--r-- | otsu2018.py | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/otsu2018.py b/otsu2018.py index b99e563..b17c518 100644 --- a/otsu2018.py +++ b/otsu2018.py @@ -223,14 +223,11 @@ class Node: if not self.leaf: raise RuntimeError('Node.PCA called for a node that is not a leaf') - # https://dev.to/akaame/implementing-simple-pca-using-numpy-3k0a self.mean = np.mean(self.colours.reflectances, axis=0) - data = self.colours.reflectances - self.mean - cov = np.cov(data.T) / data.shape[0] - v, w = np.linalg.eig(cov) - idx = v.argsort()[::-1] - w = w[:, idx] - self.basis_functions = np.real(w[:, :3].T) + data_matrix = self.colours.reflectances - self.mean + covariance_matrix = np.dot(data_matrix.T, data_matrix) + _eigenvalues, eigenvectors = np.linalg.eigh(covariance_matrix) + self.basis_functions = eigenvectors[:, -3:].T # TODO: better names M = np.empty((3, 3)) |