【问题标题】:How to divide the data set into train, test and validation purpose如何将数据集划分为训练、测试和验证目的
【发布时间】:2019-10-15 23:47:27
【问题描述】:

我的数据框包含 442 亿行。我想将它分成 3 组(训练、测试和验证)。这样就没有点重叠了。

我已经完成了(第一个过程)-

train, valid, test = np.split(df.sample(frac=1), [int(.8*len(df)), int(.95*len(df))])

检查是否存在任何值 -

len(valid[valid.id.isin(test.id)])
len(train[train.id.isin(test.id)])

第二个过程-

train = df[(np.random.rand(len(df)) < 0.8)]
valid = df[(np.random.rand(len(df)) > 0.8) & (np.random.rand(len(df)) < 0.95)]
test = df[(np.random.rand(len(df)) > 0.95) & (np.random.rand(len(df)) < 1)]

但据我了解,以上两种方法并不完美。谁能帮帮我

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:
    from sklearn.model_selection import train_test_split
    
    X_train, X_test, y_train, y_test  = train_test_split(X, y, test_size=0.2, random_state=1)
    
    X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.25, random_state=1)
    

    第一行将数据集拆分为 80:20 的比例。您可以使用相同的函数将 20% 的数据拆分为 15:5。

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 2021-04-19
      • 2016-07-28
      • 2016-07-04
      • 2022-11-10
      相关资源
      最近更新 更多