Skip to content

Instantly share code, notes, and snippets.

@Harshit1694
Created June 27, 2019 12:27
Show Gist options
  • Save Harshit1694/614dc7641f42ddc83ac0b36bc83bd9dd to your computer and use it in GitHub Desktop.
Save Harshit1694/614dc7641f42ddc83ac0b36bc83bd9dd to your computer and use it in GitHub Desktop.
install.packages("class")
library(class)
#Normalization
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x))) }
norm <- as.data.frame(lapply(data[,1:4], normalize))
set.seed(123)
data_spl <- sample(1:nrow(norm),size=nrow(norm)*0.7,replace = FALSE)
train2 <- data[data_spl,] # 70% training data
test2 <- data[-data_spl,] # remaining 30% test data
train_labels <- data[data_spl,5]
test_labels <-data[-data_spl,5]
knn_pred <- knn(train=train2, test=test2, cl=train_labels, k=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment