Automatic forecasting procedure for time series data with strong seasonal effects and multiple seasons of historical data
—
Model persistence functionality for saving and loading trained Prophet models using JSON serialization with support for all model components including parameters, history, and Stan backend state.
Save and load Prophet models using JSON format.
def model_to_json(model):
"""
Serialize a Prophet model to JSON string.
Parameters:
- model: Prophet, fitted Prophet model
Returns:
- str, JSON string representation of the model
"""
def model_from_json(model_json):
"""
Deserialize a Prophet model from JSON string.
Parameters:
- model_json: str, JSON string representation of a Prophet model
Returns:
- Prophet, reconstructed Prophet model
"""Convert Prophet models to/from Python dictionaries.
def model_to_dict(model):
"""
Convert a Prophet model to a dictionary.
Parameters:
- model: Prophet, fitted Prophet model
Returns:
- dict, dictionary representation of the model
"""
def model_from_dict(model_dict):
"""
Recreate a Prophet model from a dictionary.
Parameters:
- model_dict: dict, dictionary representation of a Prophet model
Returns:
- Prophet, reconstructed Prophet model
"""Internal constants defining which model attributes are serialized.
SIMPLE_ATTRIBUTES = [
'growth', 'n_changepoints', 'changepoint_range', 'yearly_seasonality',
'weekly_seasonality', 'daily_seasonality', 'seasonality_mode',
'seasonality_prior_scale', 'holidays_prior_scale', 'changepoint_prior_scale',
'mcmc_samples', 'interval_width', 'uncertainty_samples', 'scaling',
'holidays_mode'
]
# List of simple model attributes to serialize
PD_SERIES = ['y_scale', 'start', 'y_min', 'y_max', 't_scale']
# Pandas Series attributes
PD_TIMESTAMP = ['start']
# Pandas Timestamp attributes
PD_TIMEDELTA = ['t_scale']
# Pandas Timedelta attributes
PD_DATAFRAME = [
'history', 'holidays', 'train_holiday_names', 'train_component_cols',
'component_modes', 'seasonalities', 'extra_regressors'
]
# Pandas DataFrame attributes
NP_ARRAY = ['changepoints_t', 'train_component_cols']
# NumPy array attributes
ORDEREDDICT = ['seasonalities', 'extra_regressors']
# OrderedDict attributesfrom prophet import Prophet
from prophet.serialize import model_to_json, model_from_json
# Train a model
model = Prophet()
model.fit(df)
# Save model to JSON string
model_json = model_to_json(model)
# Save to file
with open('prophet_model.json', 'w') as f:
f.write(model_json)
# Load from file
with open('prophet_model.json', 'r') as f:
model_json = f.read()
# Recreate model from JSON
loaded_model = model_from_json(model_json)
# Use loaded model for prediction
forecast = loaded_model.predict(future)from prophet.serialize import model_to_dict, model_from_dict
import pickle
# Convert model to dictionary
model_dict = model_to_dict(model)
# Save dictionary using pickle
with open('prophet_model.pkl', 'wb') as f:
pickle.dump(model_dict, f)
# Load and reconstruct
with open('prophet_model.pkl', 'rb') as f:
loaded_dict = pickle.load(f)
reconstructed_model = model_from_dict(loaded_dict)import numpy as np
# Verify model integrity after loading
original_forecast = model.predict(future)
loaded_forecast = loaded_model.predict(future)
# Check if forecasts are identical
forecast_match = np.allclose(
original_forecast['yhat'].values,
loaded_forecast['yhat'].values
)
print(f"Forecasts match: {forecast_match}")Install with Tessl CLI
npx tessl i tessl/pypi-prophet