首页 > 解决方案 > 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])

标签: pythontime-seriesyahoo-financearima

解决方案


推荐阅读