R-k折交叉验证
2016-08-10 18:08
423 查看
training <-iris #抽样方法 #ind<-sample(2,nrow(training),replace=TRUE,prob=c(0.7,0.3)) #对数据分成两部分,70%训练数据,30%检测数据nrow(training)行数 #traindata<- training [ind==1,] #训练集 #testdata<- training [ind==2,] #测试集 10-fold cross-validation 就是十折交叉验证,用来测试精度,是常用的精度测试方法。 将数据集分成十分,轮流将其中9份做训练1份做测试,10次的结果的均值作为对算法精度的估计, 一般还需要进行多次10倍交叉验证求均值,例如10次10倍交叉验证,更精确一点。 #使用切分函数-K折交叉验证 library("caret") folds<-createFolds(y=training$Species,k=10) #根据training的laber-Species把数据集切分成10等份 re<-{} for(i in 1:10){ traindata<-training[-folds[[i]],] testdata<-training[folds[[i]],] rf <- randomForest(Species ~ ., data=training, ntree=100, proximity=TRUE) #Species是因变量 re=c(re,length(training$Species[which(predict(rf)==training$Species)])/length(training$Species)) } mean(re)#取k折交叉验证结果的均值作为评判模型准确率的结果
参考其他R包自动测试
现存的方法很多都会自带现成的工具caret ## Not run: ## Do 5 repeats of 10-Fold CV for the iris data. We will fit ## a KNN model that evaluates 12 values of k and set the seed ## at each iteration. set.seed(123) seeds <- vector(mode = "list", length = 51) for(i in 1:50) seeds[[i]] <- sample.int(1000, 22) ## For the last model: seeds[[51]] <- sample.int(1000, 1) ctrl <- trainControl(method = "repeatedcv",repeats = 5,seeds = seeds) set.seed(1) mod <- train(Species ~ ., data = iris,method = "knn",tuneLength = 12,trControl = ctrl) ctrl2 <- trainControl(method = "adaptive_cv",repeats = 5,verboseIter = TRUE,seeds = seeds) set.seed(1) mod2 <- train(Species ~ ., data = iris,method = "knn",tuneLength = 12,trControl = ctrl2)
相关文章推荐
- Redis Twemproxy
- *_train_test.prototxt,*_deploy.prototxt,*_slover.prototxt文件编写时注意事项
- How to Install Subversion (SVN) 1.8.15 on CentOS/RHEL 7/6/5
- sizeof浅析,求结构体大小
- zookeeper 入门讲解实例 转
- Mysql --分区表的管理与维护
- 不可取代的网站开发工具---------dreamweaver
- ArrayList类源码浅析(二)
- mysql查杀会话
- 适配器与组合模式
- 机器学习(1)之梯度下降(gradient descent)
- mac gradle path设置
- dubbox 中kryo序列化 KryoException: Encountered unregistered class ID: 114
- Unity -- Mask
- [Android]_[初级]_[adb 实用命令]
- 判断一个文件是否为二进制文件
- OC中的字典dictionary
- [Android]_[初级]_[adb 实用命令]
- CentOS6.5安装nginx
- 移动端框架