JoaquinAmatRodrigo / skforecast

Time series forecasting with machine learning models
https://skforecast.org
BSD 3-Clause "New" or "Revised" License
996 stars 113 forks source link

ForecasterAutoReg Error On .fit() #679

Closed jrodenberg0 closed 1 month ago

jrodenberg0 commented 1 month ago

Hey all, getting an error when fitting forecaster autoreg. Currently enrolled in timeseries forecasting course and when i download their notebook it works perfectly. Super stumped on this one... ml_forecasting_1.pdf

JoaquinAmatRodrigo commented 1 month ago

Hi @jrodenberg0

Thanks for reporting the issue. Could you share the url to download the data? I cannot see the full url in the pdf file.

JoaquinAmatRodrigo commented 1 month ago

Could you check if this code works for you?

import pandas as pd
from skforecast.ForecasterAutoreg import ForecasterAutoreg
from sklearn.linear_model import Lasso
import skforecast
import sklearn

print(f"skforecast: {skforecast.__version__}")
print(f"skearn: {sklearn.__version__}")

df = pd.read_csv(
    "https://raw.githubusercontent.com/tidyverts/tsibbledata/master/data-raw/vic_elec/VIC2015/demand.csv"
)
df.drop(columns=["Industrial"], inplace=True)
# Convert the integer Date to an actual date with datetime type
df["date"] = df["Date"].apply(
    lambda x: pd.Timestamp("1899-12-30") + pd.Timedelta(x, unit="days")
)
# Create a timestamp from the integer Period representing 30 minute interval
df["date_time"] = df["date"] + pd.to_timedelta((df["Period"] - 1) * 30, unit="m")
df.dropna(inplace=True)
# Rename columns
df = df[["date_time", "OperationalLessIndustrial"]]
df.columns = ["date_time", "demand"]
# Resample to hourly
df = df.set_index("date_time").resample("H").agg({"demand": "sum"})

split_idx = "2014-12-31 23:59:59"
X_train = df.loc[:split_idx]
X_test = df.loc[split_idx:]

model = Lasso()
forecaster = ForecasterAutoreg(
    regressor=model,  # the machine learning model
    lags=[1, 24, 7 * 24],  # the lag features to create
    forecaster_id="recursive",
)
forecaster.fit(y = X_train['demand'])
forecaster.predict(steps=5)

skforecast: 0.12.0 skearn: 1.4.2

2015-01-01 00:00:00 8067.171431 2015-01-01 01:00:00 7670.026188 2015-01-01 02:00:00 7246.648989 2015-01-01 03:00:00 6884.436850 2015-01-01 04:00:00 6656.328930 Freq: H, Name: pred, dtype: float64

JavierEscobarOrtiz commented 1 month ago

@jrodenberg0 If the code doesn't work for you, try it with Python 3.11

JoaquinAmatRodrigo commented 1 month ago

We are closing this issue due to inactivity. If you believe this issue is still relevant, please feel free to reopen it or create a new issue with updated details.

Thank you for your understanding and cooperation.