【问题标题】:How do I split a dataset into training, test, and cross validation sets?如何将数据集拆分为训练集、测试集和交叉验证集?
【发布时间】:2018-06-08 03:31:35
【问题描述】:

首先,我对 1000x20 数组中的数值数据进行了标准化,然后创建了另一个数组,其中包含标准化数据行索引的随机排列。如何将这个新数组拆分为训练、交叉验证和测试集?

    In[150]:
    row_indices = np.random.permutation(X_norm.shape[0])

    In[151]:  
    # Create a Training Set - 60 percent of data - 600x20
    X_train = 

    # Create a Cross Validation Set - 20 percent - 200x20
    X_crossVal = 

    # Create a Test Set - 20 percent - 200x20
    X_test = 

    # If you performed the above calculations correctly, then X_train 
    # should have 600 rows and 20 columns, X_crossVal should have 200 rows 
    # and 20 columns, and X_test should have 200 rows and 20 columns. You 
    # can verify this by filling the code below:

    In[152]:
    # Print the shape of X_train
    X_train.shape

    # Print the shape of X_crossVal


    # Print the shape of X_test

请原谅我在堆栈溢出方面有多糟糕。

【问题讨论】:

  • 这是作业吗?您需要拆分洗牌的索引,然后使用每个拆分从X_norm 中选择行...例如拆分后做X_norm[train_indices]
  • 你可以使用其他库吗?如果是,那么scikit-learn's train_test_split 会非常好用。

标签: numpy partitioning cross-validation indices numpy-ndarray


【解决方案1】:

您可以使用np.split 将您的数据拆分为预定义大小的块:

X_train, X_crossVal, X_test = np.split(row_indices, [600, 800])

API Documentation

【讨论】:

  • 当我打印 X_train.shape 时,我仍然得到 (20,) 而不是 (600,20)
  • row_indices 是否包含 1000x20 数组的每个元素索引?如果是这样,X_trainX_crossValX_test 实际上包含数组的索引(而不是元素本身。
【解决方案2】:
X_train = X_norm[row_indices[0:600]]

创建交叉验证集

X_crossVal = X_norm[row_indices[600:800]]

创建测试集

X_test = X_norm[row_indices[800:1000]]

还要确保在打印时使用它们:

print(X_train.shape)

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 2019-05-01
    • 2016-07-04
    • 2021-09-17
    • 2016-09-13
    • 2013-09-27
    • 2016-05-12
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多