Beispiel #1
0
    def test02PCAScaleOnly(self):
        '''
        This is the R result.
        
        Standard deviations:
        [1] 1.5649625 0.7209789 0.1762999
        
        Rotation:
                    PC1        PC2        PC3
        [1,]  0.6261964 -0.2060872 -0.7519349
        [2,] -0.5082884 -0.8392166 -0.1932835
        [3,] -0.5912030  0.5032332 -0.6302661
        
        Scale:
        [1] 1.415300 2.449408 2.032120
        '''
        stdev,rot,scale = pv.prcomp(self.data,center=False,scale=True)
        
        r_stdev = [1.5649625, 0.7209789, 0.1762999]
        r_rot = [0.6261964, -0.2060872, -0.7519349, -0.5082884, -0.8392166, -0.1932835, -0.5912030,  0.5032332, -0.6302661]
        r_scale = [1.415300, 2.449408, 2.032120]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
        self.assertLess(np.abs(scale.flatten() - r_scale).max(), 0.0001)
Beispiel #2
0
 def test01PCACenterOnly(self):
     '''
     This is the R result.
     
             Standard deviations:
     [1] 2.9519567 1.5879939 0.2839961
     
     Rotation:
                 PC1        PC2       PC3
     [1,]  0.4204812  0.2913070 0.8592647
     [2,] -0.7281257  0.6733826 0.1280189
     [3,] -0.5413211 -0.6794822 0.4952529
     
     center:
     [1] -0.4235626  0.4724063  0.6102422
     '''
     stdev,rot,center = pv.prcomp(self.data,center=True,scale=False)
     
     r_stdev = [2.9519567, 1.5879939, 0.2839961]
     r_rot = [0.4204812, 0.2913070, 0.8592647, -0.7281257, 0.6733826, 0.1280189, -0.5413211, -0.6794822, 0.4952529]
     r_center = [-0.4235626, 0.4724063, 0.6102422]
     
     self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
     self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
     self.assertLess(np.abs(center - r_center).max(), 0.0001)
Beispiel #3
0
    def test02PCAAll(self):
        '''
        This is the R result.
        
        Standard deviations:
        [1] 1.5550735 0.7399330 0.1850559
        
        Rotation:
                    PC1        PC2        PC3
        [1,]  0.6297830 -0.1983517 -0.7510193
        [2,] -0.5027915 -0.8410733 -0.1994906
        [3,] -0.5920929  0.5032419 -0.6294232
        
        Center:
        [1] -0.4235626  0.4724063  0.6102422
        
        Scale
        [1] 1.346932 2.400976 1.933266
        '''
        stdev,rot,center,scale = pv.prcomp(self.data,center=True,scale=True)
        
        r_stdev = [1.5550735, 0.7399330, 0.1850559]
        r_rot = [0.6297830, -0.1983517, -0.7510193, -0.5027915, -0.8410733, -0.1994906, -0.5920929, 0.5032419, -0.6294232]
        r_scale = [1.346932, 2.400976, 1.933266]
        r_center = [-0.4235626, 0.4724063, 0.6102422]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
        self.assertLess(np.abs(scale.flatten() - r_scale).max(), 0.0001)
        self.assertLess(np.abs(center - r_center).max(), 0.0001)
Beispiel #4
0
    def test01PCACenterOnly(self):
        '''
        This is the R result.
        
                Standard deviations:
        [1] 2.9519567 1.5879939 0.2839961
        
        Rotation:
                    PC1        PC2       PC3
        [1,]  0.4204812  0.2913070 0.8592647
        [2,] -0.7281257  0.6733826 0.1280189
        [3,] -0.5413211 -0.6794822 0.4952529
        
        center:
        [1] -0.4235626  0.4724063  0.6102422
        '''
        stdev, rot, center = pv.prcomp(self.data, center=True, scale=False)

        r_stdev = [2.9519567, 1.5879939, 0.2839961]
        r_rot = [
            0.4204812, 0.2913070, 0.8592647, -0.7281257, 0.6733826, 0.1280189,
            -0.5413211, -0.6794822, 0.4952529
        ]
        r_center = [-0.4235626, 0.4724063, 0.6102422]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
        self.assertLess(np.abs(center - r_center).max(), 0.0001)
Beispiel #5
0
    def test02PCAAll(self):
        '''
        This is the R result.
        
        Standard deviations:
        [1] 1.5550735 0.7399330 0.1850559
        
        Rotation:
                    PC1        PC2        PC3
        [1,]  0.6297830 -0.1983517 -0.7510193
        [2,] -0.5027915 -0.8410733 -0.1994906
        [3,] -0.5920929  0.5032419 -0.6294232
        
        Center:
        [1] -0.4235626  0.4724063  0.6102422
        
        Scale
        [1] 1.346932 2.400976 1.933266
        '''
        stdev, rot, center, scale = pv.prcomp(self.data,
                                              center=True,
                                              scale=True)

        r_stdev = [1.5550735, 0.7399330, 0.1850559]
        r_rot = [
            0.6297830, -0.1983517, -0.7510193, -0.5027915, -0.8410733,
            -0.1994906, -0.5920929, 0.5032419, -0.6294232
        ]
        r_scale = [1.346932, 2.400976, 1.933266]
        r_center = [-0.4235626, 0.4724063, 0.6102422]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
        self.assertLess(np.abs(scale.flatten() - r_scale).max(), 0.0001)
        self.assertLess(np.abs(center - r_center).max(), 0.0001)
Beispiel #6
0
    def test02PCAScaleOnly(self):
        '''
        This is the R result.
        
        Standard deviations:
        [1] 1.5649625 0.7209789 0.1762999
        
        Rotation:
                    PC1        PC2        PC3
        [1,]  0.6261964 -0.2060872 -0.7519349
        [2,] -0.5082884 -0.8392166 -0.1932835
        [3,] -0.5912030  0.5032332 -0.6302661
        
        Scale:
        [1] 1.415300 2.449408 2.032120
        '''
        stdev, rot, scale = pv.prcomp(self.data, center=False, scale=True)

        r_stdev = [1.5649625, 0.7209789, 0.1762999]
        r_rot = [
            0.6261964, -0.2060872, -0.7519349, -0.5082884, -0.8392166,
            -0.1932835, -0.5912030, 0.5032332, -0.6302661
        ]
        r_scale = [1.415300, 2.449408, 2.032120]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
        self.assertLess(np.abs(scale.flatten() - r_scale).max(), 0.0001)
Beispiel #7
0
 def test02PCAAllOff(self):
     '''
     This is the R result.
     
     Standard deviations:
     [1] 3.0796955 1.6021872 0.2839987
     
     Rotation:
                 PC1        PC2       PC3
     [1,]  0.4287266  0.2792776 0.8591842
     [2,] -0.7085966  0.6939010 0.1280322
     [3,] -0.5604323 -0.6637058 0.4953890
     '''
     stdev,rot = pv.prcomp(self.data,center=False,scale=False)
     
     r_stdev = [3.0796955, 1.6021872, 0.2839987]
     r_rot = [0.4287266,  0.2792776, 0.8591842,-0.7085966, 0.6939010, 0.1280322, -0.5604323, -0.6637058, 0.4953890]
     
     self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
     self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
Beispiel #8
0
    def test02PCAAllOff(self):
        '''
        This is the R result.
        
        Standard deviations:
        [1] 3.0796955 1.6021872 0.2839987
        
        Rotation:
                    PC1        PC2       PC3
        [1,]  0.4287266  0.2792776 0.8591842
        [2,] -0.7085966  0.6939010 0.1280322
        [3,] -0.5604323 -0.6637058 0.4953890
        '''
        stdev, rot = pv.prcomp(self.data, center=False, scale=False)

        r_stdev = [3.0796955, 1.6021872, 0.2839987]
        r_rot = [
            0.4287266, 0.2792776, 0.8591842, -0.7085966, 0.6939010, 0.1280322,
            -0.5604323, -0.6637058, 0.4953890
        ]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot.flatten() - r_rot).max(), 0.0001)
Beispiel #9
0
    def testPCATraspose(self):
        '''
        Test the prcomp function when pca rows < cols
        Standard deviations:
        [1] 8.331994e+00 4.825577e+00 1.501489e-15
        
        Rotation:
                       PC1         PC2          PC3
         [1,]  0.120344086  0.09556545 -0.936538815
         [2,]  0.252861085  0.10623090  0.315668331
         [3,]  0.024271246 -0.04690261 -0.005344113
         [4,]  0.299257461  0.16307696  0.018798443
         [5,]  0.382358959  0.28156629  0.028963283
         [6,] -0.156277263  0.15979575  0.031295934
         [7,] -0.074892351  0.03402898  0.014440886
         [8,] -0.113677717 -0.40901126 -0.025021991
         [9,]  0.101972797  0.03267545  0.003185637
        [10,]  0.018748487  0.50967727 -0.025423314
        [11,] -0.009698747  0.12256309  0.034447390
        [12,]  0.186645947 -0.06658096  0.014372061
        [13,]  0.299222477  0.09675674  0.048910786
        [14,]  0.268436630 -0.01164026  0.079693297
        [15,] -0.057854438  0.07654372 -0.064354215
        [16,] -0.020516392  0.01883692  0.004268303
        [17,]  0.297578045 -0.59552725 -0.054518114
        [18,] -0.504587911  0.06186216 -0.004543222
        [19,]  0.213855451 -0.14782261 -0.047011001
        [20,] -0.205779238 -0.03449968 -0.007623901
        
        Center:
         [1]  0.70935633  1.04780400 -0.06950500  0.90656600  1.00758200 -0.91922167  0.09474133 -0.05085167  0.00192200 -0.35097200 -0.34026333  0.95575633  0.91019200  0.87737233 -0.43181900 -0.02607167
        [17]  1.75353700 -1.85199600  0.88335000 -0.71357333

        '''
        stdev,rot,center = pv.prcomp(self.data.T,center=True,scale=False)
        
        r_stdev = [8.331994e+00, 4.825577e+00, 1.501489e-15]
        r_rot = [0.120344086,  0.09556545, -0.936538815,
                 0.252861085,  0.10623090,  0.315668331,
                 0.024271246, -0.04690261, -0.005344113,
                 0.299257461,  0.16307696,  0.018798443,
                 0.382358959,  0.28156629,  0.028963283,
                 -0.156277263,  0.15979575,  0.031295934,
                 -0.074892351,  0.03402898,  0.014440886,
                 -0.113677717, -0.40901126, -0.025021991,
                 0.101972797,  0.03267545,  0.003185637,
                  0.018748487,  0.50967727, -0.025423314,
                  -0.009698747,  0.12256309,  0.034447390,
                   0.186645947, -0.06658096,  0.014372061,
                   0.299222477,  0.09675674,  0.048910786,
                   0.268436630, -0.01164026,  0.079693297,
                   -0.057854438, 0.07654372, -0.064354215,
                   -0.020516392,  0.01883692,  0.004268303,
                   0.297578045, -0.59552725, -0.054518114,
                   -0.504587911,  0.06186216, -0.004543222,
                   0.213855451, -0.14782261, -0.047011001,
                   -0.205779238, -0.03449968, -0.007623901]
        r_rot = np.array(r_rot).reshape(20,3)
        r_center = [0.70935633,  1.04780400, -0.06950500,  0.90656600,  
                    1.00758200, -0.91922167,  0.09474133, -0.05085167,
                    0.00192200, -0.35097200, -0.34026333, 0.95575633,
                    0.91019200,  0.87737233, -0.43181900, -0.02607167,
                    1.75353700, -1.85199600,  0.88335000, -0.71357333]
        
        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(np.abs(rot[:,:2].flatten() - r_rot[:,:2].flatten()).max(), 0.0001)
        self.assertLess(np.abs(center.flatten() - r_center).max(), 0.0001)
Beispiel #10
0
    def testPCATraspose(self):
        '''
        Test the prcomp function when pca rows < cols
        Standard deviations:
        [1] 8.331994e+00 4.825577e+00 1.501489e-15
        
        Rotation:
                       PC1         PC2          PC3
         [1,]  0.120344086  0.09556545 -0.936538815
         [2,]  0.252861085  0.10623090  0.315668331
         [3,]  0.024271246 -0.04690261 -0.005344113
         [4,]  0.299257461  0.16307696  0.018798443
         [5,]  0.382358959  0.28156629  0.028963283
         [6,] -0.156277263  0.15979575  0.031295934
         [7,] -0.074892351  0.03402898  0.014440886
         [8,] -0.113677717 -0.40901126 -0.025021991
         [9,]  0.101972797  0.03267545  0.003185637
        [10,]  0.018748487  0.50967727 -0.025423314
        [11,] -0.009698747  0.12256309  0.034447390
        [12,]  0.186645947 -0.06658096  0.014372061
        [13,]  0.299222477  0.09675674  0.048910786
        [14,]  0.268436630 -0.01164026  0.079693297
        [15,] -0.057854438  0.07654372 -0.064354215
        [16,] -0.020516392  0.01883692  0.004268303
        [17,]  0.297578045 -0.59552725 -0.054518114
        [18,] -0.504587911  0.06186216 -0.004543222
        [19,]  0.213855451 -0.14782261 -0.047011001
        [20,] -0.205779238 -0.03449968 -0.007623901
        
        Center:
         [1]  0.70935633  1.04780400 -0.06950500  0.90656600  1.00758200 -0.91922167  0.09474133 -0.05085167  0.00192200 -0.35097200 -0.34026333  0.95575633  0.91019200  0.87737233 -0.43181900 -0.02607167
        [17]  1.75353700 -1.85199600  0.88335000 -0.71357333

        '''
        stdev, rot, center = pv.prcomp(self.data.T, center=True, scale=False)

        r_stdev = [8.331994e+00, 4.825577e+00, 1.501489e-15]
        r_rot = [
            0.120344086, 0.09556545, -0.936538815, 0.252861085, 0.10623090,
            0.315668331, 0.024271246, -0.04690261, -0.005344113, 0.299257461,
            0.16307696, 0.018798443, 0.382358959, 0.28156629, 0.028963283,
            -0.156277263, 0.15979575, 0.031295934, -0.074892351, 0.03402898,
            0.014440886, -0.113677717, -0.40901126, -0.025021991, 0.101972797,
            0.03267545, 0.003185637, 0.018748487, 0.50967727, -0.025423314,
            -0.009698747, 0.12256309, 0.034447390, 0.186645947, -0.06658096,
            0.014372061, 0.299222477, 0.09675674, 0.048910786, 0.268436630,
            -0.01164026, 0.079693297, -0.057854438, 0.07654372, -0.064354215,
            -0.020516392, 0.01883692, 0.004268303, 0.297578045, -0.59552725,
            -0.054518114, -0.504587911, 0.06186216, -0.004543222, 0.213855451,
            -0.14782261, -0.047011001, -0.205779238, -0.03449968, -0.007623901
        ]
        r_rot = np.array(r_rot).reshape(20, 3)
        r_center = [
            0.70935633, 1.04780400, -0.06950500, 0.90656600, 1.00758200,
            -0.91922167, 0.09474133, -0.05085167, 0.00192200, -0.35097200,
            -0.34026333, 0.95575633, 0.91019200, 0.87737233, -0.43181900,
            -0.02607167, 1.75353700, -1.85199600, 0.88335000, -0.71357333
        ]

        self.assertLess(np.abs(stdev - r_stdev).max(), 0.0001)
        self.assertLess(
            np.abs(rot[:, :2].flatten() - r_rot[:, :2].flatten()).max(),
            0.0001)
        self.assertLess(np.abs(center.flatten() - r_center).max(), 0.0001)