code: def test_gridsearchcv_cv_results():
      """Test the cv_results attribute"""
  
      f = os.path.join(os.path.dirname(__file__), "./u1_ml100k_test")
      data = Dataset.load_from_file(f, Reader("ml-100k"))
      kf = KFold(3, shuffle=True, random_state=4)
      param_grid = {
          "n_epochs": [5],
          "lr_all": [0.2, 0.4],
          "reg_all": [0.4, 0.6],
          "n_factors": [5],
          "random_state": [0],
      }
      gs = GridSearchCV(
          SVD,
          param_grid,
          measures=["RMSE", "mae", "fcp"],
          cv=kf,
          return_train_measures=True,
      )
      gs.fit(data)
  
      # test keys split*_test_rmse, mean and std dev.
      assert gs.cv_results["split0_test_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["split1_test_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["split2_test_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["mean_test_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert np.allclose(
          gs.cv_results["mean_test_rmse"],
          np.mean(
              [
                  gs.cv_results["split0_test_rmse"],
                  gs.cv_results["split1_test_rmse"],
                  gs.cv_results["split2_test_rmse"],
              ],
              axis=0,
          ),
      )
      assert np.allclose(
          gs.cv_results["std_test_rmse"],
          np.std(
              [
                  gs.cv_results["split0_test_rmse"],
                  gs.cv_results["split1_test_rmse"],
                  gs.cv_results["split2_test_rmse"],
              ],
              axis=0,
          ),
      )
  
      # test keys split*_train_mae, mean and std dev.
      assert gs.cv_results["split0_train_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["split1_train_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["split2_train_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["mean_train_rmse"].shape == (
          4,
      )  # 4 param comb.
      assert np.allclose(
          gs.cv_results["mean_train_rmse"],
          np.mean(
              [
                  gs.cv_results["split0_train_rmse"],
                  gs.cv_results["split1_train_rmse"],
                  gs.cv_results["split2_train_rmse"],
              ],
              axis=0,
          ),
      )
      assert np.allclose(
          gs.cv_results["std_train_rmse"],
          np.std(
              [
                  gs.cv_results["split0_train_rmse"],
                  gs.cv_results["split1_train_rmse"],
                  gs.cv_results["split2_train_rmse"],
              ],
              axis=0,
          ),
      )
  
      # test fit and train times dimensions.
      assert gs.cv_results["mean_fit_time"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["std_fit_time"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["mean_test_time"].shape == (
          4,
      )  # 4 param comb.
      assert gs.cv_results["std_test_time"].shape == (
          4,
      )  # 4 param comb.
  
      assert gs.cv_results["params"] is gs.param_combinations
  
      # assert that best parameter in gs.cv_results['rank_test_measure'] is
      # indeed the best_param attribute.
      best_index = np.argmin(gs.cv_results["rank_test_rmse"])
      assert (
          gs.cv_results["params"][best_index] == gs.best_params["rmse"]
      )
      best_index = np.argmin(gs.cv_results["rank_test_mae"])
      assert (
          gs.cv_results["params"][best_index] == gs.best_params["mae"]
      )
      best_index = np.argmin(gs.cv_results["rank_test_fcp"])
      assert (
          gs.cv_results["params"][best_index] == gs.best_params["fcp"]
      )
  
file_path: Surprise/tests/test_search.py
imports: import os
  import numpy
  import pytest
  from scipy.stats import randint, uniform
  from surprise import Dataset, Reader, SVD
  from surprise.model_selection import cross_validate, GridSearchCV, KFold, PredefinedKFold, RandomizedSearchCV
label_desc: Use image node_id 5 for calling a global function with example usage: test_gridsearchcv_cv_results() without return types