【问题标题】:What does this error mean with StratifiedShuffleSplit?这个错误对 StratifiedShuffleSplit 意味着什么?
【发布时间】:2018-09-04 02:35:03
【问题描述】:

总的来说,我对数据科学完全陌生,希望有人能解释为什么这不起作用:

我正在使用来自以下 url 的广告数据集:“http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv”,它有 3 个特征列(“TV”、“Radio”、“Newspaper”)和 1 个标签列(“sales”)。我的完整数据集命名为data

接下来,我尝试使用sklearn的StratifiedShuffleSplit函数将数据分为训练集和测试集。

from sklearn.model_selection import StratifiedShuffleSplit

split = StratifiedShuffleSplit(n_splits=1, random_state=0) # can use test_size=0.8
for train_index, test_index in split.split(data.drop("sales", axis=1), data["sales"]): # Generate indices to split data into training and test set.
    strat_train_set = data.loc[train_index]
    strat_test_set = data.loc[test_index]

我知道了ValueError: The least populated class in y has only 1 member, which is too few. The minimum number of groups for any class cannot be less than 2.

在具有 14 个特征列和 1 个标签列的另一个数据集上使用相同的代码可以适当地分隔数据。为什么它在这里不起作用?谢谢。

【问题讨论】:

标签: python pandas scikit-learn data-science sklearn-pandas


【解决方案1】:

我认为问题在于您的 data_y 是二维矩阵。

但正如我在sklearn.model_selection.StratifiedShuffleSplit doc 中看到的,它应该是1D 向量。尝试将data_y的每一行编码为整数(会被解释为一个类),并在使用split之后。

或者您的 y 可能是一个回归变量(连续数值数据)。(Vivek 的链接)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2021-06-10
    相关资源
    最近更新 更多