机器学习中具有10倍交叉验证的代码 [英] Code with 10 fold cross validation in machine learning

查看:387
本文介绍了机器学习中具有10倍交叉验证的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用机器学习.我尝试使用C5.0模型运行10倍交叉验证.我要求代码返回kappa值.

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)})

给出以下错误提示:

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!

推荐答案

如果没有可复制的示例,这会有些困难,但是我认为共享最后一行的收益是原因.我对您的代码进行了重新格式化,以提高可读性

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)
  })

这篇关于机器学习中具有10倍交叉验证的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆