code: def test_explained_variance(setup):
      # Check that PCA output has unit-variance
      rng = np.random.RandomState(0)
      n_samples = 100
      n_features = 80
  
      X = mt.tensor(rng.randn(n_samples, n_features))
  
      pca = PCA(n_components=2, svd_solver="full").fit(X)
      rpca = PCA(
          n_components=2, svd_solver="randomized", random_state=42
      ).fit(X)
      assert_array_almost_equal(
          pca.explained_variance_.to_numpy(),
          rpca.explained_variance_.to_numpy(),
          1,
      )
      assert_array_almost_equal(
          pca.explained_variance_ratio_.to_numpy(),
          rpca.explained_variance_ratio_.to_numpy(),
          1,
      )
  
      # compare to empirical variances
      expected_result = np.linalg.eig(
          np.cov(X.to_numpy(), rowvar=False)
      )[0]
      expected_result = sorted(expected_result, reverse=True)[:2]
  
      X_pca = pca.transform(X)
      assert_array_almost_equal(
          pca.explained_variance_.to_numpy(),
          mt.var(X_pca, ddof=1, axis=0).to_numpy(),
      )
      assert_array_almost_equal(
          pca.explained_variance_.to_numpy(), expected_result
      )
  
      X_rpca = rpca.transform(X)
      assert_array_almost_equal(
          rpca.explained_variance_.to_numpy(),
          mt.var(X_rpca, ddof=1, axis=0).to_numpy(),
          decimal=1,
      )
      assert_array_almost_equal(
          rpca.explained_variance_.to_numpy(),
          expected_result,
          decimal=1,
      )
  
      # Same with correlated data
      X = datasets.make_classification(
          n_samples,
          n_features,
          n_informative=n_features - 2,
          random_state=rng,
      )[0]
      X = mt.tensor(X)
  
      pca = PCA(n_components=2).fit(X)
      rpca = PCA(
          n_components=2, svd_solver="randomized", random_state=rng
      ).fit(X)
      assert_array_almost_equal(
          pca.explained_variance_ratio_.to_numpy(),
          rpca.explained_variance_ratio_.to_numpy(),
          5,
      )
  
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 4 for calling a global function with example usage: test_explained_variance(setup) without return types