이 포스팅은 Kaggle::Titanic 시리즈 10 편 중 3 번째 글 입니다.

  • Part 1 - 01: 문제 정의
  • Part 2 - 02: 데이터 수집
  • Part 3 - This Post
  • Part 4 - 04: 데이터 미리보기
  • Part 5 - 05: 데이터 정제
  • Part 6 - 06: EDA (Exploratory Data Analysis)
  • Part 7 - 07: 모델링
  • Part 8 - 08: 모델링 평가하기
  • Part 9 - 09: Hyper Parameter Tuning
  • Part 10 - 10: Ensemble
▼ 목록 보기

Kaggle에 있는 Titanic Prediction 문제의 라이브러리를 로드한다.

데이터 정제

제공된 데이터 셋을 사용하기 편하게 정제한다.

라이브러리 사용

사용하는 라이브러리를 확인한다.

기본 라이브러리

# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python

#load packages
import sys # 시스템 파라미터에 접근할 수 있게 도와준다.
print("Python version: {}". format(sys.version))

import pandas as pd # 데이터 정제에 도움을 주는 라이브러리
print("pandas version: {}". format(pd.__version__))

import matplotlib # 매트랩에서 사용하는 시각화 도구를 사용할 수 있게 도와주는 시각화 도구
print("matplotlib version: {}". format(matplotlib.__version__))

import numpy as np # 행렬 계산을 위해 필요한 라이브러리
print("NumPy version: {}". format(np.__version__))

import scipy as sp # 수학 관련 함수가 내장된 라이브러리
print("SciPy version: {}". format(sp.__version__))

import IPython
from IPython import display # 주피터 노트북에서 예쁘게 시각화 해주는 도구
print("IPython version: {}". format(IPython.__version__))

import sklearn # 각종 통계 도구와 머신 러닝 알고리즘이 내장되어 있는 라이브러리
print("scikit-learn version: {}". format(sklearn.__version__))

# 파이썬 내장 라이브러리
import random
import time


# Jupyter Notebook 이나 ipython 을 사용하다보면 향후 버전이 올라갈 때 변경될 사항 등을 알려주는 경고 메시지(warning message)를 뜨지 않게 해준다.
import warnings
warnings.filterwarnings('ignore')
print('-'*25)


# input data 파일은 "../input"에 있다.
# 아래 코드를 수행하게 되면, "../input/"에서 리눅스 명령어 "ls"를 수행한 결과를 보여주게 된다. 이 때 나온 binary code를 "utf8"로 디코딩해서 보여준다.
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))

# Any results you write to the current directory are saved as output.

subprocess 모듈에 대해 궁금하다면 다음의 글을 읽어보자.
subprocess 모듈에 관하여

데이터 모델링 라이브러리

# 일반적인 모델링 라이브러리
from sklearn import svm, tree, linear_model, neighbors, naive_bayes, ensemble, discriminant_analysis, gaussian_process
from xgboost import XGBClassifier

# 모델링 시 헬퍼 함수들
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import feature_selection
from sklearn import model_selection
from sklearn import metrics

# 시각화 도구
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import seaborn as sns
from pandas.tools.plotting import scatter_matrix

# 시각화 도구 default 세팅
%matplotlib inline # 주피터 노트북에서 plot 결과를 볼 수 있게 해준다.
mpl.style.use('ggplot') # matplotlib에서 plot되는 결과를 선택할 수 있다.
sns.set_style('white') # seaborn에서 사용할 style을 설정할 수 있다.
pylab.rcParams['figure.figsize'] = 12,8 # plot의 크기와 선 등의 기본 값을 설정할 수 있다.

matplotlib에서 style 바꾸는 법

이 부분을 바꾸면 전체적인 그래프의 색등이 바뀐다.

print(plt.style.available)
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']
  • 사용법
plt.style.use(['fivethirtyeight'])

seaborn에서 스타일 바꾸는 법

이 부분을 바꾸면 뒤의 배경과 같은 부분을 바꿀 수 있다. darkgrid, whitegrid, dark, white, 그리고 ticks 스타일을 제공한다.

  • 사용법
sns.set_style('darkgrid')

seaborn 시각화

Reference

kaggle Notebook
subprocess 모듈에 관하여
seaborn 시각화