# Prediction with new data brings shape error

**URL:** <https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421>\
**Category:** v5\
**Created:** [December 6, 2023, 1:41pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421 "2023-12-06T13:41:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://discourse.pymc.io/u/Sarah)\
**Post date:** [December 6, 2023, 1:41pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/1 "2023-12-06T13:41:42Z")

</div>

Hi all,

I build a simple model like this and want to do a prediction for a new data input.

```python
with pm.Model() as model:
    # data container for all variables
    target = pm.MutableData("target", df_model['target_scaled'])
    TV = pm.MutableData("TV", df_model['TV_scaled'])
    OOH = pm.MutableData("OOH", df_model['OOH_scaled'])
    Print = pm.MutableData("Print", df_model['Print_scaled'])

    #priors
    alpha = pm.Gamma("alpha", alpha=2, beta=0.5) #like intercept
    beta_TV = pm.HalfNormal("beta_TV", sigma = 0.2)
    beta_OOH = pm.HalfNormal("beta_OOH", sigma = 0.2)
    beta_Print = pm.HalfNormal("beta_Print", sigma = 0.2)
     
    #transformation
    channel_adstock_TV = pm.Deterministic(name="channel_adstock_TV", var=geometric_adstock(x=TV, alpha=0.8))
    channel_adstock_OOH = pm.Deterministic(name="channel_adstock_OOH", var=geometric_adstock(x=OOH, alpha=0.6))
    channel_adstock_Print = pm.Deterministic(name="channel_adstock_Print", var=geometric_adstock(x=Print, alpha=0.4))
    
    mu = pm.Deterministic(name="mu", var=alpha + beta_TV*channel_adstock_TV + beta_OOH*channel_adstock_OOH + beta_Print*channel_adstock_Print)
    #mu = pm.Deterministic(name="mu", var=alpha + beta_TV*geometric_adstock(x=TV, alpha=0.8) + beta_OOH*geometric_adstock(x=OOH, alpha=0.6) + beta_Print*geometric_adstock(x=Print, alpha=0.4))

    sigma = pm.Gamma("sigma", alpha=2, beta=0.5)
    y = pm.Normal("y", mu=mu, sigma=sigma, observed=target, shape=TV.shape[0])
    
    trace = pm.sample(random_seed=RANDOM_SEED, chains=2, draws=100, tune=100, target_accept = 0.9)
    posterior_predictive = pm.sample_posterior_predictive(trace)

```

When I try it without the media transformation everything works fine. But since I implemented it I get this error when I set the new data input.

```python
with model:
    pm.set_data(
        {"TV": np.array([0.08710, 0.29998, 0.33917, 0.48765, 0.53105]),
         "OOH": np.array([0.8, 0.8, 0.1, 0.92466743, 0.92466743]),
         "Print": np.array([0.83331646, 0.97947805, 0.99924164, 0.90886795, 0.71668052])
        }
    )
    predictions = pm.sample_posterior_predictive(
        trace, var_names=["y"], random_seed=RANDOM_SEED
    )

```

> **This is the full error message**
>
> Sampling: [y]
> 
> ## 0.00% [0/200 00:00\<?]
> 
> ValueError Traceback (most recent call last)  
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\link\vm.py:406, in Loop. **call** (self)  
> 403 for thunk, node, old\_storage in zip\_longest(  
> 404 self.thunks, self.nodes, self.post\_thunk\_clear, fillvalue=()  
> 405 ):  
> → 406 thunk()  
> 407 for old\_s in old\_storage:
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\graph\op.py:518, in Op.make\_py\_thunk..rval(p, i, o, n)  
> 516 @is\_thunk\_type  
> 517 def rval(p=p, i=node\_input\_storage, o=node\_output\_storage, n=node):  
> → 518 r = p(n, [x[0] for x in i], o)  
> 519 for o in node.outputs:
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\tensor\subtensor.py:1614, in IncSubtensor.perform(self, node, inputs, out\_)  
> 1612 else:  
> 1613 # sub\_x += -sub\_x + y  
> → 1614 x. **setitem** (cdata, y)  
> 1615 else:  
> 1616 # scalar case
> 
> ValueError: could not broadcast input array from shape (4,) into shape (0,)
> 
> During handling of the above exception, another exception occurred:
> 
> ValueError Traceback (most recent call last)  
> Cell In [22], line 9  
> 1 with model:  
> 2 pm.set\_data(  
> 3 {“TV”: np.array([0.08710, 0.29998, 0.33917, 0.48765, 0.53105]),  
> 4 #“TV”: np.array([0.08710, 0.08710, 0.08710, 0.08710, 0.08710]),  
> (…)  
> 7 }  
> 8 )  
> ----\> 9 predictions = pm.sample\_posterior\_predictive(  
> 10 trace, var\_names=[“y”], random\_seed=RANDOM\_SEED  
> 11 )
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pymc\sampling\forward.py:644, in sample\_posterior\_predictive(trace, model, var\_names, sample\_dims, random\_seed, progressbar, return\_inferencedata, extend\_inferencedata, predictions, idata\_kwargs, compile\_kwargs)  
> 639 # there’s only a single chain, but the index might hit it multiple times if  
> 640 # the number of indices is greater than the length of the trace.  
> 641 else:  
> 642 param = _trace[idx % len\_trace]  
> → 644 values = sampler\_fn(\*\*param)  
> 646 for k, v in zip(vars_, values):  
> 647 ppc\_trace\_t.insert(k.name, v, idx)
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pymc\util.py:393, in point\_wrapper..wrapped(\*\*kwargs)  
> 391 def wrapped(\*\*kwargs):  
> 392 input\_point = {k: v for k, v in kwargs.items() if k in ins}  
> → 393 return core\_function(\*\*input\_point)
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\compile\function\types.py:970, in Function. **call** (self, \*args, \*\*kwargs)  
> 967 t0\_fn = time.perf\_counter()  
> 968 try:  
> 969 outputs = (  
> → 970 self.vm()  
> 971 if output\_subset is None  
> 972 else self.vm(output\_subset=output\_subset)  
> 973 )  
> 974 except Exception:  
> 975 restore\_defaults()
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\link\vm.py:410, in Loop. **call** (self)  
> 408 old\_s[0] = None  
> 409 except Exception:  
> → 410 raise\_with\_op(self.fgraph, node, thunk)  
> 412 return self.perform\_updates()
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\link\utils.py:531, in raise\_with\_op(fgraph, node, thunk, exc\_info, storage\_map)  
> 526 warnings.warn(  
> 527 f"{exc\_type} error does not allow us to add an extra error message"  
> 528 )  
> 529 # Some exception need extra parameter in inputs. So forget the  
> 530 # extra long error message in that case.  
> → 531 raise exc\_value.with\_traceback(exc\_trace)
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\link\vm.py:406, in Loop. **call** (self)  
> 402 try:  
> 403 for thunk, node, old\_storage in zip\_longest(  
> 404 self.thunks, self.nodes, self.post\_thunk\_clear, fillvalue=()  
> 405 ):  
> → 406 thunk()  
> 407 for old\_s in old\_storage:  
> 408 old\_s[0] = None
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\graph\op.py:518, in Op.make\_py\_thunk..rval(p, i, o, n)  
> 516 @is\_thunk\_type  
> 517 def rval(p=p, i=node\_input\_storage, o=node\_output\_storage, n=node):  
> → 518 r = p(n, [x[0] for x in i], o)  
> 519 for o in node.outputs:  
> 520 compute\_map[o][0] = True
> 
> File ~\AppData\Roaming\Python\Python39\site-packages\pytensor\tensor\subtensor.py:1614, in IncSubtensor.perform(self, node, inputs, out\_)  
> 1611 sub\_x += y  
> 1612 else:  
> 1613 # sub\_x += -sub\_x + y  
> → 1614 x. **setitem** (cdata, y)  
> 1615 else:  
> 1616 # scalar case  
> 1617 if not self.set\_instead\_of\_inc:
> 
> ValueError: could not broadcast input array from shape (4,) into shape (0,)  
> Apply node that caused the error: SetSubtensor{start:stop, i}(SetSubtensor{start:stop, i}.0, Subtensor{:stop}.0, 6, ScalarFromTensor.0, 6)  
> Toposort index: 131  
> Inputs types: [TensorType(float64, shape=(None, 12)), TensorType(float64, shape=(None,)), ScalarType(int64), ScalarType(int64), ScalarType(uint8)]  
> Inputs shapes: [(5, 12), (4,), (), (), ()]  
> Inputs strides: [(96, 8), (8,), (), (), ()]  
> Inputs values: [‘not shown’, array([0.0871 , 0.29998, 0.33917, 0.48765]), 6, 5, 6]  
> Outputs clients: [[SetSubtensor{start:stop, i}(SetSubtensor{start:stop, i}.0, Subtensor{:stop}.0, 7, ScalarFromTensor.0, 7)]]
> 
> Backtrace when the node is created (use PyTensor flag traceback\_\_limit=N to make it longer):  
> File “C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”, line 2995, in \_run\_cell  
> return runner(coro)  
> File “C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\async\_helpers.py”, line 129, in _pseudo\_sync\_runner  
> coro.send(None)  
> File “C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”, line 3194, in run\_cell\_async  
> has\_raised = await self.run\_ast\_nodes(code\_ast.body, cell\_name,  
> File “C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”, line 3373, in run\_ast\_nodes  
> if await self.run\_code(code, result, async_=asy):  
> File “C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py”, line 3433, in run\_code  
> exec(code\_obj, self.user\_global\_ns, self.user\_ns)  
> File “C:\Users\s.leschke.CI10229\AppData\Local\Temp\ipykernel\_16164\1565565827.py”, line 14, in   
> channel\_adstock\_TV = pm.Deterministic(name=“channel\_adstock\_TV”, var=geometric\_adstock(x=TV, alpha=0.8))  
> File “C:\Users\s.leschke.CI10229\AppData\Roaming\Python\Python39\site-packages\pymc\_marketing\mmm\transformers.py”, line 100, in geometric\_adstock  
> return batched\_convolution(x, w, axis=axis)  
> File “C:\Users\s.leschke.CI10229\AppData\Roaming\Python\Python39\site-packages\pymc\_marketing\mmm\transformers.py”, line 50, in batched\_convolution  
> padded\_x = pt.set\_subtensor(
> 
> HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

I already tried to specify the model in another way, but I brings the same error.

```python
coords = {"channel": ['TV_scaled','OOH_scaled','Print_scaled']}

with pm.Model(coords = coords) as model:
    model.add_coord("KW", np.arange(299), mutable = True)
    
    ## data container for all variables
    target = pm.MutableData("target", df_model['target_scaled'], dims="KW")
    input = pm.MutableData("input", df_model[features], dims=("KW", "channel") )

    #priors
    alpha = pm.Gamma("alpha", alpha=2, beta=0.5) #like intercept
    beta = pm.HalfNormal("beta", sigma = 0.2, dims=("channel",))
    
    #transform media
    channel_adstock = pm.Deterministic(name="channel_adstock", var=geometric_adstock(x=input, alpha=0.8), dims=("KW", "channel"),)
    
    channel_contributions = pm.Deterministic(
                name="channel_contributions",
                var=channel_adstock * beta,
                dims=("KW", "channel"),
            )
    
    #define target
    mu_var = alpha + channel_contributions.sum(axis=-1)
    mu = pm.Deterministic(name="mu", var=mu_var, dims="KW")

    sigma = pm.Gamma("sigma", alpha=2, beta=0.5)
    y = pm.Normal("y", mu=mu, sigma=sigma, observed=target, shape=df_model['TV_scaled'].shape[0])
    
    trace = pm.sample(random_seed=RANDOM_SEED, chains=2, draws=100, tune=100, target_accept = 0.9)
    posterior_predictive = pm.sample_posterior_predictive(trace)

new_data = pd.DataFrame({'KW': [0,1,2,3,4],
                        'TV_scaled': [0.08710, 0.29998, 0.33917, 0.48765, 0.53105],
                        'OOH_scaled': [0.8, 0.8, 0.1, 0.92466743, 0.92466743],
                       'Print_scaled': [0.83331646, 0.97947805, 0.99924164, 0.90886795, 0.71668052]})

new_data = new_data.set_index(new_data['KW'])
new_data = new_data[features]
new_data

with model:
    pm.set_data(
        {"input": new_data}, coords = {"channel": ['TV_scaled','OOH_scaled','Print_scaled'], "KW":[0,1,2,3,4]}
    )
    predictions = pm.sample_posterior_predictive(
        trace, var_names=["y"], random_seed=RANDOM_SEED
    )

```

Any suggestions? Are the data containers and coords defined correctly like this?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://discourse.pymc.io/u/Sarah)\
**Post date:** [December 13, 2023, 1:06pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/2 "2023-12-13T13:06:33Z")

</div>

[@Community\_Team](https://discourse.pymc.io/groups/community_team): Since this is a very important topic for my work, I would be very grateful if someone could take a look at it.

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [December 13, 2023, 2:00pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/3 "2023-12-13T14:00:37Z")

</div>

It’s difficult to know what is going on without an example that I can run. But if I had to guess, I might assume that you are replacing `TV`, `OOH`, and `Print`, with new values (and a different number of values/shape), but **not** replacing `target` (so `target` is still the original shape) and the mismatch is causing the issue. Does that seem plausible?

---

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://discourse.pymc.io/u/Sarah)\
**Post date:** [December 13, 2023, 3:29pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/4 "2023-12-13T15:29:14Z")

</div>

Thanks for your reply @cluhmann . But I think the reason must be something else.  
Because I defined all variables as mutable and made the target variable depending on the shape of a input variable as it is explained in one of the PyMC examples.

The prediction for this basic model works well (even with different shape of the input):

```python
with pm.Model() as model_basic:
    ## data container for all variables
    target = pm.MutableData("target", df_model['target_scaled'])
    TV = pm.MutableData("TV", df_model['TV_scaled'])
    OOH = pm.MutableData("OOH", df_model['OOH_scaled'])
    Print = pm.MutableData("Print", df_model['Print_scaled'])

    #priors
    alpha = pm.Gamma("alpha", alpha=2, beta=0.5) #like intercept
    beta_TV = pm.HalfNormal("beta_TV", sigma = 0.2)
    beta_OOH = pm.HalfNormal("beta_OOH", sigma = 0.2)
    beta_Print = pm.HalfNormal("beta_Print", sigma = 0.2)
        
    mu = pm.Deterministic(name="mu", var=alpha + beta_TV*TV + beta_OOH*OOH + beta_Print*beta_Print)
    
    sigma = pm.Gamma("sigma", alpha=2, beta=0.5)
    y = pm.Normal("y", mu=mu, sigma=sigma, observed=target, shape=TV.shape[0])
    
    trace_basic = pm.sample(random_seed=RANDOM_SEED, chains=2, draws=100, tune=100, target_accept = 0.9)
    posterior_predictive_basic = pm.sample_posterior_predictive(trace_basic)

with model_basic:
    pm.set_data(
        {"TV": np.array([0.08710, 0.29998, 0.33917, 0.48765, 0.53105]),
         "OOH": np.array([0.8, 0.8, 0.1, 0.92466743, 0.92466743]),
         "Print": np.array([0.83331646, 0.97947805, 0.99924164, 0.90886795, 0.71668052])
        }
    )
    predictions = pm.sample_posterior_predictive(
        trace_basic, predictions=True
    )

```

The error ocurrs when I want to run the model with adstock transformation like this.

```python
with pm.Model() as model_adstock:
    ## data container for all variables
    target = pm.MutableData("target", df_model['target_scaled'])
    TV = pm.MutableData("TV", df_model['TV_scaled'])
    OOH = pm.MutableData("OOH", df_model['OOH_scaled'])
    Print = pm.MutableData("Print", df_model['Print_scaled'])

    #priors
    alpha = pm.Gamma("alpha", alpha=2, beta=0.5) #like intercept
    beta_TV = pm.HalfNormal("beta_TV", sigma = 0.2)
    beta_OOH = pm.HalfNormal("beta_OOH", sigma = 0.2)
    beta_Print = pm.HalfNormal("beta_Print", sigma = 0.2)
     
    #transformation
    channel_adstock_TV = pm.Deterministic(name="channel_adstock_TV", var=geometric_adstock(x=TV, alpha=0.8))
    channel_adstock_OOH = pm.Deterministic(name="channel_adstock_OOH", var=geometric_adstock(x=OOH, alpha=0.6))
    channel_adstock_Print = pm.Deterministic(name="channel_adstock_Print", var=geometric_adstock(x=Print, alpha=0.4))
    
    mu = pm.Deterministic(name="mu", var=alpha + beta_TV*channel_adstock_TV + beta_OOH*channel_adstock_OOH + beta_Print*channel_adstock_Print)
    #mu = pm.Deterministic(name="mu", var=alpha + beta_TV*geometric_adstock(x=TV, alpha=0.8) + beta_OOH*geometric_adstock(x=OOH, alpha=0.6) + beta_Print*geometric_adstock(x=Print, alpha=0.4))

    sigma = pm.Gamma("sigma", alpha=2, beta=0.5)
    y = pm.Normal("y", mu=mu, sigma=sigma, observed=target, shape=TV.shape[0])
    
    trace = pm.sample(random_seed=RANDOM_SEED, chains=2, draws=100, tune=100, target_accept = 0.9)
    posterior_predictive = pm.sample_posterior_predictive(trace)

```

My assumptions is that something wents wrong because I have to define the transformed variables as pm.Deterministic and that this can’t be changed in the prediction. But I also saw that in PyMC-Marketing it’s done in a similar way.

This is how I created the data:

```python
import pandas as pd
import numpy as np
import pickle
from pytensor import tensor as pt
import arviz as az
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

def adbank_geom(variable, factor):
    variable=list(variable)
    temp = []
    for i in range(0,len(variable)):
      if i == 0:
        temp.append(variable[0])
      else:
        temp.append(variable[i] + factor * temp[i-1])
    return(temp)

def hill_transform(x, inflexion, slope): 
  return 1 / (1 + (x / inflexion)**(-slope))

dat_example=pd.DataFrame()
dat_example['TV']=np.array([ 500, 400, 400, 300, 200, 400, 400, 300, 300, 200, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 1000, 800, 600, 1000, 800, 600, 200, 200,
        200, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 250, 200, 200,
        150, 100, 400, 400, 300, 300, 200, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       1500, 1200, 900, 1000, 2400, 900, 600, 600, 400, 200, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 500, 400, 300, 500, 400, 300, 200, 200, 200,
        200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0])

dat_example['OOH']=np.array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 200,
       200, 200, 300, 300, 300, 50, 50, 50, 50, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 50,
        50, 200, 200, 200, 100, 100, 100, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 40, 40, 80, 80, 240, 240, 500, 500, 300, 200, 100, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 600, 500, 400, 600, 500, 400, 300, 200, 100, 50,
        50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

dat_example['Print']=np.array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 100, 80, 80, 60, 60, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 60, 80, 80,
       100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        20, 40, 60, 80, 100, 100, 80, 60, 40, 20, 10, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20,
        40, 60, 80, 100, 100, 80, 60, 40, 20, 10, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

dat_example['TV_tf'] = hill_transform(np.array(adbank_geom(dat_example['TV'], 0.6)), 800, 5)
dat_example['OOH_tf'] = hill_transform(np.array(adbank_geom(dat_example['OOH'], 0.4)), 300, 7)
dat_example['Print_tf'] = hill_transform(np.array(adbank_geom(dat_example['Print'], 0.2)), 70, 3)

dat_example['target'] = dat_example['TV_tf']*60 + dat_example['OOH_tf']*40 + dat_example['Print_tf']*20 + 10
dat_example['KW'] = range(0, 299)
dat_example['Region'] = 'Test'

df_model = dat_example

scaler_tv = MinMaxScaler()
scaler_ooh = MinMaxScaler()
scaler_print = MinMaxScaler()
scaler_tg = MinMaxScaler()

df_model['TV_scaled'] = scaler_tv.fit_transform(np.array(df_model['TV_tf']).reshape(-1, 1)).flatten()
df_model['OOH_scaled'] = scaler_ooh.fit_transform(np.array(df_model['OOH_tf']).reshape(-1, 1)).flatten()
df_model['Print_scaled'] = scaler_print.fit_transform(np.array(df_model['Print_tf']).reshape(-1, 1)).flatten()
df_model['target_scaled'] = scaler_tg.fit_transform(np.array(df_model['target']).reshape(-1, 1)).flatten()

features = ['TV_scaled','OOH_scaled','Print_scaled']

```

---

<div class="post-metadata">

**Author:** ![mcao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mcao/32/4886_2.png) [@mcao](https://discourse.pymc.io/u/mcao)\
**Post date:** [December 18, 2023, 6:10am UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/5 "2023-12-18T06:10:15Z")

</div>

Hi @Sarah, I ran your codes and it worked on my end.

You didn’t provide the `geometric_adstock` function, so I used the one below which I got from this [post](https://juanitorduz.github.io/pymc_mmm/#:~:text=def%20geometric_adstock(x%2C%20alpha,%2B%20pt.exp(%2Dlam%20*%20x))).

```auto
def geometric_adstock(x, alpha: float = 0.0, l_max: int = 12):
    """Geometric adstock transformation."""
    cycles = [
        pt.concatenate(
            [pt.zeros(i), x[: x.shape[0] - i]]
        )
        for i in range(l_max)
    ]
    x_cycle = pt.stack(cycles)
    w = pt.as_tensor_variable([pt.power(alpha, i) for i in range(l_max)])
    return pt.dot(w, x_cycle)

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://discourse.pymc.io/u/Sarah)\
**Post date:** [December 18, 2023, 9:50am UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/6 "2023-12-18T09:50:07Z")

</div>

Hi @mcao,  
thanks, that’s a good hint. I’ll have a closer look on the adstock function itself.  
Which versions for pymc and pytensor are you using?

---

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://discourse.pymc.io/u/Sarah)\
**Post date:** [January 2, 2024, 2:29pm UTC](https://discourse.pymc.io/t/prediction-with-new-data-brings-shape-error/13421/7 "2024-01-02T14:29:12Z")

</div>

Solution: number of input values for prediction has to be minimum the value of l\_max in the adstock function.
