R语言将数据拆分为测试集和_R语言科研基础知识:把基因表达数据随机分为训练集和和测试集...

将数据集分为训练集和和测试方法

方案一:

library(ISLR)
attach(Smarket)
smp_siz = floor(0.75*nrow(Smarket)) 
 # creates a value for dividing the data into train and test. In this case the value is defined as 75% of the number of rows in the data
 setsmp_siz  # shows the value of the sample size
## [1] 937
set.seed(123)   # set seed to ensure you always have same random numbers generated
train_ind = sample(seq_len(nrow(Smarket)),size = smp_siz)  # Randomly identifies therows equal to sample size ( defined in previous instruction) from  all the rows of Smarket dataset and stores the row number in train_ind
train =Smarket[train_ind,] #creates the training dataset with row numbers stored in train_indtest=Smarket[-train_ind,]  # creates the test dataset excluding the row numbers mentioned in trai
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值