Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rm(list=ls())
- library(class) # imports knn
- library(MLmetrics) # imports accuracy
- glass = data(Glass, package = 'mlbench')
- training = Glass[c(1:50, 91:146), -10]
- trainingType = factor(Glass[c(1:50, 91:146), 10])
- testing = Glass[51:90, -10]
- testingType = factor(Glass[51:90, 10])
- # cor(training)
- pca_model <- prcomp(training, center = TRUE, scale = TRUE)
- eigenvectors = pca_model$rotation
- eigenvalues = pca_model$sdev^2
- # Plot the variance percentage for each component
- barplot(eigenvalues / sum(eigenvalues))
- # Q1
- eigenvalues[1]/sum(eigenvalues)
- # Q2 Info loss = percentage of not included eigenvalues
- sum(eigenvalues[-c(1:4)])/sum(eigenvalues)
- # Q3
- model = knn(training, testing, trainingType, k = 3)
- Accuracy(model, testingType)
- # Q4 same model - incorrect? elearning = 0.8
- Recall(model, testingType, '2')
- max_acc = 0
- max_i = 1
- for (i in 1:9) {
- pcs_training = as.data.frame(predict(pca_model, training)[, 1:i])
- pcs_testing = as.data.frame(predict(pca_model, testing)[, 1:i])
- model = knn(pcs_training, pcs_testing, trainingType, k = 3)
- # print(Accuracy(model, testingType))
- # print(i)
- acc = Accuracy(model, testingType)
- if (acc > max_acc){
- max_acc = acc
- max_i = i
- }
- }
- # Q5
- max_acc
- max_i
Advertisement
Add Comment
Please, Sign In to add comment