air_2021, air_2022
weather_2021, weather_2022
air_21['측정일시'] -= 1
air_21['time'] = pd.to_datetime(air_21['측정일시'].astype(str), format = '%Y%m%d%H')
df_21 = air_21.merge(weather_21, on = 'time')
# 병합 결과 = 데이터프레임_1.merge(데이터프레임_2, on = 기준 컬럼)
df_21.set_index('time', drop = True, inplace = True)
# inter: 'interpolate'를 적용할 컬럼명을 담은 배열
for c in inter:
df_21[c].interpolate(method = 'linear', inplace = True)
df_22[c].interpolate(method = 'linear', inplace = True)
# fill: 'fillna'를 적용할 컬럼명을 담은 배열
for c in fill:
df_21[c].fillna(0, inplace = True)
df_22[c].fillna(0, inplace = True)
df_21['month'] = df_21.index.month
df_21['day'] = df_21.index.day
df_21['hour'] = df_21.index.hour
df_21['month'] = df_21['time'].dt.month
df_21['day'] = df_21['time'].dt.day
df_21['hour'] = df_21['time'].dt.hour
df_21['PM10_1'] = df_21['PM10'].shift(-1)
df_21['PM10_lag1'] = df_21['PM10'].shift(24)
train_x = df_21.drop('PM10_1', axis = 1)
train_y = df_21.loc[:, 'PM10_1']
test_x = df_22.drop('PM10_1', axis = 1)
test_y = df_22.loc[:, 'PM10_1']
train_x.to_csv('train_x.csv')
train_y.to_csv('train_y.csv')
test_x.to_csv('test_x.csv')
test_y.to_csv('test_y.csv')
Linear Regression, RandomForest, Gradient Boost 등의 모델을 만들어 성능을 평가해보았음
RandomForestRegressor 모델에서 feature importance
GradientBoostingRegressor 모델에서 feature importance
Linear Regression에서 성능이 좋지 않아 'Elastic Net'을 시도해보았고, 성능이 개선되었음을 확인함
model = ElasticNet(alpha=0.01, l1_ratio=0.5)
model.fit(train_x, train_y)
y_pred_EN = model.predict(test_x)
print(mse(test_y, y_pred_EN)**(0.5))
print(r2_score(test_y, y_pred_EN))