<jupyter_start><jupyter_text>Time Series Forecasting reference site : [Time Series Forecasting](https://h3imdallr.github.io/2017-08-19/arima/) Analysis Gantry CPU using time Series with ARIMA<jupyter_code>%matplotlib inline
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
# EDA: 시계열 데이터 확인하기 
import datetime 
from dateutil.relativedelta import relativedelta

import statsmodels
import statsmodels.api as sm
from statsmodels.tsa.stattools import acf
from statsmodels.tsa.stattools import pacf
from statsmodels.tsa.seasonal import seasonal_decompose
#df = pd.read_csv('../csv_data/datasets_20066_26008_portland-oregon-average-monthly-.csv', index_col='Month')
col_list = ['date', 'cpu']    # 컬럼 선택
df = pd.read_csv('../csv_data/m_data.csv', usecols=col_list, index_col='date')
df.head()
df.dropna(axis=0, inplace=True)
df.columns = ['ridership']
print(df.head(), '\n...\n', df.tail())
df = df.iloc[:-1]
print(df.tail())
df.index = pd.to_datetime(df.index)
type(df.index); 
print(df.head(), '\n...\n', df.tail())
time_window_l = datetime.datetime(2020, 6, 30)
time_window_r = datetime.datetime(2020, 7, 9)
temp_df = df[
    (df.index >= time_window_l)
    & (df.index <= time_window_r)
]
print(temp_df)
temp_df = df[:time_window_l]
print(temp_df)
df['ridership'] = df['ridership'].astype(float)
print(df.dtypes)
df.plot()
decomposition = seasonal_decompose(df['ridership'], period=2)
plt.rcParams['figure.figsize'] = [10,10]
fig = plt.figure()
fig = decomposition.plot()
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
    rolmean = timeseries.rolling(2).mean()
    rolstd = timeseries.rolling(2).std()
    
    fig = plt.figure(figsize=(10, 6 ))
    orig = plt.plot(timeseries, color='blue', label='Original')
    mean = plt.plot(rolmean, color='red', label='Rolling Mean')
    std = plt.plot(rolstd, color='black', label = 'Rolling Std')
    
    plt.legend(loc='best'); plt.title('Rolling Mean & Standard Deviation')
    plt.show()
    
    print('<Results of Dickey-Fuller Test>')
    dftest = adfuller(timeseries, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4],
                         index = ['Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used'])
    for key,value in dftest[4].items():
        dfoutput['Critical Value (%s)'%key] = value
    print(dfoutput)
    
    
test_stationarity(df['ridership'])
df['first_difference'] = df['ridership'] - df['ridership'].shift(1)
test_stationarity(df.first_difference.dropna(inplace=False))
df['seasonal_first_difference'] = df['first_difference'] - df['first_difference'].shift(4)
test_stationarity(df.seasonal_first_difference.dropna(inplace=False))
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(df.seasonal_first_difference.iloc[13:], lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(df.seasonal_first_difference.iloc[13:],lags=40,ax=ax2)
mod = sm.tsa.SARIMAX(df['ridership'],order=(0,1,0), seasonal_order=(1,1,1,12))
results = mod.fit()
print (results.summary())
plt.rcParams['figure.figsize'] = [10,5]
results.plot_diagnostics();
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0);
# 시계열 예측
df['forecast'] = results.predict(start = len(df)-24, end= len(df), dynamic= True)  
df[['ridership', 'forecast']].plot()
df[-24:]
# 24개월 예측
df['forecast'] = results.predict(start = len(df)-24, end= len(df), dynamic= True)  
df[['ridership', 'forecast']].plot()
#start = datetime.datetime.strptime("2020-07-01", "%Y-%m-%d")
## >2020-07-01 00:00:00
#date_list = [start + relativedelta(months=x) for x in range(0,12)]
##> 1982/7/1,8/1, ... 1983/6/1

#future_df = pd.DataFrame(index=date_list, columns= df.columns)
#new_df = pd.concat([df, future_df]) #concatenated  dataframe
## print(new_df.head(),'\n...\n',new_df.tail())
#new_df['forecast'] = results.predict(start = len(df), end = len(df)+11, dynamic= True)  
#new_df[['ridership', 'forecast']].ix[-48:].plot()
#print(df.forecast[-12:])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1786422200198216, 'fraction_numerical': 0.03468780971258672, 'mean_word_length': 5.407936507936508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '126950', 'license': 'Apache-2.0', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1559, 'n_words': 274, 'path': 'gantry-jupyterhub/time_series/pd_arima/pd_arima_cpu.ipynb', 'repo_name': 'jaebongchun/gantry-aiops', 'size': '4036'}