데이터 셋 (Dataset) 다루기

sklearn.dataset 안에는 빌트인 (built-in) 데이터 셋들이 존재합니다. 물론 튜토리얼 진행을 위한 수준이므로, 규모가 크지는 않습니다 (Toy Dataset 이라고도 불리웁니다.)

그렇지만, mldata.org 데이터 셋은 조금 더 규모가 큰 데이터 셋을 제공하며, 온라인에서 동적으로 다운로드를 받을 수 있습니다.

이번 튜토리얼에서는 Built-in 데이터 셋을 활용하는 방법에 대해서 알아보도록 하겠습니다.

import warnings

# 경고 메시지 출력 표기 생략
warnings.filterwarnings('ignore')

빌트인 (Built-in) 데이터셋 활용

데이터 셋의 종류

  • load_boston: 보스톤 집값 데이터
  • load_iris: 아이리스 붓꽃 데이터
  • load_diabetes: 당뇨병 환자 데이터
  • load_digits: 손글씨 데이터
  • load_linnerud: multi-output regression 용 데이터
  • load_wine: 와인 데이터
  • load_breast_cancer: 위스콘신 유방암 환자 데이터

데이터 셋 조회

빌트인 데이터셋은 sklearn.utils.Bunch 라는 자료구조를 활용합니다.

key-value 형식으로 구성되어 있으며, 사전(dict)형 타입과 유사한 구조를 가지고 있습니다.

공통 key는 다음과 같습니다.

  • data: 샘플 데이터, Numpy 배열로 이루어져 있습니다.
  • target: Label 데이터, Numpy 배열로 이루어져 있습니다.
  • feature_names: Feature 데이터의 이름
  • target_names: Label 데이터의 이름
  • DESCR: 데이터 셋의 설명
  • filename: 데이터 셋의 파일 저장 위치 (csv)

간단한 실습으로 빌트인 데이터셋의 활용법에 대하여 알아보겠습니다.

iris 붓꽃 데이터 로드하기

from IPython.display import Image

Image(url='https://user-images.githubusercontent.com/15958325/56006707-f69f3680-5d10-11e9-8609-25ba5034607e.png')
# iris 붓꽃 데이터 로드
from sklearn.datasets import load_iris

load_iris로 데이터 셋을 불러와서 iris 변수에 저장합니다.

# 코드를 입력해 주세요
iris = 

변수를 출력해 보면 다음과 같이 key-value 로 이루어진 데이터셋이 로드됩니다.

iris

Feature 데이터 (X)

feature 데이터 값 조회하기

feature data는 data 키로 접근하여 가져올 수 있습니다.

features 변수에 data를 대입해 주세요.

# 코드를 입력해 주세요
features = 

5개만 출력해 본다면 다음과 같은 모양새를 띄고 있습니다.

features[:5]

feature data 에 대한 이름은 feature_names 로 가져올 수 있습니다.

iris 데이터의 경우 총 4개의 feature를 가지고 있음을 확인할 수 있습니다.

[참고] - sepal: 꽃받침 - petal: 꽃잎

feature_names = iris['feature_names']
feature_names

Label 데이터 (Y)

label data는 target 키로 접근하여 가져올 수 있습니다.

labels = iris['target']
labels

feature data와 마찬가지로, label 데이터도 target_names라는 키로 target에 대한 클래쓰 이름을 확인해 볼 수 있습니다.

데이터 셋을 DataFrame으로 변환

import pandas as pd

첫번째로 datafeature_names 키로 가져온 데이터를 활용하여 데이터 프레임을 만들어 줍니다.

# 코드를 입력해 주세요
df = 
df.head()

[출력 결과]

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

그런 다음 target 데이터를 새로운 컬럼을 만들어 추가해 줍니다. 여기서 target 데이터의 column 명 임의로 지정해 주면 됩니다.

# 코드를 입력해 주세요
df['target'] = 
df.head()

로드한 DataFrame 시각화

import matplotlib.pyplot as plt
import seaborn as sns

Sepal (꽃받침)의 길이 넓이에 따른 꽃의 종류가 어떻게 다르게 나오는지 살펴보겠습니다.

plt.figure(figsize=(10, 7))
sns.scatterplot(df.iloc[:, 0], df.iloc[:, 1], hue=df['target'], palette='muted')
plt.title('Sepal', fontsize=17)
plt.show()

이번에는, Petal (꽃잎)의 길이 넓이에 따른 꽃의 종류가 어떻게 다르게 나오는지 살펴보겠습니다.

plt.figure(figsize=(10, 7))
sns.scatterplot(df.iloc[:, 2], df.iloc[:, 3], hue=df['target'], palette='muted')
plt.title('Petal', fontsize=17)
plt.show()

데이터 분할 (train_test_split)

기계학습에서 데이터 분할을 중요한 전처리 과정입니다.

sklearn.model_selectiontrain_test_split은 클래스 이름 그대로 학습과 검증 (혹은 테스트) 셋을 나누어 주는 역할을 합니다.

학습 (Train) / 검증 (Validation or Test) 세트로 나누며, 검증 세트로 과대 적합여부를 모니터링 할 수 있습니다.

또한, 검증 세트를 활용하여 모델의 성능 평가를 진행할 수 있습니다.

샘플 데이터 확인

df.head()

feature(x), label(y) 데이터를 분할 합니다.

from sklearn.model_selection import train_test_split

target 변수를 제외한 나머지 컬럼은 x변수에 대입합니다.

x = df.iloc[:, :4]
x.head()

y변수에 df['target']을 대입합니다.

y = df['target']
y.head()

주요 hyperparameter

  • test_size: validation set에 할당할 비율 (20% -> 0.2), 기본값 0.25
  • stratify: 분할된 샘플의 class 갯수가 동일한 비율로 유지
  • random_state: 랜덤 시드값
  • shuffle: 셔플 옵션, 기본값 True
x_train, x_test, y_train, y_test = train_test_split(x, y, stratify=y, test_size=0.2, random_state=123)

원본 x의 shape

x.shape

분할된 x의 shape

x_train.shape, x_test.shape

원본 y의 shape

y.shape

분할된 y의 shape

y_train.shape, y_test.shape

연습문제

wine 데이터셋을 불러와 로드 하고 데이터셋을 분할 합니다.

# 와인 데이터 로드
from sklearn.datasets import load_wine

wine 변수에 load_wine() 결과 대입

# 코드를 입력해 주세요
wine = 
  • df 변수에 데이터 프레임 생성
  • column 지정 및 target 컬럼 생성
# 코드를 입력해 주세요
df = 

df.head()

[출력 결과]

alcohol malic_acid ash alcalinity_of_ash magnesium total_phenols flavanoids nonflavanoid_phenols proanthocyanins color_intensity hue od280/od315_of_diluted_wines proline target
0 14.23 1.71 2.43 15.6 127.0 2.80 3.06 0.28 2.29 5.64 1.04 3.92 1065.0 0
1 13.20 1.78 2.14 11.2 100.0 2.65 2.76 0.26 1.28 4.38 1.05 3.40 1050.0 0
2 13.16 2.36 2.67 18.6 101.0 2.80 3.24 0.30 2.81 5.68 1.03 3.17 1185.0 0
3 14.37 1.95 2.50 16.8 113.0 3.85 3.49 0.24 2.18 7.80 0.86 3.45 1480.0 0
4 13.24 2.59 2.87 21.0 118.0 2.80 2.69 0.39 1.82 4.32 1.04 2.93 735.0 0

생성한 데이터프레임 shape 확인

# 코드를 입력해 주세요

[출력 결과]

(178, 14)

target의 분포 확인

# 코드를 입력해 주세요

[출력 결과]

1    71
0    59
2    48
Name: target, dtype: int64

countplot() 으로 target의 분포 확인

# 코드를 입력해 주세요

[출력 결과]

  • x_train, x_test, y_train, y_test 에 분할
  • test_size=0.2
  • random_state=123
  • stratify 옵션 지정

[출력 결과]

# 코드를 입력해 주세요
x_train, x_test, y_train, y_test = 
# 코드검증
x_train.shape, x_test.shape

[출력 결과]

((142, 13), (36, 13))

y_train의 countplot 확인

# 코드를 입력해 주세요

[출력 결과]

y_test countplot 확인

[출력 결과]

# 코드를 입력해 주세요

[출력 결과]

[출력 결과]