Use of Knn function in R

Asked

Viewed 200 times

1

I’m studying clusters using R.

My question is whether, to classify new entrants with an already performed Clusterization, the function Knn can be used, whether for hierarchical or partitional clusters.

  • 3

    I don’t know anything about R, but as far as I know Knn is not used to classify/evaluate an unknown entrant.

  • Victor, thanks for the support. I understand that this function serves to classify new entrants. My question is whether I can use it for hierarchical clusters (e.g., hclust()) as well as partitional (e.g., kmeans).

1 answer

3

It depends on which package you are using Knn, but usually to evaluate the model you use the function predict(modelo, novo_dado).

Follow an example using the package caret who already chooses the best parameters automatically:

library(caret)

# fixar semente RNG para resultado reprodutível
set.seed(123)

# treinar modelo knn utilizando a coluna Species como saída
knn_model <- train(
  Species ~ .,
  data = iris,
  method = 'knn'
)

print("Resultado do treinamento:")
print(knn_model)

print("Predição da primeira linha:")
print(predict(knn_model, iris[1,]))

Exit:

[1] "Resultado do treinamento:"
k-Nearest Neighbors 

150 samples
  4 predictor
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 
Resampling results across tuning parameters:

  k  Accuracy   Kappa    
  5  0.9583142  0.9364989
  7  0.9573900  0.9350381
  9  0.9599157  0.9391095

Accuracy was used to select the optimal model using the
 largest value.
The final value used for the model was k = 9.
[1] "Predição da primeira linha:"
[1] setosa
Levels: setosa versicolor virginica
  • Lucas, thanks for your help. I’ll test it here. Hugs.

Browser other questions tagged

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