How to fetch values from a theano-pymc variable and store it in a pandas dataframe column?

44 Views Asked by At

I am trying to build a bayesian model using pymc3 package. I am using marketing spends data to predict sales. Here, I am trying to build to get the output of a geometric_adstock_tt function into a pandas column but not able to do that as object of type 'TensorVariable' has no len(). Any help would be appreciated!

import theano.tensor as tt
def geometric_adstock_tt(x, alpha=0,L=12, normalize=True):
    df = pd.DataFrame()
    w = tt.power(alpha, np.arange(L))
    xx = tt.stack([tt.concatenate([tt.zeros(i), x[:x.shape[0]-i]]) for i in range(L)])
    
    if not normalize:
        y = tt.dot(w,xx)
    else:
        y = tt.dot(w/tt.sum(w),xx)
    return y

x_test = np.ones(100)*10000
import pymc3 as pm
with pm.Model() as test_model:
    alpha_test = Beta('alpha_test',3,3)
    df['test_col'] = geometric_adstock_tt(x_test, alpha=alpha_test)```
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_326/3345703325.py in <module>
     15 with pm.Model() as test_model:
     16     alpha_test = Beta('alpha_test',3,3)
---> 17     df['test_col'] = geometric_adstock_tt(x_test, alpha=alpha_test)

/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   3653         else:
   3654             # set column
-> 3655             self._set_item(key, value)
   3656 
   3657     def _setitem_slice(self, key: slice, value):

/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py in _set_item(self, key, value)
   3830         ensure homogeneity.
   3831         """
-> 3832         value = self._sanitize_column(value)
   3833 
   3834         if (

/opt/conda/lib/python3.8/site-packages/pandas/core/frame.py in _sanitize_column(self, value)
   4527 
   4528         if is_list_like(value):
-> 4529             com.require_length_match(value, self.index)
   4530         return sanitize_array(value, self.index, copy=True, allow_2d=True)
   4531 

/opt/conda/lib/python3.8/site-packages/pandas/core/common.py in require_length_match(data, index)
    554     Check the length of data matches the length of the index.
    555     """
--> 556     if len(data) != len(index):
    557         raise ValueError(
    558             "Length of values "

TypeError: object of type 'TensorVariable' has no len()```
0

There are 0 best solutions below