# Auto ARIMA
# 자동으로 SARIMA 분석 수행

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('../data/arima_data.csv', names = ['day', 'price'])
data['day'] = pd.to_datetime(data['day'], format = '%Y-%m-%d')
data.set_index('day', inplace = True)
s_data = data
# 패키지 설치
! pip install pmdarima

train_len = int(len(s_data)*0.8)
training = s_data[:train_len]
test = s_data.drop(training.index)

from pmdarima import auto_arima
# 1-5까지 그리드서치
auto_model = auto_arima(training, start_P = 1, D = 1, Q = 1, max_P = 5, max_D = 5, max_Q = 5, m=12,
	seasonal = True, information_criterion = 'aic', trace = True, stepwise = True)
    
auto_model.summary()
prediction = pd.DataFrame(auto_model.predict(n_periods=len(test)), index = test.index)
prediction.columns = ['predicted_price']

plt.figure(figsize = (10, 6))
plt.plot(training, label = 'Train')
plt.plot(prediction, label = 'Prediction')
plt.plot(test, label = 'Test')
plt.legend(loc = 'upper left')
plt.show()
from sklearn.metrics import r2_score, mean_squared_error
test['predicted_price'] = prediction
r2_score(test['price'], test['prediced_price'])

import numpy as np
import math
rmse = np.sqrt(mean_squared_error(test['price'], test['predicted_price']))
rmse

len(test['price']

# 결과해석
# 시계열 데이터 분석 결과, 1년 주기의 계절성을 가지고 있음을 확인
# 우상향하는 추세를 가지고 있음
# 추세를 가지고 있기에 1차 차분
# 계절성을 가지고 있으므로 예측 모형은 SARIMA를 사용하였음

'ADP > 실기' 카테고리의 다른 글

의사결정나무(분류, 회귀)  (0) 2024.01.29
회귀분석(LinearRegression, Ridge, Lasso, Elasticnet)  (0) 2024.01.29
정상성 검정 및 확인  (1) 2024.01.28
시계열 분해, ARIMA 모델  (1) 2024.01.28
연관분석  (0) 2024.01.28

+ Recent posts