code: def test_r2_score(setup, n_samples=50):
      y_true = mt.arange(n_samples)
      y_pred = y_true + 1
  
      assert_almost_equal(r2_score(y_true, y_pred).fetch(), 0.995, 2)
  
      y_true = mt.array([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]])
      y_pred = mt.array([[0, 0, 0, 1], [1, 0, 1, 1], [0, 0, 0, 1]])
  
      error = r2_score(y_true, y_pred, multioutput="variance_weighted")
      assert_almost_equal(error.fetch(), 1.0 - 5.0 / 2)
      error = r2_score(y_true, y_pred, multioutput="uniform_average")
      assert_almost_equal(error.fetch(), -0.875)
  
      assert_almost_equal(r2_score([0.0, 1], [0.0, 1]).fetch(), 1.00, 2)
      assert_almost_equal(
          r2_score(
              [0.0, 1], [0.0, 1], sample_weight=[0.5, 0.5]
          ).fetch(),
          1.00,
          2,
      )
  
      y_true = [[1, 2], [2.5, -1], [4.5, 3], [5, 7]]
      y_pred = [[1, 1], [2, -1], [5, 4], [5, 6.5]]
  
      r = r2_score(y_true, y_pred, multioutput="raw_values")
  
      assert_array_almost_equal(r, [0.95, 0.93], decimal=2)
  
      # mean_absolute_error and mean_squared_error are equal because
      # it is a binary problem.
      y_true = [[0, 0]] * 4
      y_pred = [[1, 1]] * 4
      r = r2_score(y_true, y_pred, multioutput="raw_values")
      assert_array_almost_equal(r, [0.0, 0.0], decimal=2)
  
      r = r2_score(
          [[0, -1], [0, 1]], [[2, 2], [1, 1]], multioutput="raw_values"
      )
      assert_array_almost_equal(r, [0, -3.5], decimal=2)
      assert (
          np.mean(r.fetch())
          == r2_score(
              [[0, -1], [0, 1]],
              [[2, 2], [1, 1]],
              multioutput="uniform_average",
          ).fetch()
      )
  
      # Checking for the condition in which both numerator and denominator is
      # zero.
      y_true = [[1, 3], [-1, 2]]
      y_pred = [[1, 4], [-1, 1]]
      r2 = r2_score(y_true, y_pred, multioutput="raw_values")
      assert_array_almost_equal(r2, [1.0, -3.0], decimal=2)
      assert (
          np.mean(r2.fetch())
          == r2_score(
              y_true, y_pred, multioutput="uniform_average"
          ).fetch()
      )
  
      y_true = [[1, 2], [2.5, -1], [4.5, 3], [5, 7]]
      y_pred = [[1, 1], [2, -1], [5, 4], [5, 6.5]]
  
      rw = r2_score(y_true, y_pred, multioutput=[0.4, 0.6])
  
      assert_almost_equal(rw.fetch(), 0.94, decimal=2)
  
      y_true = [0]
      y_pred = [1]
      warning_msg = "not well-defined with less than two samples."
  
      # Trigger the warning
      with pytest.warns(UndefinedMetricWarning, match=warning_msg):
          score = r2_score(y_true, y_pred)
          assert np.isnan(score)
  
file_path: mars/mars/learn/metrics/tests/test_regression.py
imports: from itertools import product
  import numpy
  import pytest
  from sklearn.exceptions import UndefinedMetricWarning
  from sklearn.utils._testing import assert_almost_equal, assert_array_equal, assert_array_almost_equal
  from ....None import tensor
  from ..None import r2_score
  from .._regresssion import _check_reg_targets
label_desc: Use image node_id 3 for calling a global function with example usage: test_r2_score(setup, n_samples) without return types