0

如何将以下用 python 编写的代码写入 R ?

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.2, random_state=42)   

拆分为训练集和测试集 80/20 的比例。

4

4 回答 4

5

可能是更简单的方法

#read in iris dataset 
 data(iris)  
 library(caret) #this package has the createDataPartition function
    
 set.seed(123) #randomization`
    
 #creating indices
 trainIndex <- createDataPartition(iris$Species,p=0.75,list=FALSE)
    
 #splitting data into training/testing data using the trainIndex object
 IRIS_TRAIN <- iris[trainIndex,] #training data (75% of data)
    
 IRIS_TEST <- iris[-trainIndex,] #testing data (25% of data)
于 2017-11-09T20:16:26.433 回答
1

您可以使用caret'screateDataPartition函数执行此操作:

library(caret)

# Make example data
X = data.frame(matrix(rnorm(200), nrow = 100)) 
y = rnorm(100) 

#Extract random sample of indices for test data
set.seed(42) #equivalent to python's random_state arg
test_inds = createDataPartition(y = 1:length(y), p = 0.2, list = F) 

# Split data into test/train using indices
X_test = X[test_inds, ]; y_test = y[test_inds] 
X_train = X[-test_inds, ]; y_train = y[-test_inds]

您还可以使用test_inds“从头开始”创建test_inds = sample(1:length(y), ceiling(length(y) * 0.2))

于 2017-11-09T20:05:28.450 回答
1

使用基础 R,您可以执行以下操作:

set.seed(12345)
#getting training data set sizes of .20 (in this case 20 out of 100)
train.x<-sample(1:100, 20)
train.y<-sample(1:100, 20)

#simulating random data
x<-rnorm(100)
y<-rnorm(100)

#sub-setting the x data
training.x.data<-x[train]
testing.x.data<-x[-train]

#sub-setting the y data
training.y.data<-y[train]
testing.y.data<-y[-train]
于 2017-11-09T20:20:24.990 回答
0

让我们拿iris数据集:

# in case you want to use a seed
set.seed(5)
## 70% of the sample size
train_size <- floor(0.75 * nrow(iris))

in_rows <- sample(c(1:nrow(iris)), size = train_size, replace = FALSE)

train <- iris[in_rows, ]
test <- iris[-in_rows, ]
于 2020-08-19T22:55:06.837 回答