code: def test_whitening(setup):
      # 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
  
      assert X.shape == (n_samples, n_features)
  
      # the component-wise variance is thus highly varying:
      assert X.std(axis=0).std().to_numpy() > 43.8
  
      for solver, copy in product(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())
          assert 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_)
          assert 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
          )
  
imports: from itertools import product
  import numpy
  import pytest
  import scipy
  from sklearn import datasets
  from sklearn.utils._testing import assert_array_almost_equal, assert_almost_equal, assert_raises_regex, assert_raise_message, assert_raises
  from ....None import tensor
  from .._pca import PCA, _assess_dimension, _infer_dimension
label_desc: Use image node_id 3 for calling a global function with example usage: test_whitening(setup) without return types