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

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

在使用 scikit-learn 中的 GroupKFold 方法时,我收到一条错误消息,根据文档我无法理解。

错误信息是:

ValueError: too many values to unpack (expected 2)

文档指出:

对于可重现的示例:

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 )

这会产生以下错误消息:

---------------------------------------------------------------------------
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)

split函数returns一个生成器。 您将不得不遍历生成器以获得训练组和测试组。

如示例所示

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