【问题标题】:How to partition a dataset into three equal parts?如何将数据集分成三个相等的部分?
【发布时间】:2021-07-09 06:49:41
【问题描述】:

我正在尝试使用 scikit-learn 将我的数据集分成三个相等的部分。但是当我使用 StratifiedKFold(在 sklearn 上)来执行此操作时,它只显示了我为分区数据集所做的命令,而不是结果:

from sklearn.model_selection import StratifiedKFold
partition = StratifiedKFold(n_splits = 3, shuffle = True, random_state = None)
print(partition)

我还是 Python 库的新手,所以我不知道该怎么做。

【问题讨论】:

  • 请不要将代码sn-ps粘贴为截图,写下来有问题。您只初始化了对象,但没有调用方法来获取拆分。查看示例部分here

标签: python scikit-learn


【解决方案1】:

您的代码的第二行创建了一个 StratifiedKFold 对象,它并没有真正对您的数据进行分区。您应该使用此对象来拆分数据(请参见下面的示例)

partition = StratifiedKFold(n_splits = 3, shuffle = True, random_state = 1)

for train_index, test_index in partition.split(x, y):
    x_train_f, x_test_f = x[train_index], x[test_index]
    y_train_f, y_test_f = y[train_index], y[test_index]

您关于将数据分成 3 部分的答案已得到答复here

X_train, X_test, X_validate  = np.split(X, [int(.7*len(X)), int(.8*len(X))])
y_train, y_test, y_validate  = np.split(y, [int(.7*len(y)), int(.8*len(y))])

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 2019-05-06
    • 2012-09-17
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多