library(kernlab) #used for ksvm library(kknn) #used for knn
#load the data data <- read.table(“credit_card_data.txt”, header=F, stringsAsFactors = F) #see the first few rows head(data)
#set up model model <- ksvm(as.matrix(data[,1:10]),as.factor(data[,11]), C=100, scaled = T, kernel=“vanilladot”, type = “C-svc”)
#take a look at it model
#calculating the coefficients (a1..am) a <- colSums(model@xmatrix[[1]] * model@coef[[1]] ) #print out a a
#calculate a0 a0 <- -model@b #print a0 a0
#lets see what the model predicts pred <- predict(model, data[,1:10]) pred
#lets observe the percentage of the model’s correct predictions. sum(pred == data[,11]) / nrow(data) * 100
acc_chk <- function(Z) { pred <- rep(0, nrow(data))
for (i in 1:nrow(data)) {
knn_model <- kknn(
V11 ~ V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9 + V10,
train = data[-i, ], # training data (leave one out)
test = data[i, ], # test point
k = Z,
scale = TRUE
)
pred[i] <- as.integer(fitted(knn_model) + 0.5)
}
acc <- sum(pred == data[,11]) / nrow(data) return(acc) } test_vec <- rep(0,30) # 30 zeroes vector for accuracy test (knn values ranging from 1 to 30) for (Z in 1:30){ test_vec[Z] = acc_chk(Z) }
knn_accuracy <- as.matrix(test_vec * 100) #see accuracy as percentage knn_accuracy #print out knn values and percentage of accuracy
knn_value <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30)
plot(knn_value,knn_accuracy)#observe accuracies per knn value
max(knn_accuracy)