df
Date Col1 Col2 Col3
2019-11-1 12 13 14
2019-10-1 2 3 1
2019-03-01 2 1 1
and so on
시계열을 분해하여 계절성, 추세, 관찰 및 잔차 값을 얻는 코드 :
df = df.set_index('Date')
from statsmodels.tsa.seasonal import seasonal_decompose
for cols in df.columns.values:
s_dec_multiplicative = seasonal_decompose(df[cols], model =
"multiplicative")
s_dec_multiplicative.plot()
plt.title(cols)
plt.show()
이 그래프의 문제는 그림과 같이 잔여 그래프 위에 제목이 표시된다는 것입니다. 관찰 된 그래프 위에 제목이 필요합니다.
저는 가짜 데이터를 만들었습니다. 나는 이것을 달성하는 더 나은 방법이 있다고 확신합니다.
# imports
import pandas as pd
import numpy as np
from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.pyplot as plt
그런 다음 가짜 데이터를 만듭니다.
# random data
np.random.seed(0)
data = np.random.randint(1, 10, [50, 3])
# dates
start = pd.datetime(2019, 11, 1)
end = start + pd.DateOffset(days=data.shape[0] - 1)
# dummy dataframe
df = pd.DataFrame(
data,
index = pd.date_range(start, end),
columns = ['col_one', 'col_two', 'col_three']
)
그런 다음 다음을 사용하여 제목이있는 그래프를 제어 할 수 있습니다. DecomposeResult
속성에 액세스 할 수 있으며 , attr
아래 에 나와 있습니다.
attr = ['observed', 'trend', 'seasonal', 'resid']
for col in df:
fig, ax = plt.subplots(len(attr), 1, sharex=True)
sd = seasonal_decompose(df[col], model = "multiplicative")
for idx, a in enumerate(attr):
s_attr = getattr(sd, a)
s_attr.plot(ax = ax[idx], title = col if idx == 0 else None)
ax[idx].set(ylabel= a)
추가 ylabels
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다