user8270077
user8270077

Reputation: 5081

In GroupKFold in ScikitLearn error message: ValueError: too many values to unpack (expected 2)

In using the GroupKFold method from scikit-learn I am getting an error message which I can not understand given the documentation.

The error message is:

ValueError: too many values to unpack (expected 2)

The documentation states:

enter image description here

For a reproducible example:

from sklearn.model_selection import GroupKFold

X1 = np.random.randint(1, 10, size = (100, 2))

groups1 = np.random.choice([1,2,3, 4, 5], size = 100, p = [.15, .2, .3, .15, .2])


gkf1 = GroupKFold(5)

train, test = gkf1.split(X = X1, groups = groups1 )

Which yields the following error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-911681dea183> in <module>
      8 gkf1 = GroupKFold(5)
      9 
---> 10 train, test = gkf1.split(X = X1, groups = groups1 )

ValueError: too many values to unpack (expected 2)

Upvotes: 1

Views: 293

Answers (1)

abhilb
abhilb

Reputation: 5757

The split function returns a generator. You will have to iterate over the generator to get the train and test groups.

Like shown in the example

for train_index, test_index in gkf1.split(X, y, groups):

Upvotes: 1

Related Questions