A simple example with exogenous data #6544
-
Hello! I checked the forecasting tutorial and some of the threads in this repo's discussion, but still have trouble figuring out how to run a prediction with just exogenous data. Here's a sample example, where the time series element is absent as I'm trying to narrow down the problem in grokking sktime. This is a basic y=a+bX problem, I expect perfect forecasting accuracy for this linear model: from pandas import DataFrame
from sklearn.linear_model import LinearRegression
from sktime.forecasting.compose import make_reduction
from sktime.pipeline import sklearn_to_sktime
df = DataFrame({"x": [0, 1, 3, 4, 5, 8, 15, 16]})
df["y1"] = 5 + 3 * df["x"]
df["y2"] = 10 + 10 * df["x"]
df["y1true"] = 5 + 3 * df["x"]
df["y2true"] = 10 + 10 * df["x"]
df.loc[6:, "y1"] = None
df.loc[6:, "y2"] = None
print(df)
mask_fit = df["y1"].notna()
Xf = df[mask_fit]["x"]
yf = df[mask_fit][["y1", "y2"]]
mask_predict = df["y1"].isna()
Xp = df[mask_predict]["x"]
yp = df[mask_predict]["y1"]
# this is not quite what I want, but I don't know what else I should use for this problem
forecaster = make_reduction(LinearRegression(), strategy="recursive", window_length=1)
forecaster.fit(y=yf, X=Xf)
print(forecaster.predict(fh=[1, 2, 3], X=Xp))
# expect perfect forecast From the documentation for |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hmm, it looks like what I want is from pandas import DataFrame
from sklearn.linear_model import LinearRegression
from sktime.forecasting.compose import DirectReductionForecaster
from sktime.pipeline import sklearn_to_sktime
df = DataFrame({"x": [0, 1, 3, 4, 5, 8, 15, 16]})
df["y1"] = 5 + 3 * df["x"]
df["y2"] = 10 + 10 * df["x"]
df["y1true"] = 5 + 3 * df["x"]
df["y2true"] = 10 + 10 * df["x"]
df.loc[6:, "y1"] = None
df.loc[6:, "y2"] = None
print(df)
mask_fit = df["y1"].notna()
Xf = df[mask_fit]["x"]
yf = df[mask_fit][["y1", "y2"]]
mask_predict = df["y1"].isna()
Xp = df[mask_predict]["x"]
yp = df[mask_predict]["y1"]
forecaster = DirectReductionForecaster(LinearRegression(), window_length=1)
forecaster.fit(y=yf, X=Xf, fh=[1,2])
print(forecaster.predict(fh=[1, 2], X=Xp)) |
Beta Was this translation helpful? Give feedback.
-
You need to pass The direct reduction forecaster requires the |
Beta Was this translation helpful? Give feedback.
You need to pass
fh
only in one offit
orpredict
, but some forecasters - like direct reduction - need it infit
already. Whether a forecaster requires is infit
is annotated via therequires-fh-in-fit
tag.The direct reduction forecaster requires the
fh
in fit, because you need to lag they
in the training of the tabular regressors according to thefh
, againstX
.