4 Managing data

Download Report

Transcript 4 Managing data

고급컴퓨터알고리듬
전자전기컴퓨터공학부
홍기주

US Census data
https://github.com/WinVector/zmPDSwR/tree/master/Custdata
에서 ‘exampleData.rData’ 다운로드
R에서 load("exampleData.rData") 를 이용하여 데이터 load

To drop or not to drop?
1,000개 중 결측치가 56개 (6%)
‘housing.type’, ‘recent.move’,’num.vehicles’의 동일한 row에서 결
측치가 발생하므로 drop해도 될만하다

To drop or not to drop?
‘is.employed’처럼 결측치가 너무 많다면..

Missing data in categorical variables
NA를 remapping
대부분의 R function에서 NA가 포함된 row를 drop시키므로 NA를
level로 remapping함

Missing data in categorical variables
왜 ‘is.employed’에만 결측치가 많을까? 결측치는 무슨 의미인가?
같은 row의 데이터를 보면서 유추
-> 대부분의 경우 주부, 학생,은퇴자 등 active workforce가 아님

Missing values in numeric data
‘Income’의 많은 데이터가 결측치라면..
소득이 예측 모델을 만드는 데 중요한 역할을 한다면?

When values are missing randomly
데이터 수집이 random하게 잘못된 것이라면..(faulty sensor)
‘Income’의 평균을 결측치에 대입
(방법은 쉬우나 소득이 과대/과소 측정될 수 있음)
다른 방법론은 R in Action, Second Edition (Robert Kabacoff, 2014, http://mng.bz/ybS4) 참고

When values are missing systematically
실제로 소득이 없어서 결측치인 경우라면..(not in active workforce)

When values are missing systematically
결측치를 0으로 변경
단, 변경 전에 ‘Income’에서 원래 0인 경우도 있으므로
missingIncome 변수에 NA의 위치를 저장함(masking variable)
이해가 쉽도록
데이터 변경
주(state)마다 cost of
living이 다르다면
nomalize하는 것이
더욱 의미있음

Converting continuous variables to discrete
Discrete가 continuous에 비해 이해하기 쉽다.
(소득 $20,000 이하 고객, 25세 이상 60세 이하 고객 등)
Linear한 관계가 아닐 때, continuous를 discretizing하는 것이 좋음
Regression을 활용

Converting continuous variables to discrete
소득 $20,000 이상이면 TRUE, 아니면 FALSE
간단하게 cut()을 사용하여 discretizing 하는 것도 가능

Normalization and rescaling
일반적인 고객에 비해 얼마나 어리거나 늙었는가?
population1에서는 35살
약간 어린 편이지만,
Population2에서는 많이
어린 편에 속한다.

Normalization and rescaling
평균과 표준편차를 이용하여 rescaling
데이터 분포가 대략적으로 대칭일 경우 더욱 좋음

Log transformations for skewed and wide
distributions
Monetary amounts – 소득,고객 수,구매량 등은 대부분 skewed 함
Monetary amounts의 경우
로그의 밑을 10으로 해서
변환하는 것이 읽기 좋음
($100,$1000,$10,000…)

Log transformations for skewed and wide
distributions
로그 변환은 Non-negative일 경우에만 동작
대안 -> signed logarithm

Creating a sample group column
runif함수를 이용한 random sampling
sample함수를 통해서도 random sampling가능하지만
reproducible sampling이 어려움

Record grouping
단일 row가 아니라 그룹을 지어서 sampling하고 싶다면..
household ID와 customer ID
를 묶어서 하나의 unique ID로
만들고 sampling

Record grouping
Household당 하나의 ramdom number를 부여 받도록 병합




결측치의 비중이 얼마나 되는지, 무작위인지, 시스템적
인지 판단
의심의 여지가 있다면, 결측치는 시스템적인 걸로 간주
적절한 데이터 변형은 데이터를 더욱 이해하기 쉽게 하
고 모델링하기도 좋음
Normalization과 rescaling은 상대적인 양이 의미있을
때 더욱 중요함
END