gakonst

Untitled

Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. rm(list=ls())
  2.  
  3. library(class) # imports knn
  4. library(MLmetrics) # imports accuracy
  5.  
  6. glass = data(Glass, package = 'mlbench')
  7. training = Glass[c(1:50, 91:146), -10]
  8. trainingType = factor(Glass[c(1:50, 91:146), 10])
  9. testing = Glass[51:90, -10]
  10. testingType = factor(Glass[51:90, 10])
  11.  
  12. # cor(training)
  13. pca_model <- prcomp(training, center = TRUE, scale = TRUE)
  14. eigenvectors = pca_model$rotation
  15. eigenvalues = pca_model$sdev^2
  16.  
  17. # Plot the variance percentage for each component
  18. barplot(eigenvalues / sum(eigenvalues))
  19.  
  20. # Q1
  21. eigenvalues[1]/sum(eigenvalues)
  22.  
  23. # Q2 Info loss = percentage of not included eigenvalues
  24. sum(eigenvalues[-c(1:4)])/sum(eigenvalues)
  25.  
  26. # Q3
  27. model = knn(training, testing, trainingType, k = 3)
  28. Accuracy(model, testingType)
  29.  
  30. # Q4 same model - incorrect? elearning = 0.8
  31. Recall(model, testingType, '2')
  32.  
  33. max_acc = 0
  34. max_i = 1
  35. for (i in 1:9) {
  36. pcs_training = as.data.frame(predict(pca_model, training)[, 1:i])
  37. pcs_testing = as.data.frame(predict(pca_model, testing)[, 1:i])
  38. model = knn(pcs_training, pcs_testing, trainingType, k = 3)
  39. # print(Accuracy(model, testingType))
  40. # print(i)
  41. acc = Accuracy(model, testingType)
  42. if (acc > max_acc){
  43. max_acc = acc
  44. max_i = i
  45. }
  46. }
  47.  
  48. # Q5
  49. max_acc
  50. max_i
Advertisement
Add Comment
Please, Sign In to add comment