def test_apply_reduce_num_components(self): """ Checks whether PCA performs dimensionality reduction """ sut = PCA(self.num_components - 1, whiten=True) sut.apply(self.dataset, True) assert self.dataset.get_design_matrix().shape[1] ==\ self.num_components - 1
def test_apply_whiten(self): """ Confirms that PCA has decorrelated the input dataset and variance is the same along all principal components and equal to one """ sut = PCA(self.num_components, whiten=True) sut.apply(self.dataset, True) cm = np.cov(self.dataset.get_design_matrix().T) # covariance matrix # testing whether the covariance matrix is a diagonal one np.testing.assert_almost_equal(cm * (np.ones(cm.shape[0]) - np.eye(cm.shape[0])), np.zeros((cm.shape[0], cm.shape[0]))) # testing whether the eigenvalues are all ones np.testing.assert_almost_equal(np.diag(cm), np.ones(cm.shape[0]))
def test_apply_whiten(self): """ Confirms that PCA has decorrelated the input dataset and variance is the same along all principal components and equal to one """ sut = PCA(self.num_components, whiten=True) sut.apply(self.dataset, True) cm = np.cov(self.dataset.get_design_matrix().T) # covariance matrix # testing whether the covariance matrix is a diagonal one np.testing.assert_almost_equal( cm * (np.ones(cm.shape[0]) - np.eye(cm.shape[0])), np.zeros((cm.shape[0], cm.shape[0]))) # testing whether the eigenvalues are all ones np.testing.assert_almost_equal(np.diag(cm), np.ones(cm.shape[0]))
def test_apply_no_whiten(self): """ Confirms that PCA has decorrelated the input dataset and principal components are arranged in decreasing order by variance """ # sut is an abbreviation for System Under Test sut = PCA(self.num_components) sut.apply(self.dataset, True) cm = np.cov(self.dataset.get_design_matrix().T) # covariance matrix # testing whether the covariance matrix is a diagonal one np.testing.assert_almost_equal(cm * (np.ones(cm.shape[0]) - np.eye(cm.shape[0])), np.zeros((cm.shape[0], cm.shape[0]))) # testing whether the eigenvalues are in decreasing order assert (np.diag(cm)[:-1] > np.diag(cm)[1:]).all()
def test_apply_no_whiten(self): """ Confirms that PCA has decorrelated the input dataset and principal components are arranged in decreasing order by variance """ # sut is an abbreviation for System Under Test sut = PCA(self.num_components) sut.apply(self.dataset, True) cm = np.cov(self.dataset.get_design_matrix().T) # covariance matrix # testing whether the covariance matrix is a diagonal one np.testing.assert_almost_equal( cm * (np.ones(cm.shape[0]) - np.eye(cm.shape[0])), np.zeros((cm.shape[0], cm.shape[0]))) # testing whether the eigenvalues are in decreasing order assert (np.diag(cm)[:-1] > np.diag(cm)[1:]).all()