code: def test_explained_variance(setup):
      # Test sparse data
      svd_r_10_sp = TruncatedSVD(
          10, algorithm="randomized", random_state=42
      )
      svd_r_20_sp = TruncatedSVD(
          20, algorithm="randomized", random_state=42
      )
      X_trans_r_10_sp = svd_r_10_sp.fit_transform(X)
      X_trans_r_20_sp = svd_r_20_sp.fit_transform(X)
  
      # Test dense data
      svd_r_10_de = TruncatedSVD(
          10, algorithm="randomized", random_state=42
      )
      svd_r_20_de = TruncatedSVD(
          20, algorithm="randomized", random_state=42
      )
      X_trans_r_10_de = svd_r_10_de.fit_transform(X.toarray())
      X_trans_r_20_de = svd_r_20_de.fit_transform(X.toarray())
  
      # helper arrays for tests below
      svds = (svd_r_10_sp, svd_r_20_sp, svd_r_10_de, svd_r_20_de)
      svds_trans = (
          (svd_r_10_sp, X_trans_r_10_sp),
          (svd_r_20_sp, X_trans_r_20_sp),
          (svd_r_10_de, X_trans_r_10_de),
          (svd_r_20_de, X_trans_r_20_de),
      )
      svds_10_v_20 = (
          (svd_r_10_sp, svd_r_20_sp),
          (svd_r_10_de, svd_r_20_de),
      )
      svds_sparse_v_dense = (
          (svd_r_10_sp, svd_r_10_de),
          (svd_r_20_sp, svd_r_20_de),
      )
  
      # Assert the 1st component is equal
      for svd_10, svd_20 in svds_10_v_20:
          assert_array_almost_equal(
              svd_10.explained_variance_ratio_.to_numpy(),
              svd_20.explained_variance_ratio_[:10].to_numpy(),
              decimal=4,
          )
  
      # Assert that 20 components has higher explained variance than 10
      for svd_10, svd_20 in svds_10_v_20:
          assert (
              svd_20.explained_variance_ratio_.sum().to_numpy()
              > svd_10.explained_variance_ratio_.sum().to_numpy()
          )
  
      # Assert that all the values are greater than 0
      for svd in svds:
          assert_array_less(
              0.0, svd.explained_variance_ratio_.to_numpy()
          )
  
      # Assert that total explained variance is less than 1
      for svd in svds:
          assert_array_less(
              svd.explained_variance_ratio_.sum().to_numpy(), 1.0
          )
  
      # Compare sparse vs. dense
      for svd_sparse, svd_dense in svds_sparse_v_dense:
          assert_array_almost_equal(
              svd_sparse.explained_variance_ratio_.to_numpy(),
              svd_dense.explained_variance_ratio_.to_numpy(),
          )
  
      # Test that explained_variance is correct
      for svd, transformed in svds_trans:
          total_variance = mt.var(X.toarray(), axis=0).sum().to_numpy()
          variances = mt.var(transformed, axis=0)
          true_explained_variance_ratio = variances / total_variance
  
          assert_array_almost_equal(
              svd.explained_variance_ratio_.to_numpy(),
              true_explained_variance_ratio.to_numpy(),
          )
  
file_path: mars/mars/learn/decomposition/tests/test_truncated_svd.py
imports: import numpy
  import pytest
  import scipy.sparse
  from sklearn.utils import check_random_state
  from sklearn.utils._testing import assert_array_almost_equal, assert_array_less
  from ....None import tensor
  from ..None import TruncatedSVD
label_desc: Use image node_id 6 for calling a global function with example usage: test_explained_variance(setup) without return types