python - SARIMAX - 没有支持的索引可用。预测结果将以“start”开始的整数索引给出
问题描述
我想用 SARIMAX 预测苹果的股价。我使用了 Yahoo Finance API 并收集了数据。但是,当我想将模型应用于测试数据时,我遇到了这个错误:
KeyError: 'The `start` argument could not be matched to a location related to the index of the data.'
以下是代码:
# Import the plotting library
import matplotlib.pyplot as plt
# Import the yfinance. If you get module not found error the run !pip install yfiannce from your Jupyter notebook
import yfinance as yf
# Get the data of the stock AAPL
data = yf.download('AAPL','2010-01-01','2021-05-02')
# Plot the close price of the AAPL
data.Close.plot()
plt.show()
train_df = data.loc[:"2020-12-31"]
test_df = data.loc["2021-01-02":]
print("Training Set Shape - ", train_df.shape)
print("Testing Set Shape - ", test_df.shape)
import statsmodels.graphics.tsaplots as sgt
# Fixing plot size
plt.rcParams["figure.figsize"] = 18, 5
# Defining Subplots
fig, axes = plt.subplots(1, 2)
# Plotting ACF and PACF
sgt.plot_acf(train_df.Close[1:], zero = False, lags = 100, ax = axes[0])
sgt.plot_pacf(train_df.Close[1:], zero = False, lags = 100, ax = axes[1])
# Display the Plot
plt.show()
# MODEL FITTING
# Importing Required Package
from statsmodels.tsa.statespace.sarimax import SARIMAX
# Defining the Model
model = SARIMAX(train_df["Close"][1:], order = (10, 0, 1))
# Fitting the Model
model_results = model.fit()
# Printing the model summary
print(model_results.summary())
# FORECASTING
# Building Forecast Object to generate Confidence Intervals
arma_forecast = model_results.get_forecast(len(test_df.index))
arma_predictions_df = arma_forecast.conf_int(alpha = 0.05) # Confidence level of 95%
# Predictions
arma_predictions_df["Predictions"] = model_results.predict(start = test_df.index[0], end = test_df.index[-1])
# RMSE for the Predictions
arma_rmse = np.sqrt(mean_squared_error(test_df["Close"].values, arma_predictions_df["Predictions"]))
我在以下代码行中遇到错误:
model_results.predict(start = test_df.index[0], end = test_df.index[-1])
解决方案
推荐阅读
- c++ - 并非所有具有相同材料的物体看起来都一样 - Open GL/C++
- python-3.x - 使用 Heston 模型的 QuantLib-python 定价障碍选项
- c# - 事件处理程序冒泡
- c - 以下 C 代码显示:格式 '%d' 需要类型为 'int *' 的参数,但参数 7 的类型为 float *
- python - Python Selenium 将 URL 重定向到另一个页面
- c# - 如何引用列表 C#
- javascript - react如何区分具有相同父级的两个数组的相似键?
- c# - 在应用关闭时在 Xamarin 表单中运行事件处理程序,例如接收消息
- tensorflow - 由于内部错误,无法在解释器上运行 tflite 模型
- python - 如何处理棉花糖模式中的 Flask_SQLAlchemy 模型方法?