Database for Everything

[R 프로그램] Kaggle 타이타닉 생존자 예측(1) - 데이터 탐색 및 병합 본문

R

[R 프로그램] Kaggle 타이타닉 생존자 예측(1) - 데이터 탐색 및 병합

Yeenn 2021. 12. 5. 14:44
728x90

이번 실습에서는 KaggleTitanic 데이터를 활용하여 승객의 생존여부를 예측해보겠다. 

데이터셋은 아래 Kaggle 링크를 타고 들어가면 다운받을 수 있다. 

 

https://www.kaggle.com/c/titanic/data?select=train.csv 

 

Titanic - Machine Learning from Disaster | Kaggle

 

www.kaggle.com

 

※ 아래부터 진행되는 분석은 모두 아래 링크의 R tutorial을 바탕으로 진행되었습니다. 참고바랍니다.

 

https://www.kaggle.com/jrchun1/titanic-tutarial-with-r-basic

 

Titanic tutarial with R Basic

Explore and run machine learning code with Kaggle Notebooks | Using data from Titanic - Machine Learning from Disaster

www.kaggle.com

 


 

 

본격적인 EDA에 들어가기에 앞서, 데이터를 간단히 탐색해보고 병합하는 과정을 포스팅해보려고 한다. 

 

 

데이터 탐색

#데이터 불러오기
train<-read.csv('train.csv')
test<-read.csv('test.csv')

#Train 데이터 확인
str(train)
'data.frame':	891 obs. of  12 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : int  0 1 1 1 0 0 0 0 1 1 ...
 $ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Name       : chr  "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
 $ Sex        : chr  "male" "female" "female" "female" ...
 $ Age        : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket     : chr  "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin      : chr  "" "C85" "" "C123" ...
 $ Embarked   : chr  "S" "C" "S" "S" ...

str(test)
'data.frame':	418 obs. of  11 variables:
 $ PassengerId: int  892 893 894 895 896 897 898 899 900 901 ...
 $ Pclass     : int  3 3 2 3 3 3 3 2 3 3 ...
 $ Name       : chr  "Kelly, Mr. James" "Wilkes, Mrs. James (Ellen Needs)" "Myles, Mr. Thomas Francis" "Wirz, Mr. Albert" ...
 $ Sex        : chr  "male" "female" "male" "male" ...
 $ Age        : num  34.5 47 62 27 22 14 30 26 18 21 ...
 $ SibSp      : int  0 1 0 0 1 0 0 1 0 2 ...
 $ Parch      : int  0 0 0 0 1 0 0 1 0 0 ...
 $ Ticket     : chr  "330911" "363272" "240276" "315154" ...
 $ Fare       : num  7.83 7 9.69 8.66 12.29 ...
 $ Cabin      : chr  "" "" "" "" ...
 $ Embarked   : chr  "Q" "S" "Q" "S" ...

 

train과 test데이터를 불러와 데이터를 확인해보았다. 

 

차이점은 train set에만 Survived 변수가 존재한다는점! train에서 모형을 학습시킨 후 test set에서 이 생존자변수를 예측하는 것이 이번 실습의 목적이다. 

 

 

 

데이터 병합

#Train, Test 구분 행번호
split_num<-nrow(train)

#Test에 Survived변수 생성
test$Survived <- NA

#Survived 변수 정의
train$Survived <- factor(train$Survived, levels=c(0,1), labels=c('No', 'Yes'))

#데이터 병합
data <- rbind(train,test)
str(data)
'data.frame':	1309 obs. of  12 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : Factor w/ 2 levels "No","Yes": NA NA NA NA NA NA NA NA NA NA ...
 $ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Name       : chr  "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
 $ Sex        : chr  "male" "female" "female" "female" ...
 $ Age        : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket     : chr  "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin      : chr  "" "C85" "" "C123" ...
 $ Embarked   : chr  "S" "C" "S" "S" ...

 

데이터 병합을 위해 test 셋에는 존재하지 않는 Survived 변수를 생성해준 후, 생존 여부 변수를 재정의 해준 뒤, rbind()를 통해 데이터 병합을 진행하였다. 

728x90
Comments