Guest User

Untitled

a guest
Jan 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. # Use the Cars93 dataset from the MASS package to
  2. # Use KNN to classify Origin of cars using cars characteristics
  3. set.seed(8675309)
  4. rm(list = ls())
  5. library(MASS) # Cars93 dataset
  6. library(class) # KNN function
  7. library(gmodels) # CrossTable function
  8.  
  9. data(Cars93)
  10.  
  11. # remove incomplete rows
  12. cars <- Cars93[complete.cases(Cars93), ]
  13.  
  14. # hold out 25 random observations
  15. holdOutIndex <- sample(1:nrow(cars), 25, replace = FALSE)
  16.  
  17. # get numeric variables and rescale them
  18. numerics <- sapply(cars, is.numeric)
  19. # separate the labels
  20. target <- cars$Origin
  21. trainingTarget <- target[-holdOutIndex]
  22. testingTarget <- target[holdOutIndex]
  23.  
  24. scaledCars <- data.frame(sapply(cars[numerics],
  25. function(x) scale(x, center = TRUE, scale = TRUE)))
  26.  
  27. # compare pre and post scaling
  28. summary(cars)
  29. summary(scaledCars)
  30.  
  31. # build back the training and testing dataset
  32. training <- scaledCars[-holdOutIndex, ]
  33. testing <- scaledCars[holdOutIndex, ]
  34.  
  35. # run the model [default to k = sqrt(nobs) ~ 9]
  36. k <- 9
  37. knnPredictions <- knn(train = training, test = testing, cl = trainingTarget, k = k)
  38. CrossTable(testingTarget, knnPredictions, prop.chisq = FALSE)
  39.  
  40. # 8 True positives, 12 true negatives, 2 false negatives, 3 false positives
Add Comment
Please, Sign In to add comment