Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.