sklearn.cross_validation.StratifiedShuffleSplit - error: “indices are out-of-bounds”

前端 未结 1 344
名媛妹妹
名媛妹妹 2021-01-31 18:25

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         


        
相关标签:
1条回答
  • 2021-01-31 18:58

    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.

    0 讨论(0)
提交回复
热议问题