

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
ISYE 6501 Course homework assignment one solution
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!
#install.packages("kknn") library (kknn)
Read the file
data <- read.table ("/Users/marylin/Downloads/hw1/data 2.2/credit_card_data-headers.txt", header=TRUE)
Set up predictors and response
x <- data[, - 11] y <- data[,11]
Call kknn
k <- 8 y_pred <- vector ("integer", length (y))
#exclude i in the nearest neighbor for (i in 1 :nrow (data)) { train_x <- as.matrix (data[, - 11]) train_y <- data[,11]
train_data <- data.frame (train_x, R1 = train_y)
test_X <- data[i, - 11, drop=FALSE] test_data <- data.frame (test_X)
#kknn knn_model <- kknn (R1 ~ ., train_data, test_data, k=k, kernel="rectangular")
#predict y_pred[i] <- as.integer ( fitted (knn_model)) }
Calculate how good the classifier is
accuracy <- sum (y_pred == y) / length (y)
confusion_matrix <- data.frame (y, y_pred) precision <- confusion_matrix[2,2] / sum (confusion_matrix[,2]) recall <- confusion_matrix[2,2] / sum (confusion_matrix[2,]) f1 <- 2 ***** (precision ***** recall) / (precision + recall)
Print the output
accuracy
precision
recall
f