From c3900a6e5a565cc00b41d123068a8e995d087579 Mon Sep 17 00:00:00 2001 From: Dayvid Victor Date: Wed, 30 Nov 2016 11:37:00 -0300 Subject: [PATCH 1/4] removing sklearn.cross_validation DeprecationWarning issue #195 --- .../under_sampling/instance_hardness_threshold.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/imblearn/under_sampling/instance_hardness_threshold.py b/imblearn/under_sampling/instance_hardness_threshold.py index c435e952f..6bd921e48 100644 --- a/imblearn/under_sampling/instance_hardness_threshold.py +++ b/imblearn/under_sampling/instance_hardness_threshold.py @@ -10,7 +10,13 @@ from sklearn.base import ClassifierMixin from sklearn.ensemble import RandomForestClassifier -from sklearn.cross_validation import StratifiedKFold + +import sklearn + +if hasattr(sklearn, 'model_selection'): + from sklearn.model_selection import StratifiedKFold +else: + from sklearn.cross_validation import StratifiedKFold from six import string_types @@ -225,8 +231,8 @@ def _sample(self, X, y): """ # Create the different folds - skf = StratifiedKFold(y, n_folds=self.cv, shuffle=False, - random_state=self.random_state) + skf = list(StratifiedKFold(y, n_folds=self.cv, shuffle=False, + random_state=self.random_state)) probabilities = np.zeros(y.shape[0], dtype=float) From 69848ef1c575e1e4a5677bf66768e8b0225ecae8 Mon Sep 17 00:00:00 2001 From: Dayvid Victor Date: Wed, 30 Nov 2016 12:07:56 -0300 Subject: [PATCH 2/4] InstanceHardnessThreshold StratifiedKFold bugfix --- imblearn/under_sampling/instance_hardness_threshold.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/imblearn/under_sampling/instance_hardness_threshold.py b/imblearn/under_sampling/instance_hardness_threshold.py index 6bd921e48..e726dbc1a 100644 --- a/imblearn/under_sampling/instance_hardness_threshold.py +++ b/imblearn/under_sampling/instance_hardness_threshold.py @@ -231,7 +231,8 @@ def _sample(self, X, y): """ # Create the different folds - skf = list(StratifiedKFold(y, n_folds=self.cv, shuffle=False, + + skf = list(StratifiedKFold(y, self.cv, shuffle=False, random_state=self.random_state)) probabilities = np.zeros(y.shape[0], dtype=float) From 2b653d08c8d3934f824d10a84d39a73dbdd6ffc5 Mon Sep 17 00:00:00 2001 From: Dayvid Victor Date: Wed, 30 Nov 2016 12:26:05 -0300 Subject: [PATCH 3/4] InstanceHardnessThreshold StratifiedKFold sklearn 0.18.X bugfix --- .../under_sampling/instance_hardness_threshold.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/imblearn/under_sampling/instance_hardness_threshold.py b/imblearn/under_sampling/instance_hardness_threshold.py index e726dbc1a..64788834a 100644 --- a/imblearn/under_sampling/instance_hardness_threshold.py +++ b/imblearn/under_sampling/instance_hardness_threshold.py @@ -231,9 +231,14 @@ def _sample(self, X, y): """ # Create the different folds - - skf = list(StratifiedKFold(y, self.cv, shuffle=False, - random_state=self.random_state)) + + if hasattr(sklearn, 'model_selection'): + skf = list(StratifiedKFold(n_splits=self.cv, shuffle=False, + random_state=self.random_state).split(X, y)) + else: + skf = StratifiedKFold(y, n_folds=self.cv, shuffle=False, + random_state=self.random_state) + probabilities = np.zeros(y.shape[0], dtype=float) From c8260edcfc69ab325258f2c7e03f33251b1389e9 Mon Sep 17 00:00:00 2001 From: Dayvid Victor Date: Sat, 3 Dec 2016 22:23:49 -0300 Subject: [PATCH 4/4] function without raise/except calling --- imblearn/under_sampling/instance_hardness_threshold.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/imblearn/under_sampling/instance_hardness_threshold.py b/imblearn/under_sampling/instance_hardness_threshold.py index fcb6a4e77..0fa4ff8b6 100644 --- a/imblearn/under_sampling/instance_hardness_threshold.py +++ b/imblearn/under_sampling/instance_hardness_threshold.py @@ -7,6 +7,7 @@ import numpy as np from six import string_types +import sklearn from sklearn.base import ClassifierMixin from sklearn.ensemble import RandomForestClassifier @@ -14,14 +15,15 @@ def _get_cv_splits(X, y, cv, random_state): - try: + if hasattr(sklearn, 'model_selection'): from sklearn.model_selection import StratifiedKFold cv_iterator = StratifiedKFold( n_splits=cv, shuffle=False, random_state=random_state).split(X, y) - except: + else: from sklearn.cross_validation import StratifiedKFold cv_iterator = StratifiedKFold( y, n_folds=cv, shuffle=False, random_state=random_state) + return cv_iterator