model_to_json drops the fitted regressor predictor; deserialized model can't predict
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- machine-learning
Research direction
Start in python/prophet/serialize.py: compare what model_to_json writes for each entry of extra_regressors against the keys model_from_json restores, and you will see the fitted predictor model dropped while prior_scale, standardize and mode survive. Then check python/prophet/forecaster.py for how add_regressor(..., regressor_predictor=True) stores that predictor during fit. Done means the reproduction script round-trips and m2.predict(future) equals m.predict(future); add it as a regression test under python/prophet/tests/.
Written by the indexing model from the issue text.
Description
model_to_json drops the fitted regressor predictor; deserialized model can't predict
With add_regressor(name, regressor_predictor=True), the model forecasts the
regressor itself inside predict(). model_to_json replaces the fitted
predictor with None, so after model_from_json the model can no longer
predict without a caller-supplied regressor column — it raises
ValueError: Regressor 'reg' missing from dataframe. If the caller does
supply the column, the deserialized model silently produces a different
forecast than the original (user values are used where the original generated
them from the predictor).
Reproduce
import numpy as np
import pandas as pd
from prophet import Prophet
from prophet.serialize import model_from_json, model_to_json
rng = np.random.default_rng(0)
ds = pd.date_range("2020-01-01", periods=60, freq="D")
reg = np.linspace(0, 30, 60) + rng.normal(0, 1.0, 60)
df = pd.DataFrame({"ds": ds, "y": 2.0 * reg + rng.normal(0, 0.5, 60), "reg": reg})
m = Prophet(weekly_seasonality=False, yearly_seasonality=False,
daily_seasonality=False, uncertainty_samples=50)
m.add_regressor("reg", regressor_predictor=True)
m.fit(df)
future = pd.DataFrame({"ds": pd.date_range("2020-03-01", periods=8, freq="D")})
m.predict(future) # works
m2 = model_from_json(model_to_json(m))
m2.extra_regressors["reg"]["predictor"] # -> None
m2.predict(future) # ValueError: Regressor 'reg' missing from dataframe
Expected behavior
The serialized model round-trips the regressor predictor so m2.predict
returns the same forecast as m.predict, as documented for
model_to_json/model_from_json ("Can be deserialized with model_from_json"
and produce the same forecast).
Environment
prophet main (5cf15c0, after 1.5.0), Python 3.12, pandas 3.1.1, numpy 2.5.5,
cmdstanpy backend, macOS arm64.
- Dominant language
- Python
- Stars
- 20.5k
- Forks
- 4.6k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from facebook/prophet
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Linux x86-64 wheel: overlapping ELF load pages in prophet_model.bin on Linux 4.18 kernelsPossibly taken @nicolasdalsass claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
make_future_dataframe returns 1970 dates when `ds` is an integer columnPossibly taken @k0nnect claimed this 10 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
plot_components fails for logistic models with minmax scaling and no floorPossibly taken @RaphaelFakhri claimed this 38 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
All issues in facebook/prophet
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
available bug good first issue tooling
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Nixitov/Riptide-Native#816 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
SpikeInterface/spikeinterface#5008 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
teng-lin/notebooklm-py#2593 ·
Maintainers usually reply within 1 day
-
claimed-by:collie role:small-fix status:running
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Beovulfo/herd-sdlc#325 · 1 comment ·
Maintainers usually reply within 1 day