File size: 2,354 Bytes
9e55601 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import pandas as pd
import numpy as np
from imblearn.over_sampling import SMOTENC
# 지역별 데이터 파일 경로
regions = ['incheon', 'seoul','busan', 'daegu', 'daejeon', 'gwangju']
file_paths = [f'../../data/data_for_modeling/{region}_train.csv' for region in regions]
output_paths = [f'../../data/data_oversampled/smote/smote_2_{region}.csv' for region in regions]
# 지역별 처리
for file_path, output_path in zip(file_paths, output_paths):
# 데이터 로드
data = pd.read_csv(file_path, index_col=0)
data= data.loc[data['year'].isin([2018,2020]),:]
data['cloudcover'] = data['cloudcover'].astype('int')
data['lm_cloudcover'] = data['lm_cloudcover'].astype('int')
X = data.drop(columns=['multi_class', 'binary_class'])
y = data['multi_class']
# 불필요한 열 제거
X.drop(columns=['ground_temp - temp_C', 'hour_sin', 'hour_cos', 'month_sin', 'month_cos'], inplace=True)
# SMOTENC에서 사용할 범주형 변수 열 번호 설정
categorical_features_indices = [i for i, dtype in enumerate(X.dtypes) if dtype != 'float64']
# sampling_strategy 설정
count_class_2 = (y == 2).sum()
sampling_strategy = {
0: int(np.ceil(count_class_2 / 1000) * 500),
1: int(np.ceil(count_class_2 / 1000) * 500),
2: count_class_2
}
# SMOTENC 적용
smotenc = SMOTENC(categorical_features=categorical_features_indices, sampling_strategy=sampling_strategy, random_state=42)
X_resampled, y_resampled = smotenc.fit_resample(X, y)
# Resampled 데이터 생성
lerp_data = X_resampled.copy()
lerp_data['multi_class'] = y_resampled
# 제거변수 복구
lerp_data['binary_class'] = lerp_data['multi_class'].apply(lambda x: 0 if x == 2 else 1)
lerp_data['hour_sin'] = np.sin(2 * np.pi * lerp_data['hour'] / 24)
lerp_data['hour_cos'] = np.cos(2 * np.pi * lerp_data['hour'] / 24)
lerp_data['month_sin'] = np.sin(2 * np.pi * lerp_data['month'] / 12)
lerp_data['month_cos'] = np.cos(2 * np.pi * lerp_data['month'] / 12)
lerp_data['ground_temp - temp_C'] = lerp_data['groundtemp'] - lerp_data['temp_C']
# 결과 저장
lerp_data.to_csv(output_path, index = False)
print(len(lerp_data[lerp_data['multi_class']==0]),'|',len(lerp_data[lerp_data['multi_class']==1]),'|',len(lerp_data[lerp_data['multi_class']==2])) |