Pandas + Scikit learn: problem with stratified k-fold

When used with a StratifiedKFold

dataframe from scikit-learn, it returns a list of indices from 0 to n instead of a list of values ​​from the DF index. Is there a way to change this?

Example:

df = pd.DataFrame()
df["test"] = (0, 1, 2, 3, 4, 5, 6)
df.index   = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
for i, (train, test) in enumerate(StratifiedKFold(df.index)):
    print i, (train, test)

      

gives:

0 (array([], dtype=64), array([0,1,2,3,4,5,6])
1 (array([0,1,2,3,4,5,6]), array([], dtype=64))
2 (array([0,1,2,3,4,5,6]), array([], dtype=64))

      

I expect the index from df to be returned, not a range of length df ...

+3


source to share


1 answer


You only got the numbers df.index

that you selected StratifiedKFold

.

To change it back to the index of your DataFrame, simply

for i, (train, test) in enumerate(StratifiedKFold(df.index)):
    print i, (df.index[train], df.index[test])

      



which gives

0 (Index([], dtype='object'), Index([u'a', u'b', u'c', u'd', u'e', u'f', u'g'], dtype='object'))
1 (Index([u'a', u'b', u'c', u'd', u'e', u'f', u'g'], dtype='object'), Index([], dtype='object'))
2 (Index([u'a', u'b', u'c', u'd', u'e', u'f', u'g'], dtype='object'), Index([], dtype='object'))

      

+3


source







All Articles