3
$\begingroup$

This is my example of KNN model (I write it using R):

library(gmodels)
library(caret)
library(class)

db_class <- iris

row_train <- sample(nrow(db_class),nrow(db_class)*0.8)
db_train_x <- db_class[row_train,-ncol(db_class)]
db_train_y <- db_class[row_train,ncol(db_class)]
db_test_x <- db_class[-row_train,-ncol(db_class)]
db_test_y <- db_class[-row_train,ncol(db_class)]

model_knn <- knn(db_train_x,db_test_x,db_train_y,12)

summary(model_knn)

CrossTable(x=db_test_y,y=model_knn,prop.chisq = FALSE)
confusionMatrix(data=factor(model_knn),reference=factor(db_test_y))

So, this is a supervised KNN models. How can I classify a new registration? I have this new registration:

new_record <- c(5.3,3.2,2.0,0.2)

How can I classify it using the previous model?

$\endgroup$
2
  • $\begingroup$ I am not that familiar with caret but I think you have to use the predict function and pass it a fitted model (so you'd also first have to use train to train your model on the data). See also this example. $\endgroup$
    – Oxbowerce
    Sep 21, 2021 at 12:34
  • $\begingroup$ ok and how can i do to use it with iris dataset? $\endgroup$
    – Inuraghe
    Sep 21, 2021 at 13:39

1 Answer 1

2
$\begingroup$
  1. You can use the general train from caret to train the model
  2. The new entry needs to be added in the form of the Train set, only then it will be able to predict

I would have done this like this:

library(caret)

model_knn<-train(Species ~ ., data = db_class[row_train,], method = "knn",tuneLength = 10)

#You can select any other tune length too. This is just an example.
#You can even choose to preprocess the data, with the train parameter

Now you will have to convert the new_record to a suitable data frame:

new_record <- c(5.3,3.2,2.0,0.2)

test_data <- NULL

i<-1

while (i <= length(new_record)) {
  test_data <- cbind(new_record[i], test_data)
  i<- i+1
}

colnames(test_data)<-colnames(db_class)[1:4]

Now you can make the prediction:

predict(model_knn, newdata=test_data)

[1] versicolor
Levels: setosa versicolor virginica

Prediction using your test data:

predict(model_knn, newdata=db_test_x)
 [1] setosa     setosa     setosa     setosa     setosa     setosa     setosa     versicolor
 [9] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[17] versicolor versicolor versicolor versicolor virginica  versicolor virginica  virginica 
[25] virginica  virginica  virginica  virginica  versicolor virginica 
Levels: setosa versicolor virginica

Does this solve your purpose?

$\endgroup$
10
  • $\begingroup$ No, when I run: predict(model_knn, newdata=test_data) I have this error: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "factor" $\endgroup$
    – Inuraghe
    Sep 22, 2021 at 6:50
  • $\begingroup$ @Inuraghe have you run the model_knn that I have run here? It should be a list and not a "factor" $\endgroup$ Sep 22, 2021 at 7:08
  • $\begingroup$ Sorry, now it works. I tested it with several new records and it always returns the class "versicolor", even if I used values belonging to other classes as a test. Is it a code problem (so it always returns the same class) or is it a model problem (so it has a low accuracy and is not suitable for this problem)? $\endgroup$
    – Inuraghe
    Sep 22, 2021 at 7:19
  • $\begingroup$ I noticed that the new record is inserted in test_data in reverse, so the first value is put in the last column and so on... how can I fix it? $\endgroup$
    – Inuraghe
    Sep 22, 2021 at 7:43
  • $\begingroup$ @Inuraghe are you setting the i<-1 whenever you're running it? $\endgroup$ Sep 22, 2021 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.