Code with 10 fold cross validation in machine learning

元气小坏坏 提交于 2019-12-12 03:55:23

问题


I am just starting to work with machine learning. I tried to run a 10 fold cross-validation using a C5.0 model. I asked the code to return the kappa value.

folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)
str(folds)
mdd.cohort1_train = mdd.cohort1[-folds$Fold01,]
mdd.cohort1_test = mdd.cohort1[folds$Fold01,]
library(caret)
library(C5.0)
library(irr)
set.seed(123)
folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

cv_results = lapply(folds, function(x) 
{mdd.cohort1_train = mdd.cohort1[-x, ] 
mdd.cohort1_test = mdd.cohort1[x, ] 
mdd.cohort1_model = C5.0(edmsemmancomprej ~., data = mdd.cohort1_train) 
mdd.cohort1_pred = predict(mdd.cohort1_model, mdd.cohort1_test) 
mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej 
kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value    return(kappa)})

Gives the following error menssage:

Error: unexpected symbol in:
"mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej
kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value return"

Does anyone knows what happened? Thank you so much in advance!


回答1:


It is a bit difficult without a reproducible example but I think that the return sharing that last line is the cause. I reformatted your code a bit for readability

library(caret)
library(C5.0)
library(irr)   

folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

str(folds)

mdd.cohort1_train = mdd.cohort1[-folds$Fold01,]
mdd.cohort1_test = mdd.cohort1[folds$Fold01,]

set.seed(123)
folds = createFolds(mdd.cohort1$edmsemmancomprej, k=10)

cv_results = lapply(folds, function(x) {
  mdd.cohort1_train = mdd.cohort1[-x, ] 
  mdd.cohort1_test = mdd.cohort1[x, ] 
  mdd.cohort1_model = C5.0(edmsemmancomprej ~., data = mdd.cohort1_train) 
  mdd.cohort1_pred = predict(mdd.cohort1_model, mdd.cohort1_test) 
  mdd.cohort1_actual = mdd.cohort1_test$edmsemmancomprej 
  kappa = kappa2(data.frame(mdd.cohort1_actual, mdd.cohort1_pred))$value    
  return(kappa)
  })


来源:https://stackoverflow.com/questions/35593418/code-with-10-fold-cross-validation-in-machine-learning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!