Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90c57fc

Browse files
Stijn Tonkraghavrv
authored andcommitted
FIX Split data using _safe_split in _permutaion_test_score (scikit-learn#5697)
Squashed commits: [94fd9f4] split data using _safe_split in _permutaion_test_scorer [522053b] adding test case test_permutation_test_score_pandas() to check if permutation_test_score plays nice with pandas dataframe/series [21b23ce] running test_permutation_test_score_pandas on iris data to prevent warnings. [15a48bf] adding safe_indexing to _shuffle function [9ea5c9e] adding test case test_permutation_test_score_pandas() to check if permutation_test_score plays nice with pandas dataframe/series [3cf5e8f] split data using _safe_split in _permutaion_test_scorer to fix error when using Pandas DataFrame/Series
1 parent c76e8dd commit 90c57fc

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

sklearn/cross_validation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,10 @@ def _permutation_test_score(estimator, X, y, cv, scorer):
17561756
"""Auxiliary function for permutation_test_score"""
17571757
avg_score = []
17581758
for train, test in cv:
1759-
estimator.fit(X[train], y[train])
1760-
avg_score.append(scorer(estimator, X[test], y[test]))
1759+
X_train, y_train = _safe_split(estimator, X, y, train)
1760+
X_test, y_test = _safe_split(estimator, X, y, test, train)
1761+
estimator.fit(X_train, y_train)
1762+
avg_score.append(scorer(estimator, X_test, y_test))
17611763
return np.mean(avg_score)
17621764

17631765

@@ -1770,7 +1772,7 @@ def _shuffle(y, labels, random_state):
17701772
for label in np.unique(labels):
17711773
this_mask = (labels == label)
17721774
ind[this_mask] = random_state.permutation(ind[this_mask])
1773-
return y[ind]
1775+
return safe_indexing(y, ind)
17741776

17751777

17761778
def check_cv(cv, X=None, y=None, classifier=False):

sklearn/model_selection/_validation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,10 @@ def _permutation_test_score(estimator, X, y, groups, cv, scorer):
688688
"""Auxiliary function for permutation_test_score"""
689689
avg_score = []
690690
for train, test in cv.split(X, y, groups):
691-
estimator.fit(X[train], y[train])
692-
avg_score.append(scorer(estimator, X[test], y[test]))
691+
X_train, y_train = _safe_split(estimator, X, y, train)
692+
X_test, y_test = _safe_split(estimator, X, y, test, train)
693+
estimator.fit(X_train, y_train)
694+
avg_score.append(scorer(estimator, X_test, y_test))
693695
return np.mean(avg_score)
694696

695697

@@ -702,7 +704,7 @@ def _shuffle(y, groups, random_state):
702704
for group in np.unique(groups):
703705
this_mask = (groups == group)
704706
indices[this_mask] = random_state.permutation(indices[this_mask])
705-
return y[indices]
707+
return safe_indexing(y, indices)
706708

707709

708710
def learning_curve(estimator, X, y, groups=None,

sklearn/model_selection/tests/test_validation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,22 @@ def test_score_memmap():
10791079
break
10801080
except WindowsError:
10811081
sleep(1.)
1082+
1083+
1084+
def test_permutation_test_score_pandas():
1085+
# check permutation_test_score doesn't destroy pandas dataframe
1086+
types = [(MockDataFrame, MockDataFrame)]
1087+
try:
1088+
from pandas import Series, DataFrame
1089+
types.append((Series, DataFrame))
1090+
except ImportError:
1091+
pass
1092+
for TargetType, InputFeatureType in types:
1093+
# X dataframe, y series
1094+
iris = load_iris()
1095+
X, y = iris.data, iris.target
1096+
X_df, y_ser = InputFeatureType(X), TargetType(y)
1097+
check_df = lambda x: isinstance(x, InputFeatureType)
1098+
check_series = lambda x: isinstance(x, TargetType)
1099+
clf = CheckingClassifier(check_X=check_df, check_y=check_series)
1100+
permutation_test_score(clf, X_df, y_ser)

0 commit comments

Comments
 (0)