Missing data (결측자료)
실세계 데이터는 다양한 원인 때문에 누락 데이터를 포함하고 있다. 데이터에서 None, NaN, 빈칸으로 표시되는 것들이 누락 데이터이다. 이러한 누락된 값이 많은 데이터셋으로 머신러닝 모델을 학습시키면 모델의 품질에 큰 영향을 미친다. Scikit-learn Estimator 같은 일부 알고리즘은 모든 값이 의미 있는 값을 가지고 있다고 가정하기 때문이다.
Missing value는 다음과 같은 3가지 타입이 있다.
- Missing completely at random (MCAR)
- Missing at random (MAR)
- Nonignorable
Missing completely at random (MCAR)
완전 무작위 결측이라고 한다. 이는 결측값의 발생이 다른 변수와 상관이 없는 경우를 의미한다. 예를 들어, 전산 오류나 통신문제 등으로 데이터 누락 등이 있다.
Missing at random (MAR)
무작위 결측이라고 한다. 결측값의 발생이 특정 변수와 관련이 있으나 얻고자 하는 결과와는 상관이 없을 경우를 의미한다. ex) In census surveys, a respondent might skip an extended response question because relevant information was inputted in a previous question, or alternatively, they fail to complete the census survey due to low levels of language proficiency. the reason why the value is missing is linked to another variable in the dataset and not due directly to the value itself.
Nonignorable
비무작위 결측, Missing Not At Random (MNAR)이라고 한다. 이는 결측값 발생이 다른 변수와 상관이 있는 경우를 의미한다.
ex) Respondents with a criminal record may decline to supply information to certain questions due to feelings of sensitivity towards that question
isnull().sum()
다양한 이유로 인해서 생기는 결측값은 분석 오류가 발생시키거나 혹은 왜곡시킬 위험이 있다. 따라서 분석할 DataFrame을 생성했으면 결측값(missing value)이 있는지 여부에 대해서 꼭 확인하고 조치하여야 한다. Python pandas에서는 결측값을 'NaN' 으로 표기하며, 'None'도 결측값으로 인식한다. isnull() 메소드는 관측치가 결측이면 True, 결측이 아니면 False의 boollean 값을 반환합니다. df.isnull().sum()은 칼럼별 결측값 개수를 구한다.
fillna
fillna 메서드는 DataFrame에서 결측값을 원하는 값으로 변경하는 메서드이다.
dropna
dropna function은 DataFrame에서 NaN value가 존재하는 행(row) 또는 열(column)을 제거해준다.
df.dropna(axis=1)
결측값이 들어있는 행 전체(axis = 0) 삭제 (default), 결측값이 들어있는 열 전체(axis=1) 삭제
df.dropna(how='all')
row 또는 column에 있는 모든 값이 NaN이어야 drop, how='any' 는 row 또는 column에 NaN값이 1개만 있어도 drop (default)
df.dropna(thresh=2)
thresh는 threshold(임계값)을 설정할 수 있는데, NaN이 2개 이상 있는 것에 대해서만 삭제해버린다는 의미이다.
df.dropna(subset=['name','toy'])
subset에 리스트 형태의 값을 입력함으로써 결측치 제거를 수행할 레이블을 지정할 수 있다.
name과 toy에 대해서 결측치가 있는 경우만 제거되었다.
df.dropna(inplace=True)
True -> dropna가 적용된 DataFrame 자체에 dropna를 적용
False -> dropna가 적용된 DataFrame는 그대로 두고 dropna를 적용한 DataFrame을 return
원본에 대해서도 결측치 제거가 수행된다.
'인공지능 > Machine Learning' 카테고리의 다른 글
[ML] k-Means Clustering / Scree plot (0) | 2022.10.14 |
---|---|
[ML] Dimension Reduction / Correlation vs. Covariance (0) | 2022.10.14 |
[ML] Data Scrubbing / One-hot Encoding (0) | 2022.10.13 |
[ML] EXPLORATORY DATA ANALYSIS (EDA, 탐색적 자료 분석) / 데이터 살펴보기 (0) | 2022.10.13 |
[ML] 머신러닝 라이브러리 (0) | 2022.10.13 |
댓글