如何使用样本函数将数据拆分为训练/测试集 [英] How to split data into training/testing sets using sample function
本文介绍了如何使用样本函数将数据拆分为训练/测试集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始使用 R,我不确定如何将我的数据集与以下示例代码合并:
I've just started using R and I'm not sure how to incorporate my dataset with the following sample code:
sample(x, size, replace = FALSE, prob = NULL)
我有一个数据集,需要将其放入训练 (75%) 和测试 (25%) 集中.我不确定应该在 x 和大小中输入哪些信息?x 是数据集文件,我有多少个样本?
I have a dataset that I need to put into a training (75%) and testing (25%) set. I'm not sure what information I'm supposed to put into the x and size? Is x the dataset file, and size how many samples I have?
推荐答案
实现数据分区的方法有很多种.如需更完整的方法,请查看 caTools
包中的 createDataPartition
函数.
There are numerous approaches to achieve data partitioning. For a more complete approach take a look at the createDataPartition
function in the caTools
package.
这是一个简单的例子:
data(mtcars)
## 75% of the sample size
smp_size <- floor(0.75 * nrow(mtcars))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(mtcars)), size = smp_size)
train <- mtcars[train_ind, ]
test <- mtcars[-train_ind, ]
这篇关于如何使用样本函数将数据拆分为训练/测试集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文