# Index out of bouds

**URL:** <https://discourse.pymc.io/t/index-out-of-bouds/7003>\
**Category:** Questions\
**Tags:** theano\
**Created:** [March 16, 2021, 5:30am UTC](https://discourse.pymc.io/t/index-out-of-bouds/7003 "2021-03-16T05:30:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dubushuo](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Dubushuo](https://discourse.pymc.io/u/Dubushuo)\
**Post date:** [March 16, 2021, 5:30am UTC](https://discourse.pymc.io/t/index-out-of-bouds/7003/1 "2021-03-16T05:30:06Z")

</div>

I have been experiencing frustrating model specification “index out of bounds” error on specifying a Hierarchy logistic model.

I did follow the specification Junpenglao indicated on this question [Prediction using sample\_ppc in Hierarchical model - Questions - PyMC Discourse](https://discourse.pymc.io/t/prediction-using-sample-ppc-in-hierarchical-model/116). However, it did not work at my case.

Here is my code:

X\_train\_hier, X\_test\_hier, y\_train\_hier, y\_test\_hier = train\_test\_split(X\_hier, y, test\_size=.15, random\_state=seed)

X\_idx = theano.shared(np.asarray(X\_train\_hier.uid))

X\_user = len(np.unique(np.asarray(X.uid)))

X\_train\_hier = X\_train\_hier.drop(“uid”, axis=1)

X\_len = len(X\_train\_hier.keys())

X\_data = theano.shared(X\_train\_hier)

with pm.Model() as hier:  
#hyperpriors:  
mu\_a = pm.Normal(“mu\_a”, 0.0, 1e4)  
sigma\_a = pm.Exponential(“sigma\_a”, 1e4)  
mu\_b = pm.Normal(“mu\_b”, 0.0, 1e4)  
sigma\_b = pm.Exponential(“sigma\_b”, 1e4)

```
#intercept prior for each user
a = pm.Normal("a", mu_a, sigma_a, shape=X_user)

#coefficient prior for each user
b = pm.Normal("b", mu_b, sigma_b, shape=(X_user, X_len))

#likelihood
theta = pm.Deterministic("theta", pm.invlogit(a[X_idx] + b[X_idx] * X_data.T))

#observation from sigmoid
obs = pm.Bernoulli(name="AT", p=theta, observed=y_train_hier)

```

* * *

## And here is the error message I had:

IndexError Traceback (most recent call last)  
 in   
14  
15 #likelihood  
—\> 16 theta = pm.Deterministic(“theta”, pm.invlogit(b[X\_idx] \* X\_data.T))  
17  
18 #observation from sigmoid

~\AppData\Roaming\Python\Python38\site-packages\theano\tensor\var.py in **getitem** (self, args)  
572 # `take` function/`Op` serves exactly this type of indexing,  
573 # so we simply return its result.  
 → 574 return self.take(args[axis], axis)  
575 else:  
576 return theano.tensor.subtensor.advanced\_subtensor(self, \*args)

~\AppData\Roaming\Python\Python38\site-packages\theano\tensor\var.py in take(self, indices, axis, mode)  
621  
622 def take(self, indices, axis=None, mode=“raise”):  
 → 623 return theano.tensor.subtensor.take(self, indices, axis, mode)  
624  
625 def copy(self, name=None):

~\AppData\Roaming\Python\Python38\site-packages\theano\tensor\subtensor.py in take(a, indices, axis, mode)  
2522 return advanced\_subtensor1(a.flatten(), indices)  
2523 elif axis == 0:  
 → 2524 return advanced\_subtensor1(a, indices)  
2525 else:  
2526 if axis \< 0:

~\AppData\Roaming\Python\Python38\site-packages\theano\graph\op.py in **call** (self, \*inputs, \*\*kwargs)  
251  
252 if config.compute\_test\_value != “off”:  
 → 253 compute\_test\_value(node)  
254  
255 if self.default\_output is not None:

~\AppData\Roaming\Python\Python38\site-packages\theano\graph\op.py in compute\_test\_value(node)  
128 thunk.outputs = [storage\_map[v] for v in node.outputs]  
129  
 → 130 required = thunk()  
131 assert not required # We provided all inputs  
132

~\AppData\Roaming\Python\Python38\site-packages\theano\graph\op.py in rval()  
604  
605 def rval():  
 → 606 thunk()  
607 for o in node.outputs:  
608 compute\_map[o][0] = True

~\AppData\Roaming\Python\Python38\site-packages\theano\link\c\basic.py in **call** (self)  
1769 print(self.error\_storage, file=sys.stderr)  
1770 raise  
 → 1771 raise exc\_value.with\_traceback(exc\_trace)  
1772  
1773

IndexError: index 7420 is out of bounds for axis 0 with size 7420

* * *

I had played around with the model, the problem seemed to be at X\_idx. However, I used the exact method to assign X\_idx as Junpenglao did at [Prediction using sample\_ppc in Hierarchical model - Questions - PyMC Discourse](https://discourse.pymc.io/t/prediction-using-sample-ppc-in-hierarchical-model/116). I tried the sample code from the above question and it worked, but on own dataset it does not.

I am sorry I cannot upload a part of my data because of confidentiality issue, however, I can provide the dimensions of X\_idx (70345, which is the number of rows of this training dataset), X\_user (7420, which is the number of rows of uid for the entire dataset).

I am stuck on this for days. I could really appreciate some help! @junpenglao

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [March 16, 2021, 6:28am UTC](https://discourse.pymc.io/t/index-out-of-bouds/7003/2 "2021-03-16T06:28:51Z")

</div>

I would guess your variable `X.uid` is not appropriate for indexing. It should be in the range (0, X\_user)

---

<div class="post-metadata">

**Author:** ![Dubushuo](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@Dubushuo](https://discourse.pymc.io/u/Dubushuo)\
**Post date:** [March 16, 2021, 2:20pm UTC](https://discourse.pymc.io/t/index-out-of-bouds/7003/3 "2021-03-16T14:20:12Z")

</div>

Thank you very much. That is the case here. I process the data in R so I forgot that the index should start from 0 in Python!
