【问题标题】:ValueError: Provided exogenous values are not of the appropriate shape for SARIMAX modelValueError:提供的外生值不适合 SARIMAX 模型
【发布时间】:2023-02-01 15:30:00
【问题描述】:

我正在尝试对一些抵押贷款预付款数据运行 SARIMAX 模型。我有一个按抵押队列聚集的数据框列表,并根据时间将它们分成训练集和测试集。然后,我缩放训练集和测试集,并逐步运行 autoarima 函数,为我想在每个队列上运行的 SARIMAX 提供最佳 p、d 和 q 值。我这里有这段代码:

from pmdarima.arima import auto_arima
from statsmodels.tsa.statespace.sarimax import SARIMAX
from sklearn.preprocessing import MinMaxScaler
import re
columns = feature_df.columns.tolist()
cols = [c for c in columns if c not in ['ScheduledBalance', 'SMM', 'SRCDate', 'cluster', 'PredictionDate', 'CprTarget', 'bondsec_code', 'Coupon']]
i = 1
mapes = []
new_dfs = []
for df in dfs[0:1]:
    if df.empty is False:
        df.index = df['SRCDate']
        #print(df.index)
        train = df[df['SRCDate'] <= max(df['SRCDate']) - relativedelta(months = 3)]
        test = df[df['SRCDate'] > max(df['SRCDate']) - relativedelta(months = 3)]

        X_train = train[cols]
        y_train = train['CprTarget']
        X_test = test[cols]
        y_test = test['CprTarget']
        
        scaler = MinMaxScaler(feature_range=(0, 1))
        X_train_scaled = scaler.fit_transform(X_train)
        X_test_scaled = scaler.transform(X_test)
        
        scaler_output = scaler.fit_transform(feature_df[['CprTarget']])
        scaler_output =pd.DataFrame(scaler_output)
        
        train_size=int(len(X_train))
        test_size = int(len(y_test))
        
        print(f"For {df['cluster'].unique()}")
        step_wise = auto_arima(y_train, 
         exogenous= X_train,
         start_p=1, start_q=1, 
         max_p=7, max_q=7, 
         d=1, max_d=7,
         error_action='ignore', 
         suppress_warnings=True, 
         stepwise=True)
        
        
        model = SARIMAX(y_train, 
         exog=X_train,
         order=step_wise.get_params().get('order'),
         enforce_invertibility=False, enforce_stationarity=False)
        
        results = model.fit()
        
        predictions = results.predict(start = train_size, end=train_size+test_size,exog=X_test)
        
        actuals = pd.DataFrame(scaler_output.iloc[train_size:, 0])
                
        predictions=pd.DataFrame(predictions)
        predictions.reset_index(drop=True, inplace=True)
        predictions.index=X_test.index
        predictions['Actual'] = actuals['CprTarget']
        predictions.rename(columns={0:'Pred'}, inplace=True)
        
        predictions['Actual'].plot(figsize=(20,8), legend=True, color='blue')
        predictions['Pred'].plot(legend=True, color='red', figsize=(20,8))
        

这是我正在测试的dataframe。这是我收到的错误的回溯:

ValueError                                Traceback (most recent call last)
File ~\Anaconda3\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py:1775, in MLEModel._validate_out_of_sample_exog(self, exog, out_of_sample)
   1774 try:
-> 1775     exog = exog.reshape(required_exog_shape)
   1776 except ValueError:

ValueError: cannot reshape array of size 620 into shape (74,20)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Input In [128], in <cell line: 10>()
     45 model = SARIMAX(y_train, 
     46  exog=X_train,
     47  order=step_wise.get_params().get('order'),
     48  enforce_invertibility=False, enforce_stationarity=False)
     50 results = model.fit()
---> 52 predictions = results.predict(start = train_size, end=train_size+test_size,exog=X_test)
     54 actuals = pd.DataFrame(scaler_output.iloc[train_size:, 0])
     56 predictions=pd.DataFrame(predictions)

File ~\Anaconda3\lib\site-packages\statsmodels\base\wrapper.py:113, in make_wrapper.<locals>.wrapper(self, *args, **kwargs)
    111     obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
    112 elif how:
--> 113     obj = data.wrap_output(func(results, *args, **kwargs), how)
    114 return obj

File ~\Anaconda3\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py:3403, in MLEResults.predict(self, start, end, dynamic, **kwargs)
   3357 """
   3358 In-sample prediction and out-of-sample forecasting
   3359 
   (...)
   3400     including confidence intervals.
   3401 """
   3402 # Perform the prediction
-> 3403 prediction_results = self.get_prediction(start, end, dynamic, **kwargs)
   3404 return prediction_results.predicted_mean

File ~\Anaconda3\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py:3302, in MLEResults.get_prediction(self, start, end, dynamic, index, exog, extend_model, extend_kwargs, **kwargs)
   3299     extend_model = (self.model.exog is not None or
   3300                     not self.filter_results.time_invariant)
   3301 if out_of_sample and extend_model:
-> 3302     kwargs = self.model._get_extension_time_varying_matrices(
   3303         self.params, exog, out_of_sample, extend_kwargs,
   3304         transformed=True, includes_fixed=True, **kwargs)
   3306 # Make sure the model class has the current parameters
   3307 self.model.update(self.params, transformed=True, includes_fixed=True)

File ~\Anaconda3\lib\site-packages\statsmodels\tsa\statespace\sarimax.py:1718, in SARIMAX._get_extension_time_varying_matrices(self, params, exog, out_of_sample, extend_kwargs, transformed, includes_fixed, **kwargs)
   1708 """
   1709 Get time-varying state space system matrices for extended model
   1710 
   (...)
   1714 special handling in the `simple_differencing=True` case.
   1715 """
   1717 # Get the appropriate exog for the extended sample
-> 1718 exog = self._validate_out_of_sample_exog(exog, out_of_sample)
   1720 # Get the tmp endog, exog
   1721 if self.simple_differencing:

File ~\Anaconda3\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py:1777, in MLEModel._validate_out_of_sample_exog(self, exog, out_of_sample)
   1775         exog = exog.reshape(required_exog_shape)
   1776     except ValueError:
-> 1777         raise ValueError('Provided exogenous values are not of the'
   1778                          ' appropriate shape. Required %s, got %s.'
   1779                          % (str(required_exog_shape),
   1780                             str(exog.shape)))
   1781 elif self.k_exog > 0 and exog is not None:
   1782     exog = None

ValueError: Provided exogenous values are not of the appropriate shape. Required (74, 20), got (31, 20).

我不确定我需要做什么来解决这个问题。

【问题讨论】:

  • 你能发布完整的回溯吗?会有用的。
  • 当然,添加了回溯。
  • 根据错误消息,外生变量中的行数与您要求它预测的时间段数不匹配。你能提供test_size的值和X_test.shape的值吗?
  • 当然,test_size 是 31,X_test.shape 是 (31,20)。
  • 我意识到我在 step_wise 中初始化的 autoarima 对象返回了一个 SARIMAX 模型,所以不需要将它输入另一个模型

标签: python time-series arima sarimax


【解决方案1】:

在这里回答了类似的问题SARIMAX out of sample forecast with exogenous data

您需要在测试集中有 74 行才能进行预测,在训练集中需要更多或相同的行。截至目前,您只有 31 个。

【讨论】:

    猜你喜欢
    • 2017-10-24
    • 2020-08-06
    • 1970-01-01
    • 2017-01-26
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多