You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/howto/model_builder.myst.md
+109-54
Original file line number
Diff line number
Diff line change
@@ -5,16 +5,16 @@ jupytext:
5
5
format_name: myst
6
6
format_version: 0.13
7
7
kernelspec:
8
-
display_name: pymc-dev
8
+
display_name: pymc-marketing
9
9
language: python
10
-
name: python3
10
+
name: pymc-marketing
11
11
---
12
12
13
13
# Using ModelBuilder class for deploying PyMC models
14
14
:::{post} Feb 22, 2023
15
15
:tags: deployment
16
16
:category: Advanced
17
-
:author: Shashank Kirtania, Thomas Wiecki
17
+
:author: Shashank Kirtania, Thomas Wiecki, Michał Raczycki
18
18
:::
19
19
20
20
+++
@@ -32,6 +32,8 @@ The new `ModelBuilder` class allows users to use methods to `fit()`, `predict()`
32
32
Let's go through the full workflow, starting with a simple linear regression PyMC model as it's usually written. Of course, this model is just a place-holder for your own model.
33
33
34
34
```{code-cell} ipython3
35
+
from typing import Dict, List, Optional, Tuple, Union
36
+
35
37
import arviz as az
36
38
import matplotlib.pyplot as plt
37
39
import numpy as np
@@ -99,10 +101,11 @@ To define our desired model we inherit from the `ModelBuilder` class. There are
_serializable_model_config is a property that returns a dictionary with all the model parameters that we want to save.
195
+
as some of the data structures are not json serializable, we need to convert them to json serializable objects.
196
+
Some models will need them, others can just define them to return the model_config.
197
+
"""
198
+
return self.model_config
199
+
200
+
def _save_input_params(self, idata) -> None:
201
+
"""
202
+
Saves any additional model parameters (other than the dataset) to the idata object.
203
+
204
+
These parameters are stored within `idata.attrs` using keys that correspond to the parameter names.
205
+
If you don't need to store any extra parameters, you can leave this method unimplemented.
177
206
178
-
Now we can create the `LinearModel` object.
207
+
Example:
208
+
For saving customer IDs provided as an 'customer_ids' input to the model:
209
+
self.customer_ids = customer_ids.values #this line is done outside of the function, preferably at the initialization of the model object.
210
+
idata.attrs["customer_ids"] = json.dumps(self.customer_ids.tolist()) # Convert numpy array to a JSON-serializable list.
211
+
"""
212
+
pass
179
213
180
-
But we need some example data. This is where defining a `create_sample_input()` method as done above is useful. It gives users of your model an easy way to generate data (and configurations) to test your model on.
Depending on the model, we might need to preprocess the data before fitting the model.
221
+
all required preprocessing and conditional assignments should be defined here.
222
+
"""
223
+
self.model_coords = None # in our case we're not using coords, but if we were, we would define them here, or later on in the function, if extracting them from the data.
224
+
# as we don't do any data preprocessing, we just assign the data givenin by the user. Note that it's very basic model,
225
+
# and usually we would need to do some preprocessing, or generate the coords from the data.
226
+
self.X = X
227
+
self.y = y
228
+
```
229
+
230
+
Now we can create the `LinearModel` object. First step we need to take care of, is data generation:
0 commit comments