这是一个火车/测试拆分过程,它基于您原始数据集中的(唯一)机器名称。
# example dataset
df = data.frame(Date = c(rep("03/20/2001", 4), rep("03/21/2001", 4)),
Machine = rep(c("XTR003","XTR004","XTR005","XTR006"), 2),
Attr1 = c(0,0,10,5,0,4,0,8),
Failure = c(0,1,0,0,1,0,0,1),
stringsAsFactors = F)
# check how it looks like
df
# Date Machine Attr1 Failure
# 1 03/20/2001 XTR003 0 0
# 2 03/20/2001 XTR004 0 1
# 3 03/20/2001 XTR005 10 0
# 4 03/20/2001 XTR006 5 0
# 5 03/21/2001 XTR003 0 1
# 6 03/21/2001 XTR004 4 0
# 7 03/21/2001 XTR005 0 0
# 8 03/21/2001 XTR006 8 1
# create a vector of unique machine names
machine_vec = unique(df$Machine)
# calculate number of unique machines in your train dataset
# here we want 70% of machines to be in the train dataset
# (this is NOT the number of rows of your train dataset, but the number of unique machines)
N_train = round(length(machine_vec) * 0.7)
# randomly select which machine names will create your train dataset
train_machines = sample(machine_vec, N_train)
# select corresponding rows for your train and test dataset
# (all rows of the machines selected previously will form the train data)
df_train = df[df$Machine %in% train_machines,]
df_test = df[!df$Machine %in% train_machines,]
到底你的训练和测试数据集的行数应该等于你的原始数据集的行数,因为你不会丢失任何信息进行分割。另外,一台机器只能属于火车数据集,或者只属于测试数据集,因为这是拆分背后的哲学。
您必须提供您的数据的代表性样本,或者您认为代表您的结构的假数据,以便我们为您提供帮助。此外,还有很多关于如何将数据集分解为火车/测试的例子。 – AntoniosK
@AntoniosK感谢您的反馈。我收录了一个很小的样本的截图。我发现了很多关于分割数据的资源,但是如果数据集包含'date'列,我没有发现如何做到这一点。我发现的唯一资源根据某个日期将数据集分为两部分(因此训练集在该日期之前,且验证集在该日期之后)。 – dhd
就我个人而言,我会使用机器编号栏来分割数据。这样我的训练和测试数据将包含特定机器名称的所有日期。我不想将特定机器的某些行(天)作为培训,而将其他作为测试数据。例如,如果机器“XTR004”随机获得训练数据,则其所有行也将转到训练数据。这听起来合理吗?你知道该怎么做吗? – AntoniosK