前提・実現したいこと
Python3で時系列データをARモデルで予測するプログラムを書いています。
実現したいプログラムとしては、testデータとpredictデータ(予測データ)をplotしようとしています。
発生している問題・エラーメッセージ
気温データ、自己相関関数は画像が出力できましたが、最後のplotできませんでした。 ValueError: view limit minimum -36869.1 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
該当のソースコード
Python3
1 2import pandas as pd 3import numpy as np 4import matplotlib.pyplot as plt 5from matplotlib.ticker import ScalarFormatter 6from statsmodels.tsa import stattools as st 7from pandas.plotting import register_matplotlib_converters 8register_matplotlib_converters() 9 10df = pd.read_csv('data_.csv', encoding="shift-jis") 11df['日付'] = pd.to_datetime(df['日付'], format = "%Y/%m/%d") 12df = df.reset_index() 13df_r = df.set_index('index') 14# (3652, 5) 15train = np.array(df_r['気温'].loc[:701]) 16test = np.array(df_r['気温'].loc[702:]) 17train_x = df_r['日付'].loc[:701] 18test_x = df_r['日付'].loc[702:] 19print(train) 20print(test) 21 22plt.plot(df_r['日付'],df_r['気温']) 23plt.savefig('./actual_kion18.png') 24 25 26# 自己相関(Autocorrelation)プロット, 青いエリアは95%の信頼区間 27import statsmodels.api as sm 28fig = plt.figure(figsize=(12,8)) 29ax1 = fig.add_subplot(211) 30fig = sm.graphics.tsa.plot_acf(train, lags=20, ax=ax1) 31 32# 偏自己相関(Partial Autocorrelation)プロット 33ax2 = fig.add_subplot(212) 34fig = sm.graphics.tsa.plot_pacf(test, lags=20, ax=ax2) 35fig.savefig('./pacf_kion.png') 36 37 38# ARMAモデルの次数を決める 39st.arma_order_select_ic(train, ic='aic', trend = 'nc') 40#arma = st.arma_order_select_ic(train, ic='aic', trend = 'nc') 41#print(arma) 42 43 44from statsmodels.tsa.ar_model import AR 45ar = AR(train).fit(maxlag=10,ic='aic') 46#予測 47ar_predict = ar.predict(start='2019/11/18 5:00:00',end='2019/11/19 0:00:00') 48 49#プロット 50plt.plot(test_x, ar_predict) 51plt.plot(test_x, test) 52plt.savefig('./ar_lag2_kion18.png')
試したこと
ar_predict = ar.predict(start=702,end=767)
の部分を
ar_predict = ar.predict(start='2019/11/18 5:00:00',end='2019/11/19 0:00:00')
としたところ、
raise KeyError('The start
argument could not be matched to a'
KeyError: 'The start
argument could not be matched to a location related to the index of the data.'
とエラーが出ました。
補足情報(FW/ツールのバージョンなど)
参考にしたサイトを下記に示します。
リンク内容
該当のcsvファイルの先頭と末尾を図で添付します。
あなたの回答
tips
プレビュー