과정 소개

Python으로 범주형 데이터 다루기

Kasey Jones

Research Data Scientist

"범주형"의 의미는?

범주형

  • 유한한 그룹(범주) 수
  • 범주는 보통 고정/사전에 알려짐(눈 색, 머리 색 등)
  • 질적 데이터라고도 함

수치형

  • 양적 데이터라고도 함
  • 숫자값으로 표현
  • 보통 측정값(키, 몸무게, IQ 등)
Python으로 범주형 데이터 다루기

서열형 vs. 명목형 변수

서열형(Ordinal)

  • 자연스러운 순서가 있는 범주형 변수

설문 응답은 보통 '전혀 동의하지 않음'부터 '매우 동의'까지 범주에 순서가 있습니다.

명목형(Nominal)

  • 자연스러운 순서를 매길 수 없는 범주형 변수

색상 선택(Blue, Green, Red, Yellow, purple)처럼 순서가 없는 범주도 있습니다.

Python으로 범주형 데이터 다루기

첫 번째 데이터셋

adult.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 32561 entries, 0 to 32560
Data columns (total 15 columns):
 #   Column           Non-Null Count  Dtype 
 --  ------           --------------  ----- 
 0   Age              32561 non-null  int64 
 1   Workclass        32561 non-null  object
 2   fnlgwt           32561 non-null  int64 
 3   Education        32561 non-null  object
 4   Education Num    32561 non-null  int64 
 5   Marital Status   32561 non-null  object
...
1 https://www.kaggle.com/uciml/adult-census-income
Python으로 범주형 데이터 다루기

describe 사용하기

adult["Marital Status"].describe()
count                   32561
unique                      7
top        Married-civ-spouse
freq                    14976
Name: Marital Status, dtype: object
Python으로 범주형 데이터 다루기

value_counts 사용하기

adult["Marital Status"].value_counts()
 Married-civ-spouse       14976
 Never-married            10683
 Divorced                  4443
 Separated                 1025
 Widowed                    993
 Married-spouse-absent      418
 Married-AF-spouse           23
Name: Marital Status, dtype: int64
Python으로 범주형 데이터 다루기

normalize와 함께 value_counts 사용

adult["Marital Status"].value_counts(normalize=True)
 Married-civ-spouse       0.459937
 Never-married            0.328092
 Divorced                 0.136452
 Separated                0.031479
 Widowed                  0.030497
 Married-spouse-absent    0.012837
 Married-AF-spouse        0.000706
Name: Marital Status, dtype: float64
Python으로 범주형 데이터 다루기

지식 점검

Python으로 범주형 데이터 다루기

Preparing Video For Download...