Download Analysis_code/make_oversample_data/smote_sample_1.py from bong9513/Visibility_Nowcasting: direct link, hf CLI and curl.
- Browser
- Download file 2.35 kB
-
https://huggingface.co/bong9513/Visibility_Nowcasting/resolve/refs%2Fpr%2F1/Analysis_code/make_oversample_data/smote_sample_1.py
- Command line
-
hf download hf://bong9513/Visibility_Nowcasting@refs/pr/1/Analysis_code/make_oversample_data/smote_sample_1.py
-
curl -L -o smote_sample_1.py https://huggingface.co/bong9513/Visibility_Nowcasting/resolve/refs%2Fpr%2F1/Analysis_code/make_oversample_data/smote_sample_1.py
2.35 kB
| 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_1_{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,2019]),:] | |
| 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])) |