or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-forecasting.mddiagnostics.mdholidays.mdindex.mdplotting.mdserialization.mdutilities.md

serialization.mddocs/

0

# Serialization

1

2

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.

3

4

## Capabilities

5

6

### JSON Serialization

7

8

Save and load Prophet models using JSON format.

9

10

```python { .api }

11

def model_to_json(model):

12

"""

13

Serialize a Prophet model to JSON string.

14

15

Parameters:

16

- model: Prophet, fitted Prophet model

17

18

Returns:

19

- str, JSON string representation of the model

20

"""

21

22

def model_from_json(model_json):

23

"""

24

Deserialize a Prophet model from JSON string.

25

26

Parameters:

27

- model_json: str, JSON string representation of a Prophet model

28

29

Returns:

30

- Prophet, reconstructed Prophet model

31

"""

32

```

33

34

### Dictionary Serialization

35

36

Convert Prophet models to/from Python dictionaries.

37

38

```python { .api }

39

def model_to_dict(model):

40

"""

41

Convert a Prophet model to a dictionary.

42

43

Parameters:

44

- model: Prophet, fitted Prophet model

45

46

Returns:

47

- dict, dictionary representation of the model

48

"""

49

50

def model_from_dict(model_dict):

51

"""

52

Recreate a Prophet model from a dictionary.

53

54

Parameters:

55

- model_dict: dict, dictionary representation of a Prophet model

56

57

Returns:

58

- Prophet, reconstructed Prophet model

59

"""

60

```

61

62

### Serialization Constants

63

64

Internal constants defining which model attributes are serialized.

65

66

```python { .api }

67

SIMPLE_ATTRIBUTES = [

68

'growth', 'n_changepoints', 'changepoint_range', 'yearly_seasonality',

69

'weekly_seasonality', 'daily_seasonality', 'seasonality_mode',

70

'seasonality_prior_scale', 'holidays_prior_scale', 'changepoint_prior_scale',

71

'mcmc_samples', 'interval_width', 'uncertainty_samples', 'scaling',

72

'holidays_mode'

73

]

74

# List of simple model attributes to serialize

75

76

PD_SERIES = ['y_scale', 'start', 'y_min', 'y_max', 't_scale']

77

# Pandas Series attributes

78

79

PD_TIMESTAMP = ['start']

80

# Pandas Timestamp attributes

81

82

PD_TIMEDELTA = ['t_scale']

83

# Pandas Timedelta attributes

84

85

PD_DATAFRAME = [

86

'history', 'holidays', 'train_holiday_names', 'train_component_cols',

87

'component_modes', 'seasonalities', 'extra_regressors'

88

]

89

# Pandas DataFrame attributes

90

91

NP_ARRAY = ['changepoints_t', 'train_component_cols']

92

# NumPy array attributes

93

94

ORDEREDDICT = ['seasonalities', 'extra_regressors']

95

# OrderedDict attributes

96

```

97

98

## Usage Examples

99

100

### Save and Load Model

101

102

```python

103

from prophet import Prophet

104

from prophet.serialize import model_to_json, model_from_json

105

106

# Train a model

107

model = Prophet()

108

model.fit(df)

109

110

# Save model to JSON string

111

model_json = model_to_json(model)

112

113

# Save to file

114

with open('prophet_model.json', 'w') as f:

115

f.write(model_json)

116

117

# Load from file

118

with open('prophet_model.json', 'r') as f:

119

model_json = f.read()

120

121

# Recreate model from JSON

122

loaded_model = model_from_json(model_json)

123

124

# Use loaded model for prediction

125

forecast = loaded_model.predict(future)

126

```

127

128

### Dictionary-based Serialization

129

130

```python

131

from prophet.serialize import model_to_dict, model_from_dict

132

import pickle

133

134

# Convert model to dictionary

135

model_dict = model_to_dict(model)

136

137

# Save dictionary using pickle

138

with open('prophet_model.pkl', 'wb') as f:

139

pickle.dump(model_dict, f)

140

141

# Load and reconstruct

142

with open('prophet_model.pkl', 'rb') as f:

143

loaded_dict = pickle.load(f)

144

145

reconstructed_model = model_from_dict(loaded_dict)

146

```

147

148

### Model Validation After Loading

149

150

```python

151

import numpy as np

152

153

# Verify model integrity after loading

154

original_forecast = model.predict(future)

155

loaded_forecast = loaded_model.predict(future)

156

157

# Check if forecasts are identical

158

forecast_match = np.allclose(

159

original_forecast['yhat'].values,

160

loaded_forecast['yhat'].values

161

)

162

print(f"Forecasts match: {forecast_match}")

163

```