gpt4 book ai didi

r - 如何使用遗传算法优化knn中的参数k

转载 作者:行者123 更新时间:2023-12-03 17:38:25 26 4
gpt4 key购买 nike

我尝试使用r中的遗传算法来优化knn中的参数k。我使用以下代码尝试了此操作,但仍然收到错误。
我根据所选的k值将knn的精度用作适应度函数。如果您了解knn和遗传算法,请帮助我。这是我所做的。

 library(caret)
library(GA)
library(class)

#data import
tea_jenis_F3 <- read.csv("D:/inggrit/program/F3.csv")
str(tea_jenis_F3)

#to check missing data
anyNA(tea_jenis_F3)

#data slicing
set.seed(101)
intrain_jenis_F3 <- createDataPartition(tea_jenis_F3$category, p= 0.7, list = FALSE)
training_jenis_F3 <- tea_jenis_F3 [intrain_jenis_F3,]
testing_jenis_F3 <- tea_jenis_F3 [-intrain_jenis_F3,]

#transforming the dependent variable to a factor
training_jenis_F3[["category"]] = factor(training_jenis_F3[["category"]])

#fitness function
fitness_KNN <- function(chromosome)
{
# First values in chromosome are 'k' of 'knn' method
tuneGrid <- data.frame(k=chromosome[1])


# train control
train_control <- trainControl(method = "cv",number = 10)

# train the model
set.seed(1234)
model <- train(category ~ ., data= training_jenis_F3, trControl=train_control,
method="knn", tuneGrid=tuneGrid)

# Extract accuracy statistics
accuracy_val <- model$results$accuracy

}


GA <- ga(type = "real-valued", fitness = fitness_KNN, lower = -10, upper = 10, monitor = NULL)




错误:

Something is wrong; all the Accuracy metric values are missing:
Accuracy Kappa
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :1 NA's :1
Error: Stopping
In addition: There were 11 warnings (use warnings() to see them)


如果您能帮助我,我将不胜感激。谢谢

最佳答案

我认为问题不在于您的代码,而在于方法:使用遗传算法在此设置中优化k是不可能的,也是没有必要的。

您称为ga(type = "real-valued", lower = -10, upper = 10, ...),这意味着ga将在-10和10之间搜索最佳值。现在有两个问题:


的负值不适用于knn
k将产生非整数值,例如k为1.234,这当然也是不可能的


幸运的是,在这种情况下,不必使用诸如遗传算法之类的复杂方法。如果要在[1,10]范围内找到最佳 ga,只需为每个值计算模型,如下所示:

k_cands <- 1:10
accuracy <- numeric()

for(k in k_cands) {
[compute model with k]
accuracy <- c(accuracy, model$results$accuracy)
}

best_k <- k_cands[which.max(accuracy)]

关于r - 如何使用遗传算法优化knn中的参数k,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51928823/

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