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?
caret
but I think you have to use thepredict
function and pass it a fitted model (so you'd also first have to usetrain
to train your model on the data). See also this example. $\endgroup$