From 9c0dfa59598968ab5be656aac5c4f6f79c50dd61 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Fri, 24 Jul 2020 19:51:32 +0200 Subject: Simplify and clarify PCA code. --- otsu2018.py | 11 ++++------- 1 file 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)) -- cgit