# Pymc with external GP function

**URL:** <https://discourse.pymc.io/t/pymc-with-external-gp-function/12371>\
**Category:** version agnostic\
**Tags:** shape\_issue\
**Created:** [June 21, 2023, 11:35pm UTC](https://discourse.pymc.io/t/pymc-with-external-gp-function/12371 "2023-06-21T23:35:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_San](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/e_san/32/6729_2.png) [@E\_San](https://discourse.pymc.io/u/E_San)\
**Post date:** [June 21, 2023, 11:35pm UTC](https://discourse.pymc.io/t/pymc-with-external-gp-function/12371/1 "2023-06-21T23:35:49Z")

</div>

I’m trying to use pymc3 to find the parameter of a set of data modeled through a GP in skit-learn. I want pymc to take samples of the variable H, input this H into my GP to obtain an array, this array will be compared with a goal array called ‘P’.  
The code is:  
def GPR(H):  
u\_ = np.linspace(0, 10, 51)  
H\_ = np.tile(H, 51)  
u\_H = np.column\_stack((u\_, H\_))  
y\_predUnL = gp.predict(u\_H)  
return y\_predUnL

with pm.Model() as Infer:  
beta = pm.Normal(‘beta’, mu=4, sigma=1, shape=1)  
sigma = pm.HalfNormal(‘sigma’, sigma=0.05)  
mu = GPR\_UnLd(beta)  
recruits = pm.Normal(‘P’, mu=mu, sigma=sigma, observed=P)

```
# Run sampling
trace = pm.sample(4000)

```

# Analyze the results

pm.summary(trace)  
pm.traceplot(trace)

## I’m getting this error:

TypeError Traceback (most recent call last)  
TypeError: float() argument must be a string or a real number, not ‘FreeRV’

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)  
 in \<cell line: 8\>()  
9 beta = pm.Normal(‘beta’, mu=4, sigma=1, shape=1)  
10 sigma = pm.HalfNormal(‘sigma’, sigma=0.05)  
—\> 11 mu = GPR\_UnLd(beta)  
12 recruits = pm.Normal(‘P’, mu=mu, sigma=sigma, observed=P)  
13

4 frames  
 in GPR\_UnLd(H)  
2 u\_ = np.linspace(0, 10, 51)  
3 H\_ = np.tile(H, 51)  
----\> 4 y\_predUnL = gp.predict(np.column\_stack((u\_, H\_)))  
5 print(y\_predUnL)  
6 print(y\_predUnL.shape)

/usr/local/lib/python3.10/dist-packages/sklearn/gaussian\_process/\_gpr.py in predict(self, X, return\_std, return\_cov)  
383 dtype, ensure\_2d = None, False  
384  
 → 385 X = self._validate\_data(X, ensure\_2d=ensure\_2d, dtype=dtype, reset=False)  
386  
387 if not hasattr(self, "X\_train_"): # Unfitted;predict based on GP prior

/usr/local/lib/python3.10/dist-packages/sklearn/base.py in \_validate\_data(self, X, y, reset, validate\_separately, \*\*check\_params)  
563 raise ValueError(“Validation should be done on X, y or both.”)  
564 elif not no\_val\_X and no\_val\_y:  
 → 565 X = check\_array(X, input\_name=“X”, \*\*check\_params)  
566 out = X  
567 elif no\_val\_X and not no\_val\_y:

/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py in check\_array(array, accept\_sparse, accept\_large\_sparse, dtype, order, copy, force\_all\_finite, ensure\_2d, allow\_nd, ensure\_min\_samples, ensure\_min\_features, estimator, input\_name)  
877 array = xp.astype(array, dtype, copy=False)  
878 else:  
 → 879 array = \_asarray\_with\_order(array, order=order, dtype=dtype, xp=xp)  
880 except ComplexWarning as complex\_warning:  
881 raise ValueError(

/usr/local/lib/python3.10/dist-packages/sklearn/utils/\_array\_api.py in \_asarray\_with\_order(array, dtype, order, copy, xp)  
183 if xp. **name** in {“numpy”, “numpy.array\_api”}:  
184 # Use NumPy API to support order  
 → 185 array = numpy.asarray(array, order=order, dtype=dtype)  
186 return xp.asarray(array, copy=copy)  
187 else:

ValueError: setting an array element with a sequence.

I think the main problem is that I need a float as input for my function, but the beta of pymc3 has a different type. How can I convert such a value to a float so it can work with my function or any other way to make it work? The gp.predict was done on skit-learn.
