def test_linspace(): a = linspace(2.0, 3.0, num=5, chunk_size=2) assert a.shape == (5, ) a = tile(a) assert a.nsplits == ((2, 2, 1), ) assert a.chunks[0].op.start == 2. assert a.chunks[0].op.stop == 2.25 assert a.chunks[1].op.start == 2.5 assert a.chunks[1].op.stop == 2.75 assert a.chunks[2].op.start == 3. assert a.chunks[2].op.stop == 3. a = linspace(2.0, 3.0, num=5, endpoint=False, chunk_size=2) assert a.shape == (5, ) a = tile(a) assert a.nsplits == ((2, 2, 1), ) assert a.chunks[0].op.start == 2. assert a.chunks[0].op.stop == 2.2 assert a.chunks[1].op.start == 2.4 assert a.chunks[1].op.stop == 2.6 assert a.chunks[2].op.start == 2.8 assert a.chunks[2].op.stop == 2.8 _, step = linspace(2.0, 3.0, num=5, chunk_size=2, retstep=True) assert step == .25
def testLinspace(self): a = linspace(2.0, 3.0, num=5, chunk_size=2) self.assertEqual(a.shape, (5, )) a = a.tiles() self.assertEqual(a.nsplits, ((2, 2, 1), )) self.assertEqual(a.chunks[0].op.start, 2.) self.assertEqual(a.chunks[0].op.stop, 2.25) self.assertEqual(a.chunks[1].op.start, 2.5) self.assertEqual(a.chunks[1].op.stop, 2.75) self.assertEqual(a.chunks[2].op.start, 3.) self.assertEqual(a.chunks[2].op.stop, 3.) a = linspace(2.0, 3.0, num=5, endpoint=False, chunk_size=2) self.assertEqual(a.shape, (5, )) a = a.tiles() self.assertEqual(a.nsplits, ((2, 2, 1), )) self.assertEqual(a.chunks[0].op.start, 2.) self.assertEqual(a.chunks[0].op.stop, 2.2) self.assertEqual(a.chunks[1].op.start, 2.4) self.assertEqual(a.chunks[1].op.stop, 2.6) self.assertEqual(a.chunks[2].op.start, 2.8) self.assertEqual(a.chunks[2].op.stop, 2.8) _, step = linspace(2.0, 3.0, num=5, chunk_size=2, retstep=True) self.assertEqual(step, .25)
def testWhitening(self): # Check that PCA output has unit-variance rng = np.random.RandomState(0) n_samples = 100 n_features = 80 n_components = 30 rank = 50 # some low rank data with correlated features X = mt.dot( rng.randn(n_samples, rank), mt.dot(mt.diag(mt.linspace(10.0, 1.0, rank)), rng.randn(rank, n_features))) # the component-wise variance of the first 50 features is 3 times the # mean component-wise variance of the remaining 30 features X[:, :50] *= 3 self.assertEqual(X.shape, (n_samples, n_features)) # the component-wise variance is thus highly varying: self.assertGreater(X.std(axis=0).std().to_numpy(), 43.8) for solver, copy in product(self.solver_list, (True, False)): # whiten the data while projecting to the lower dim subspace X_ = X.copy() # make sure we keep an original across iterations. pca = PCA(n_components=n_components, whiten=True, copy=copy, svd_solver=solver, random_state=0, iterated_power=7) # test fit_transform X_whitened = pca.fit_transform(X_.copy()) self.assertEqual(X_whitened.shape, (n_samples, n_components)) X_whitened2 = pca.transform(X_) assert_array_almost_equal(X_whitened.fetch(), X_whitened2.fetch()) assert_almost_equal(X_whitened.std(ddof=1, axis=0).to_numpy(), np.ones(n_components), decimal=6) assert_almost_equal( X_whitened.mean(axis=0).to_numpy(), np.zeros(n_components)) X_ = X.copy() pca = PCA(n_components=n_components, whiten=False, copy=copy, svd_solver=solver).fit(X_) X_unwhitened = pca.transform(X_) self.assertEqual(X_unwhitened.shape, (n_samples, n_components)) # in that case the output components still have varying variances assert_almost_equal( X_unwhitened.std(axis=0).std().to_numpy(), 74.1, 1)