def test_singular_returns_pseudo_inverse(self): """Checks that if the input covariance matrix is singular, we return the pseudo inverse""" X, y = load_iris(return_X_y=True) # We add a virtual column that is a linear combination of the other # columns so that the covariance matrix will be singular X = np.concatenate([X, X[:, :2].dot([[2], [3]])], axis=1) cov_matrix = np.cov(X, rowvar=False) covariance = Covariance() covariance.fit(X) pseudo_inverse = covariance.get_mahalanobis_matrix() # here is the definition of a pseudo inverse according to wikipedia: assert_allclose( cov_matrix.dot(pseudo_inverse).dot(cov_matrix), cov_matrix) assert_allclose( pseudo_inverse.dot(cov_matrix).dot(pseudo_inverse), pseudo_inverse)
def test_cov(self): cov = Covariance() cov.fit(self.X) L = cov.components_ assert_array_almost_equal(L.T.dot(L), cov.get_mahalanobis_matrix())
def test_cov(self): cov = Covariance() cov.fit(self.X) L = cov.transformer_ assert_array_almost_equal(L.T.dot(L), cov.get_mahalanobis_matrix())