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

标签 python scikit-learn cross-validation

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

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/

相关文章:

python - QWebEngineUrlRequestInterceptor 不工作

python - sklearn逻辑回归值错误: X has 42 features per sample; expecting 1423

python - 如何根据sklearn中的预测概率对实例进行排名

r - 交叉验证季节性线性模型

tensorflow - 2 类分类器能否输出一类的可能性作为分数

python - 如何在 python 的 sklearn 中通过交叉验证执行 SMOTE

python - Keras 中的 Theano 图形打印

python - 如何在执行程序任务仍在等待控制台输入的情况下干净地退出 python 异步应用程序

python - 为什么 CPython Dicts 不受负一和负二哈希值的影响

python - 每个维度具有不同内核的 3D 高斯过程