Advertisement
gry1994tistorycom

Untitled

May 30th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.03 KB | None | 0 0
  1. library(nnet) # 인공신경망 구현 라이브러리
  2. library(party)# CART 라이브러리
  3. library(caret)
  4. library(doParallel)
  5. library(ada)
  6. library(gbm)
  7. library(randomForest)
  8. library(rpart)
  9. library(adabag)
  10.  
  11. earthquate_data <- read.csv("Earthquate_Damage.csv")
  12.  
  13. # Performance Evaluation Function -----------------------------------------
  14. perf_eval <- function(cm){
  15.  
  16.   # True positive rate: TPR (Recall)
  17.   TPR <- cm[2,2]/sum(cm[2,])
  18.   # Precision
  19.   PRE <- cm[2,2]/sum(cm[,2])
  20.   # True negative rate: TNR
  21.   TNR <- cm[1,1]/sum(cm[1,])
  22.   # Simple Accuracy
  23.   ACC <- (cm[1,1]+cm[2,2])/sum(cm)
  24.   # Balanced Correction Rate
  25.   BCR <- sqrt(TPR*TNR)
  26.   # F1-Measure
  27.   F1 <- 2*TPR*PRE/(TPR+PRE)
  28.  
  29.   return(c(TPR, PRE, TNR, ACC, BCR, F1))
  30. }
  31.  
  32. Perf.Table <- matrix(0, nrow = 8, ncol = 6)
  33. rownames(Perf.Table) <- c("MLR", "CART", "ANN","Bagging CART","Random Forests", "Bagging ANN", "AdaBoost", "GBM" )
  34. colnames(Perf.Table) <- c("TPR", "Precision", "TNR", "Accuracy", "BCR", "F1-Measure")
  35.  
  36.  
  37. # [사전 작업 사항] numeric 이 아닌 변수들에 대해 1-of-C coding
  38.  
  39.  
  40. id.idx <- 1 # 일련 번호는 제외 해줌
  41. target.idx <- 40 # 타겟 변수
  42.  
  43. earthquate.input <- earthquate_data[,c(-id.idx,-target.idx)] # id와 타겟변수를 제외해줌
  44. earthquate.target <- as.factor(earthquate_data[,target.idx]) # 종속 변수
  45.  
  46. str(earthquate.input) # 각 변수의 타입을 확인 => 명목형 변수를 변환
  47.  
  48. factor_val <- c(8,9,10,11,12,13,14,26) # factor형 변수들의 인덱스
  49.  
  50. # 명목형 변수 하나하나 반복문을 돌면서 일단 추가해주고, 마지막에 기존의 명목형 변수 열을 삭제 해줌
  51. for(i in factor_val){
  52.   earthquate.input <- cbind(earthquate.input,class.ind(earthquate.input[,i])) # 일단 다 추가 해 줌
  53. }
  54. earthquate.input <- earthquate.input[,-factor_val] # 기존 명목형 변수 열을 삭제
  55. earthquate.input.scaled <- scale(earthquate.input, center = TRUE, scale = TRUE)
  56.  
  57. earthquate.data.scaled <- data.frame(earthquate.input.scaled, earthquate.target) # 데이터를 합쳐줌
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement