【问题标题】:Holdout method in pythonpython中的保持方法
【发布时间】:2020-02-06 15:18:58
【问题描述】:

如何在 python 中进行 6:4 坚持?我尝试了以下代码:

X_train, X_test, y_train, y_test =  train_test_split(X,y, training_size=0.6, test_size=0.4)

但不确定是否正确。

【问题讨论】:

  • 好吧,你为什么不检查一下结果的大小/暗淡。不过看起来不错。
  • 如果我有 100 个数据点,那么是否意味着我需要用 60 个数据点进行训练,并用其他 40 个数据点进行测试?

标签: python python-3.x scikit-learn


【解决方案1】:

请参阅来自 scikit-learn 的 train_test_split 的文档。

要设置训练集和测试集的大小,您需要通过 train_size(而不是代码中的 training_size)和 test_size

要将 60% 的数据用于训练,40% 用于测试,您可以这样:

import numpy as np
from sklearn.model_selection import train_test_split

X = np.random.rand(100, 2)
y = range(100)

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.6, test_size=0.4)

您可以确认,对于本示例中使用的 100 个数据点,您得到的训练集大小为 60,测试集大小为 40:

print(len(X_train), len(X_test))
print(len(y_train), len(y_test))
> 60 40
> 60 40

【讨论】:

    猜你喜欢
    • 2014-10-27
    • 2023-02-17
    • 2020-07-10
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2012-07-19
    • 2011-04-23
    • 2018-08-31
    相关资源
    最近更新 更多