From 79a860b0a7a6d420554195545b28f969ba20e182 Mon Sep 17 00:00:00 2001
From: Olivier Grisel <olivier.grisel@ensta.org>
Date: Thu, 21 Oct 2010 21:04:44 +0200
Subject: [PATCH] FIX: make grid_search output deterministic even in case of
 tie on the scores

---
 scikits/learn/grid_search.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/scikits/learn/grid_search.py b/scikits/learn/grid_search.py
index 1d8ca1378f..22e27c0d97 100644
--- a/scikits/learn/grid_search.py
+++ b/scikits/learn/grid_search.py
@@ -210,7 +210,20 @@ class GridSearchCV(BaseEstimator):
                     for clf_params in grid)
 
         # Out is a list of pairs: score, estimator
-        self.best_score, best_estimator = max(out) # get maximum score
+
+        # Note: we do not use max(out) to make ties deterministic even if
+        # comparison on estimator instances is not deterministic
+        best_score = None
+        for score, estimator in out:
+            if best_score is None:
+                best_score = score
+                best_estimator = estimator
+            else:
+                if score >= best_score:
+                    best_score = score
+                    best_estimator = estimator
+
+        self.best_score = best_score
 
         if refit:
             # fit the best estimator using the entire dataset
-- 
GitLab