在R中,使用带有默认值的intial_split()函数(对于测试是0.75,对于训练是0.25 ),我得到了分配给测试拆分的预期行数。我在下面使用包含在R中的randu数据集演示了这一点,该数据集有400行,因此我们预计在训练拆分中为300 (400 * 0.75),在测试单中为100 (400 * 0.25)。以下是代码和输出:
> nrow(randu)
[1] 400
> randu_split <- initial_split(randu)
> randu_train <- training(randu_split)
> randu_test <- testing(randu_split)
> nrow(randu_train)
[1] 300
> nrow(randu_test)
[1] 100但是,当使用proportion参数(prop = 4/5)将训练和测试的属性的比例更改为0.80和0.20时,分配给不同拆分的行数与预期的行数不同。在一个有400行的数据集中,我期望在训练拆分中有320 (400 * 0.80)行,在测试拆分中有80 (400 * 0.20)行。
> 400*0.8
[1] 320
> 400*0.2
[1] 80然而,这不是我所观察到的。请查看下面的结果:
> randu_split_80_20 <- initial_split(randu, prop = 4/5)
> randu_train_eighty <- training(randu_split_80_20)
> randu_test_twenty <- testing(randu_split_80_20)
> nrow(randu_train_eighty)
[1] 321
> nrow(randu_test_twenty)
[1] 79
> nrow(randu_split_80_20)
analysis
321 为什么prop = 4/5的intial_split()函数将321行而不是320行分配给训练拆分?我用不同的数据集尝试了这一点,赋值总是以1为负。我还尝试使用prop = 9/10,与预期数量相比,拆分中返回的样本数量也减少了1。有什么想法吗?
发布于 2020-07-06 23:19:23
这可能是由于舍入误差。rsample::initial_split的源代码可以追溯到mc_cv和mc_splits。测试次数由floor(n * (1 - prop))计算。要查看R中的数字,请执行以下操作:
> n = 400 #number of rows
> prop = 3/4
> sprintf("%.20f", n * (1-prop))
[1] "100.00000000000000000000"
> floor(n * (1-prop)) #number of testing set
[1] 100
> prop = 4/5
> sprintf("%.20f", n * (1-prop))
[1] "79.99999999999998578915"
> floor(n * (1-prop))
[1] 79
> prop = 9/10
> sprintf("%.20f", n * (1-prop))
[1] "39.99999999999999289457"
> floor(n * (1-prop))
[1] 39https://stackoverflow.com/questions/61878688
复制相似问题