Error: Too Many Ties in knn

Asked

Viewed 29 times

0

I am trying to make a classification in a database using the KNN, without success with the following code:

#Fase 2: KNN
#Preparação df para o KNN
dfNormal = df
dfNormal[, -length(df)] = scale(dfNormal[, -length(df)])
train_index = sample(1:nrow(dfNormal), 0.6*nrow(dfNormal), replace = FALSE)
treino = data.frame()
treino = dfNormal[train_index,]
teste = data.frame()
teste = dfNormal[-train_index,]

#Execução do KNN
library("class")
Knn_Testes = list()
acuracia = numeric()
for(k in 1:20){
  Knn_Testes[[k]] = knn(treino[, -length(treino)], teste[,-length(treino)], treino$stress, k, prob=TRUE)
  acuracia[k] = sum(Knn_Testes[[k]]==teste$stress)/length(teste$stress)*100
}

But I get the following error:

Error in knn(treino[, -length(treino)], teste[, -length(treino)], treino$stress,  : 
  too many ties in knn
  • Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.