#๐Ÿ”’ error finding training score in SARIMAX

24 messages ยท Page 1 of 1 (latest)

inner wharf
#

Hello , I am trying to find R2 score on the training dataset , but it is giving me this error.

ValueError: Provided exogenous values are not of the appropriate shape. Required (1, 6), got (42, 6).

my code -

model = SARIMAX(endog = y_train, exog = x_train, order = order, seasonal_order = seasonal_order)
s_results = model.fit()

Generate forecasts for the training data

exog_forecast = x_train # Provide exogenous variables for future periods
forecast = s_results.predict(start=1, end=42, exog = exog_forecast)

note
x_train.shape , x_test.shape , y_train.shape , y_test.shape --> ((42, 6), (6, 6), (42,), (6,))

thank you in advance.

livid rootBOT
#

@inner wharf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

floral marsh
#

hi @inner wharf, the .predict function has an interesting interface, that's what's kicking here

#

but before that, this is confusing a bit:

#

exog_forecast = x_train # Provide exogenous variables for future periods

#

the variable name and comment are implying future but i guess it is temporarily so

#

anyway

#

the "start" and "end" are 0-based, so what's happening is that

#

you are requesting predictions for y[1], y[2], ..., y[41] and y[42] which doesn't exist and actually means y_future[0]

#

i.e., all of the training data except for the very first sample, and also the first future sample

#

does this make sense?

#

that's why the message is telling you "i expected 1 sample exog, you gave 42"

#

because you requested 1 future sample prediction along with 41 in-sample, hence the error

#

to fix this, you have couple of options:

#

.predict(start=0, end=41, exog=X_train) <- recalling that they are 0-based, we pass 0 and len(y) - 1 here

#

but for in-sample predictions, exog is not required to be passed extra -- the model already has it, so:

#

.predict(start=0, end=41) does the same

#

but if you didn't specify "start" and "end" at all, it gives in-sample predictions anyway, so:

#

.predict() also does the same

#

but the in-sample predictions are already available as a post-fit attribute, so:

#

.fittedvalues also does the same and probably the best option

#

TL;DR you have an off-by-1 error in specifiying the range of the predictions. it turns out the in-sample predictions are already available as an attribute s_results.fittedvalues so you don't deal with .predict() calls at all.

livid rootBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.