본문 바로가기

자격증/빅데이터 분석기사

[분류] 1. 분류 모델 구현해보기 in R (랜덤포레스트,SVM)

 

 

 

참고 데이터 : https://www.kaggle.com/datasets/tejashvi14/travel-insurance-prediction-data?select=TravelInsurancePrediction.csv 

 

Travel Insurance Prediction Data

Predict Whether A Customer Will Be Interested In Buying Travel Insurance

www.kaggle.com

# 데이터 전처리 
df <- read.csv("sample_data/TravelInsurancePrediction.csv")
df$TravelInsurance <- as.factor(df$TravelInsurance)     # as.factor 를 사용하여 종속변수를 factor형으로 변경

# 범주형 변수 변환   (as.factor)
df$Employment.Type <- as.numeric(as.factor(df$Employment.Type),levels=c('Government Sector','Private Sector/Self Employed'))
df$GraduateOrNot <- as.numeric(as.factor(df$GraduateOrNot),levels=c('Yes','No'))
df$EverTravelledAbroad <- as.numeric(as.factor(df$EverTravelledAbroad),levels=c('Yes','No'))
df$FrequentFlyer <- as.numeric(as.factor(df$FrequentFlyer),levels=c('Yes','No'))


# train, test 분리 (7:3)
idx <- sample(1:nrow(df),0.7*nrow(df))
train <- df[idx,]
test <- df[-idx,-10]

# df_train, df_val 분리 (7:3)
set.seed(13579)
idx <- sample(1:nrow(train), 0.7 *nrow(train))
df_train <- train[idx,]
df_val <- train[-idx,]

# 모델 적합      #  ★ 모델 적합전에 set.seed(13579) 설정하기
set.seed(13579)
m1 <- randomForest(TravelInsurance~., data=df_train, probability=T)   #  probability=T 확률값을 포함하여 출력   # 랜덤포레스트
m2 <- svm(TravelInsurance~.,data=df_train, probability=T)   # SVM

pred1 <- predict(m1,df_val,probability=T,type="response")    # 랜덤포레스트가 분류에 사용될 때 type="response"
pred2 <- predict(m2,df_val,probability=T)

caret::confusionMatrix(df_val$TravelInsurance,pred1)$overall[1]    # Accuracy : 0.85
caret::confusionMatrix(df_val$TravelInsurance,pred2)$overall[1]    # Accuracy : 0.83

 

 

randomForest 모델 "예측" 시에 type="response" 를 지정한다.

 

 

 

 

 

# 최종모델 적합 - randomForest 모델 
model <- randomForest(TravelInsurance~.,data=train ,probability=T)
pred <- predict(model,test,probability=T,type="prob")    # type="prob" 지정
print(head(pred))

result <- data.frame(c(1:nrow(test)),pred[,2])
colnames(result) <- c("index","y_pred")

write.csv(result,"수험번호.csv",row.names=F)
read.csv("수험번호.csv")

 

 

randomForest  "최종" 모델 "예측" 시에 type="prob" 를 지정한다.