Code

Implementation of K-Nearest Neighbors (KNN) Classifier in R

To implement a K-Nearest Neighbors (KNN) classifier in R, you can use the knn() function from the class package. The general process involves data preparation, prediction, and evaluation:

  1. Clean Dataset: KNN requires numerical inputs. Convert categorical variables into dummy variables:
data_cleaned <- as.data.frame(model.matrix(~ . - 1, data = dataset))
  1. Normalize Dataset: Scale numerical features to ensure they contribute equally to the distance metric:
normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) } data_normalized <- as.data.frame(lapply(data_cleaned, normalize))
  1. Train-Test Split: Separate the labels and split the dataset:
labels <- data_normalized$outputcolumn data_normalized$outputcolumn <- NULL train <- data_normalized[1:split_index, ] test <- data_normalized[(split_index + 1):nrow(data_normalized), ] train_labels <- labels[1:split_index] test_labels <- labels[(split_index + 1):length(labels)]
  1. Build Model and Predict:
library(class) prediction <- knn(train = train, test = test, cl = train_labels, k = 5)
  1. Evaluate Model: Assess accuracy using tools like CrossTable from the gmodels package:
library(gmodels) CrossTable(test_labels, prediction)

0

2

Updated 2026-07-05

Tags

Data Science