library(kernlab)
data(spam)
dim(spam)
indi <- sample(1:dim(spam)[1],4000)
spam.train <- spam[indi,]
spam.test <- spam[-indi,]
model.knn <- knn3(type~.,data=spam.train,k=1)
mean(predict(model.knn,spam.test,type="class")==spam.test$type)

model.knn <- knn3(type~.,data=spam.train,k=6)
mean(predict(model.knn,spam.test,type="class")==spam.test$type)
library(caret)

model.knn <- train(type~.,data=spam.train,method="knn",metric="Accuracy",
    trControl=trainControl(method="repeatedcv",number=2,repeats=10),
                   tuneGrid=data.frame(k=1:10))

mean(predict(model.knn,spam.test,type="raw")==spam.test$type)


Modifié le: vendredi 11 septembre 2020, 17:03