예제 #1
0
 def testSorting(self):
     """components should be sorted by decreasing variance
     """
     x = generate_covsig(np.diag([1, 9, 2, 6, 3, 8, 4, 5, 7]), 500)
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))
     w, v = pca(x, sort_components=True)
     c = np.cov(x.dot(w).T)
     self.assertTrue(np.allclose(c, np.diag([9, 8, 7, 6, 5, 4, 3, 2, 1]), rtol=1e-1, atol=1e-2))
예제 #2
0
 def testDecorrelation(self):
     """components should be decorrelated after PCA
     """
     x = generate_covsig([[3, 2, 1], [2, 3, 2], [1, 2, 3]], 500)
     w, v = pca(x)
     c = np.cov(x.dot(w).T)
     c -= np.diag(c.diagonal())
     self.assertTrue(np.allclose(c, np.zeros((3, 3)), rtol=1e-2, atol=1e-3))
예제 #3
0
    def testInverse(self):
        i = np.abs(self.v.dot(self.w))
        self.assertTrue(np.abs(np.mean(i.diagonal())) - 1 < epsilon)
        self.assertTrue(np.abs(np.sum(i) - i.trace()) < epsilon)

        w, v = pca(self.x, subtract_mean=True, normalize=True)
        i = np.abs(v.dot(w))
        self.assertTrue(np.abs(np.mean(i.diagonal())) - 1 < epsilon)
        self.assertTrue(np.abs(np.sum(i) - i.trace()) < epsilon)
예제 #4
0
 def testIdentity(self):
     """identity covariance in -> identity covariance out
        test for up to 50 dimensions
     """
     for i in range(1, 50):
         x = generate_covsig(np.eye(i), 500)
         w, v = pca(x)
         c = np.cov(w.dot(x.T))
         self.assertTrue(np.allclose(c, np.eye(i)))
예제 #5
0
 def setUp(self):
     self.x = np.random.rand(100, 10)
     self.y = self.x.copy()
     self.n, self.m = self.x.shape
     self.w1, self.v1 = pca(self.x, reducedim=0.9)
     self.w2, self.v2 = pca(self.x, reducedim=5)
예제 #6
0
    def testInputSafety(self):
        self.assertTrue((self.x == self.y).all())

        pca(self.x, subtract_mean=True, normalize=True)
        self.assertTrue((self.x == self.y).all())
예제 #7
0
 def setUp(self):
     self.x = np.random.rand(100, 10)
     self.y = self.x.copy()
     self.n, self.m = self.x.shape
     self.w, self.v = pca(self.x)