I was trying to split the sample dataset using Scikit-learn\'s Stratified Shuffle Split. I followed the example shown on the Scikit-learn documentation here
im
You're running into the different conventions for Pandas DataFrame
indexing versus NumPy ndarray
indexing. The arrays train_index
and test_index
are collections of row indices. But data
is a Pandas DataFrame
object, and when you use a single index into that object, as in data[train_index]
, Pandas is expecting train_index
to contain column labels rather than row indices. You can either convert the dataframe to a NumPy array, using .values
:
data_array = data.values
for train_index, test_index in sss:
xtrain, xtest = data_array[train_index], data_array[test_index]
ytrain, ytest = target[train_index], target[test_index]
or use the Pandas .iloc accessor:
for train_index, test_index in sss:
xtrain, xtest = data.iloc[train_index], data.iloc[test_index]
ytrain, ytest = target[train_index], target[test_index]
I tend to favour the second approach, since it gives xtrain
and xtest
of type DataFrame
rather than ndarray
, and so keeps the column labels.