gpt4 book ai didi

R : Caret how do we pass a parameter for k for kNN

转载 作者:行者123 更新时间:2023-12-02 20:39:38 26 4
gpt4 key购买 nike

我使用插入符号表示 knn,并且最初使用tuneLength=10 运行该过程我发现用于模型的 k=21

我想使用一组特定的 k 值来运行参数,但在传递uneGrid中的值或将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))
}

最佳答案

我知道您只想指定k。我在Cross Validated中找到了答案.

请参阅 train 函数的 tuneGrid 参数。

试试这个:

Fit.kNN <- train(response~., data = df, method = "knn", metric = metric, 
preProc = preProc, trControl = control, tuneGrid = expand.grid(k = 1:25))

关于R : Caret how do we pass a parameter for k for kNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46272797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com