Last active
April 4, 2020 13:47
-
-
Save woodongk/298952838b05458ca6706f0ce7749102 to your computer and use it in GitHub Desktop.
데이터프레임에서 이상치 검출하기 - IQR 사용
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#출처 - 파이썬을 이용한 머신러닝, 딥러닝 실전 개발 입문 | |
import np | |
def get_outlier(df=None,column=None,weight=1.5): | |
'''인자로 Dataframe과 이상치를 검출할 칼럼을 입력받는다. | |
iqr에 1.5 곱해서 이에 기반하여 이상치를 구해 해당 이상치가 있는 index 반환 | |
''' | |
column_x = df[column] | |
# 1/4 분위와 3/4 분위 지점을 np.percentile로 구함 | |
quantile_25 = np.percentile(column_x.values,25) | |
quantile_75 = np.percentile(column_x.values,75) | |
# IQR을 구하고 IQR에 1.5를 곱해 최댓값과 최솟값 지점 구함. | |
iqr = quantile_75 - quantile_25 | |
iqr_weight = iqr * weight | |
lowest_val = quantile_25 - iqr_weight | |
highest_val = quantile_75 + iqr_weight | |
# 최댓값보다 크거나, 최솟값보다 작은 값을 이상치 데이터로 설정하고 Dataframe index 반환 | |
outlier_index = column_x[(column_x < lowest_val) | (column_x > highest_val)].index | |
return outlier_index |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment