Guest User

knn

a guest
Nov 16th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. bank <- read.csv("data/bank/bank.csv")
  2.  
  3. bank$job=NULL
  4. bank$marital=NULL
  5. bank$education=NULL
  6. bank$housing=NULL
  7. bank$loan=NULL
  8. bank$contact=NULL
  9. bank$month=NULL
  10. bank$poutcome=NULL
  11. bank$default=NULL
  12.  
  13. # table of success/failure
  14. table(bank$y)
  15.  
  16.  
  17. # recode y as a factor
  18. bank$y <- factor(bank$y,
  19. levels = c("yes", "no"),
  20. labels = c("Success", "Fail"))
  21.  
  22. # table or proportions with more informative labels
  23. prop.table(table(bank$y))
  24.  
  25.  
  26. # summarize three numeric features
  27. summary(bank[c("age", "balance",
  28. "duration")])
  29.  
  30.  
  31. # create normalization function
  32. normalize <- function(x) {
  33. return ((x - min(x)) / (max(x) - min(x)))
  34. }
  35.  
  36.  
  37. # test normalization function - result should be identical
  38. normalize(c(1, 2, 3, 4, 5))
  39. normalize(c(10, 20, 30, 40, 50))
  40.  
  41.  
  42. # note doesn’t include the labels
  43. bank_n <- as.data.frame(lapply(bank[1:7], normalize))
  44. summary(bank_n$age)
  45. hist(bank_n$age)
  46.  
  47.  
  48. # create training and test data (no labels)
  49. bank_train <- bank_n[1:4421, ]
  50. bank_test <- bank_n[4422:4521, ]
  51.  
  52.  
  53. # create labels for training and test data
  54. bank_train_labels <- bank_n[1:4421, 1]
  55. bank_test_labels <- bank_n[4422:4521, 1]
  56.  
  57.  
  58. ## Step 3: Training a model on the data ----
  59. library(class)
  60. predictions <- knn(train = bank_train, test =
  61. bank_test, cl = bank_train_labels, k=21)
  62.  
  63.  
  64. ## Step 4: Evaluating model performance ----
  65. # load the "gmodels" library
  66. #install.packages("gmodels")
  67. library(gmodels)
  68.  
  69. # Create the cross tabulation of predicted vs. actual
  70. CrossTable(predictions, bank_test_labels,
  71. prop.chisq = FALSE,
  72. prop.c = FALSE, prop.r = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment