我刚开始使用R,不确定如何将我的数据集与以下示例代码结合起来:
sample(x, size, replace = FALSE, prob = NULL)
我有一个数据集,需要将其分为训练集(75%)和测试集(25%)。我不确定应该放入x和size的信息是什么?x是数据集文件吗?size是指样本数量吗?
有许多方法可以实现数据分区。要了解更完整的方法,请查看caret
包中的createDataPartition
函数。
这里是一个简单的例子:
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, ]
mtcars[!train_ind]
,虽然它没有失败,但并没有按预期工作。我该如何使用 !
进行子集操作? - user989762这可以很容易地完成:
set.seed(101) # Set Seed so that same sample can be reproduced in future also
# Now Selecting 75% of data as sample from total 'n' rows of the data
sample <- sample.int(n = nrow(data), size = floor(.75*nrow(data)), replace = F)
train <- data[sample, ]
test <- data[-sample, ]
通过使用caTools软件包:
require(caTools)
set.seed(101)
sample = sample.split(data$anycolumn, SplitRatio = .75)
train = subset(data, sample == TRUE)
test = subset(data, sample == FALSE)
sample = sample.split(data[,1], SplitRatio = .75)
应该可以避免给列命名的需要。 - Benjamin Ziepert我会使用dplyr
来完成这个任务,它使得整个过程非常简单。但是你需要在数据集中包含一个id变量,这个变量不仅可以用于创建数据集,还可以在项目跟踪中提高可追溯性。如果没有包含id变量,就需要添加。
mtcars$id <- 1:nrow(mtcars)
train <- mtcars %>% dplyr::sample_frac(.75)
test <- dplyr::anti_join(mtcars, train, by = 'id')
这几乎是相同的代码,但外观更加美观。
bound <- floor((nrow(df)/4)*3) #define % of training and test set
df <- df[sample(nrow(df)), ] #sample rows
df.train <- df[1:bound, ] #get training set
df.test <- df[(bound+1):nrow(df), ] #get test set
sample
方法是内置的吗? - Regressordf <- df[sample(nrow(df)), ]
中,所有行都是随机抽样的。请查看 https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/sample 以获取更多关于 R 基础函数 sample
的信息。 - Spacezlibrary(caret)
intrain<-createDataPartition(y=sub_train$classe,p=0.7,list=FALSE)
training<-m_train[intrain,]
testing<-m_train[-intrain,]
我将把'a'数据集分成训练集(70%)和测试集(30%)
a # original data frame
library(dplyr)
train<-sample_frac(a, 0.7)
sid<-as.numeric(rownames(train)) # because rownames() returns character
test<-a[-sid,]
完成
我的解决方案基本上与dickoa的相同,但更易于理解:
data(mtcars)
n = nrow(mtcars)
trainIndex = sample(1:n, size = round(0.7*n), replace=FALSE)
train = mtcars[trainIndex ,]
test = mtcars[-trainIndex ,]
# choosing 75% of the data to be the training data
data_split <- initial_split(data, prop = .75)
# extracting training data and test data as two seperate dataframes
data_train <- training(data_split)
data_test <- testing(data_split)
rsample
是 tidymodels
框架的一部分,适用于使用 tidymodels 的用户。除了 initial_split
之外,还有 group_initial_split
和 initial_time_split
。 - undefined在查看了所有发布在这里的不同方法后,我没有看到任何人利用TRUE/FALSE
来选择和取消选择数据。因此,我想分享一种使用该技术的方法。
n = nrow(dataset)
split = sample(c(TRUE, FALSE), n, replace=TRUE, prob=c(0.75, 0.25))
training = dataset[split, ]
testing = dataset[!split, ]
R语言中有多种选择数据的方式,最常用的是使用正/负索引来选择/取消选择。然而,也可以使用 TRUE/FALSE
来进行选择/取消选择。
考虑以下示例。
# let's explore ways to select every other element
data = c(1, 2, 3, 4, 5)
# using positive indices to select wanted elements
data[c(1, 3, 5)]
[1] 1 3 5
# using negative indices to remove unwanted elements
data[c(-2, -4)]
[1] 1 3 5
# using booleans to select wanted elements
data[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
[1] 1 3 5
# R recycles the TRUE/FALSE vector if it is not the correct dimension
data[c(TRUE, FALSE)]
[1] 1 3 5
只需使用强大的dplyr库,就可以更简洁、更简单地完成以下操作:
library(dplyr)
set.seed(275) #to get repeatable data
data.train <- sample_frac(Default, 0.7)
train_index <- as.numeric(rownames(data.train))
data.test <- Default[-train_index, ]
Default[-train_index,]
? - Matt L.
x
可以是你的data
的索引(行/列编号)。size
可以是0.75*nrow(data)
。尝试使用sample(1:10, 4, replace = FALSE, prob = NULL)
查看它的作用。 - harkmug