gpt4 book ai didi

python - 值错误: n_splits=10 cannot be greater than the number of members in each class

转载 作者:行者123 更新时间:2023-12-02 03:09:17 28 4
gpt4 key购买 nike

我正在尝试运行以下代码:

from sklearn.model_selection import StratifiedKFold 
X = ["hey", "join now", "hello", "join today", "join us now", "not today", "join this trial", " hey hey", " no", "hola", "bye", "join today", "no","join join"]
y = ["n", "r", "n", "r", "r", "n", "n", "n", "n", "r", "n", "n", "n", "r"]

skf = StratifiedKFold(n_splits=10)

for train, test in skf.split(X,y):
print("%s %s" % (train,test))

但我收到以下错误:

ValueError: n_splits=10 cannot be greater than the number of members in each class.

我看过这里scikit-learn error: The least populated class in y has only 1 member但我仍然不确定我的代码有什么问题。

我的列表的长度均为 14 print(len(X)) print(len(y))

我的部分困惑是我不确定在此上下文中 members 的定义是什么以及 class 是什么。

问题:如何修复该错误?什么是成员(member)?什么是类(class)? (在这种情况下)

最佳答案

分层是指保持每个折叠中每个类的比例。因此,如果您的原始数据集有 3 个类,比例分别为 60%、20% 和 20%,那么分层将尝试在每次折叠中保持该比例。

就您而言,

X = ["hey", "join now", "hello", "join today", "join us now", "not today",
"join this trial", " hey hey", " no", "hola", "bye", "join today",
"no","join join"]
y = ["n", "r", "n", "r", "r", "n", "n", "n", "n", "y", "n", "n", "n", "y"]

您总共有 14 个样本(成员),其分布为:

class    number of members         percentage
'n' 9 64
'r' 3 22
'y' 2 14

因此 StratifiedKFold 将尝试在每次折叠中保持该比例。现在您已指定 10 次折叠 (n_splits)。因此,这意味着在单倍中,对于“y”类来说,要维持该比例,至少有 2/10 = 0.2 名成员。但我们不能提供少于 1 个成员(样本),因此这就是它抛出错误的原因。

如果您设置的是 n_splits=2,而不是 n_splits=10,那么它就会起作用,因为“y”的成员数量将为 2/2 = 1。为了使 n_splits = 10 正常工作,每个类至少需要 10 个样本。

关于python - 值错误: n_splits=10 cannot be greater than the number of members in each class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48313387/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com