Advertisement
Guest User

KNN chunk NBA

a guest
Jul 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.45 KB | None | 0 0
  1. ```{r knnModel}
  2. library(pacman)
  3. p_load(caret, ggplot)
  4.  
  5. knnTrain = train_final
  6.  
  7. knnTest = test_final
  8.  
  9. # Categoricals to factors
  10. for (i in c(8:10)){
  11.   knnTrain[,i] <- as.factor(knnTrain[,i])
  12.   knnTest[,i] <- as.factor(knnTest[,i])
  13. }
  14.  
  15. # Sample for faster performance
  16. set.seed(7)
  17. sample1 = sample(1:84616, 1000)
  18.  
  19. # Adding names to factors due to error when running train() "one of the class levels is not a valid R variable name"
  20. levels(knnTrain$LOCATION_HOME) = c("A", "H")
  21. levels(knnTrain$FGM) = c("miss", "made")
  22. levels(knnTrain$PTS_TYPE_2) = c("three", "two")
  23.  
  24. levels(knnTest$LOCATION_HOME) = c("A", "H")
  25. levels(knnTest$FGM) = c("miss", "made")
  26. levels(knnTest$PTS_TYPE_2) = c("three", "two")
  27.  
  28. # Setting parameters for tuning repetitions and cross-validation in model
  29. ctrl <- trainControl(
  30.   classProbs = TRUE,
  31.   method = "cv", # cross-validation method
  32.   number = 5,    # number of folds
  33.   )
  34.  
  35. # Modeling with training set
  36. fgmknnModel <- train(FGM~., data = knnTrain, method = "knn", tuneGrid=expand.grid(k = c(200, 300)), trControl = ctrl)
  37. fgmknnModel
  38. summary(fgmknnModel)
  39. # Determining and plotting the variable importance
  40. fgmimpvar = varImp(fgmknnModel, scale = T)
  41. ggplot(fgmimpvar)
  42.  
  43. # Predicting shot outcome and establishing final accuracy using test set
  44. TestPred <- predict(fgmknnModel, knnTest)
  45.  
  46. # Creating the confusion matrix using the predicted and test values for FGM
  47. confusionMatrix(TestPred, knnTest$FGM, positive = "made")
  48.  
  49. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement