Skip to content

Commit

Permalink
Fix 'rank_test_fcp' order in grid search cv_results. (#403)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
  • Loading branch information
lapidshay and NicolasHug committed Aug 13, 2022
1 parent fd3fcda commit 22eb126
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 5 additions & 4 deletions surprise/model_selection/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,16 @@ def fit(self, data):
train_measures[m].std(axis=1)

# cv_results: set rank of each param comb
# also set best_index, and best_xxxx attributes
indices = cv_results['mean_test_{}'.format(m)].argsort()
cv_results['rank_test_{}'.format(m)] = np.empty_like(indices)
cv_results['rank_test_{}'.format(m)][indices] = np.arange(
len(indices)) + 1 # sklearn starts rankings at 1 as well.

# set best_index, and best_xxxx attributes
if m in ('mae', 'rmse', 'mse'):
cv_results['rank_test_{}'.format(m)][indices] = \
np.arange(len(indices)) + 1 # sklearn starts at 1 as well
best_index[m] = mean_test_measures.argmin()
elif m in ('fcp',):
cv_results['rank_test_{}'.format(m)][indices] = \
np.arange(len(indices), 0, -1)
best_index[m] = mean_test_measures.argmax()
best_params[m] = self.param_combinations[best_index[m]]
best_score[m] = mean_test_measures[best_index[m]]
Expand Down
8 changes: 5 additions & 3 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def test_gridsearchcv_cv_results():
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': [.2, .2],
'reg_all': [.4, .4], 'n_factors': [5], 'random_state': [0]}
gs = GridSearchCV(SVD, param_grid, measures=['RMSE', 'mae'], cv=kf,
param_grid = {'n_epochs': [5], 'lr_all': [.2, .4],
'reg_all': [.4, .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)

Expand Down Expand Up @@ -151,6 +151,8 @@ def test_gridsearchcv_cv_results():
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']


def test_gridsearchcv_refit(u1_ml100k):
Expand Down

0 comments on commit 22eb126

Please sign in to comment.