데이터 과학자와 머신러닝 실무자들의 온라인 커뮤니티
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
data = pd.read_csv('')
# 상위 데이터 확인하기
data.head(10)
# 데이터 행과 열의 개수 확인
data.shape
# 데이터 정보 확인
data.info()
# 데이터 요약
data.describe()
data.isnull().sum()
# object 타입인 timestmap 칼럼을 datetime 타입으로 변환
data['timestamp'] = pd.to_datetime(data['timestamp'])
# 칼럼 추가
data['year'] = data['timestamp].dt.year
data['season'].value_counts(ascending=False)
plt.figure(figsize=(가로,세로)) # 그래프 크기
sns.boxplot(x='범주',y='수치',data=데이터프레임) # 박스플롯 생성
plt.xlabel('season') # x축 레이블 설정
plt.ylabel('cnt') # y축 레이블 설정
IQR : 사분위수를 이용
def remove_outliers_iqr(df,column):
Q1 = df[column].quantile(0.25)
Q3 = df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
return df[(df[column] >= lower_bound) & (df[column] <= upper_bound)]
df_clean = remove_outliers_iqr(df, '칼럼명')
Z-score : 평균과 표준편차를 이용
def remove_outliers_zscore(df, column, threshold=3):
z_scores = np.abs((df[column] - df[column].mean()) / df[column].std())
return df[z_scores < threshold]
df = pd.read_csv('your_data.csv')
df_clean = remove_outliers_zscore(df, 'your_column_name')