Importing Libraries

library(ggplot2)
library(caret)
## Warning: package 'caret' was built under R version 4.3.3
## Loading required package: lattice
library(class)

Importing Dataset

df<-read.csv("C:/Users/ONKAR/OneDrive/Desktop/SEM-4/Projects/Data Science/dataset.csv")

Spliting Dataset Into Train and test set

set.seed(123)
data_split_index <-sample(2,nrow(df),replace = TRUE,prob<-c(0.8,0.2))
train<-df[data_split_index==1,-12]
train_labels<-df[data_split_index==1,12]
test_demo<-df[data_split_index==2,]
target<-as.factor(test_demo[,12])
test<-df[data_split_index==2,-12]

Converting train and test data to matrix form

train<-model.matrix(~ . -1,data = train)
test<-model.matrix(~.-1,data = test)

Applying KNN

k=10
knn_model <- knn(train = train, test = test, cl = train_labels, k = k)
knn_predictions<-as.factor(knn_model)

Confusion Matrix

knn_confusion_matrix<-confusionMatrix(knn_predictions,target)
knn_confusion_matrix
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   Close Neutral Very close
##   Close         24      12         11
##   Neutral        1      11          0
##   Very close    11       2         22
## 
## Overall Statistics
##                                           
##                Accuracy : 0.6064          
##                  95% CI : (0.5002, 0.7056)
##     No Information Rate : 0.383           
##     P-Value [Acc > NIR] : 9.629e-06       
##                                           
##                   Kappa : 0.3886          
##                                           
##  Mcnemar's Test P-Value : 0.01017         
## 
## Statistics by Class:
## 
##                      Class: Close Class: Neutral Class: Very close
## Sensitivity                0.6667         0.4400            0.6667
## Specificity                0.6034         0.9855            0.7869
## Pos Pred Value             0.5106         0.9167            0.6286
## Neg Pred Value             0.7447         0.8293            0.8136
## Prevalence                 0.3830         0.2660            0.3511
## Detection Rate             0.2553         0.1170            0.2340
## Detection Prevalence       0.5000         0.1277            0.3723
## Balanced Accuracy          0.6351         0.7128            0.7268

Accuracy

accuracy<-knn_confusion_matrix$overall["Accuracy"]*100
cat("Accuracy of model is ",accuracy," %")
## Accuracy of model is  60.6383  %

Why K = 10

Accucary is max at K = 10

K == 3 -> 48%

K == 5 -> 55%

K == 8 -> 58%

K == 10 -> 61%

K == 11 -> 58%

K == 12 -> 56%

K == 13 -> 55%

So for max accuracy is 61 % when k = 10