我正在使用caret for knn,我最初使用tuneLength = 10运行该过程 我发现用于模型的那个有k = 21
我想用一组特定的k值运行参数,我遇到错误传递tuneGrid中的值或直接将k值传递给列车函数
数据:
library(mlbench)
data(PimaIndiansDiabetes)
代码:
grid = expand.grid(k = c(5,7,9,15,19,21)
compute_learncurve5 <- function(df=adultFile,control=control,ratio=30,fold=10,N=3,metric="Accuracy",
seed=1234,scaled=FALSE,DEBUG=FALSE) {
result_df = c()
size <- round(size=(ratio/100 * nrow(df)))
split <- gsub(" ","",paste(as.character(100-ratio),"/",as.character(ratio)))
iter <- N
trainSize <- nrow(df)-size
testSize <- size
if (DEBUG){
print(paste("Dimension of InputDataSet : ", dim(df)))
print(paste("Test/Train Perct : ",ratio,"|",100-ratio,
" : Train/Test size = ", trainSize,"|",testSize))
}
#Set-up data
trainpct <- (100-ratio)/100
# Set-up Train and Test - Change target variable
inDfTraining <- createDataPartition(df$response, p = trainpct, list = FALSE)
inTraining <- df[ inDfTraining,]
inTesting <- df[-inDfTraining,]
# Run algo to generate MODEL
set.seed(seed)
metric=metric
control=control
grid = expand.grid(k. = c(5,7,10,15,19,21))
Fit.kNN <- train(response~., data=df, method="knn", metric=metric, preProc=preProc, trControl=control,
tuneGrid=grid)
}
learnCurve_df5 = c()
for (i in seq(95, 5, -5)) {
learnCurve_df5 <- rbind(learnCurve_df5,compute_learncurve5(df=adultFile))
}
答案 0 :(得分:1)
我了解您只想指定k
。我在doc中找到了答案。
请参见tuneGrid
函数的train
参数。
尝试一下:
Fit.kNN <- train(response~., data=df, method="knn", metric=metric, preProc=preProc, trControl=control, tuneGrid = expand.grid(k = 1:25))