1

I am running the following code where the function weighted_values returns a sequence of random values with probabilities as specified. I am using this function from this answer Generating discrete random variables with weights

The following is my code:

def weighted_values(values, probabilities, size):
    bins = np.add.accumulate(probabilities)
    return np.array(values[np.digitize(random_sample(size), bins)])

def weak_softmax(a):
    b=np.exp(a)
    return b/(1+sum(b))


 elements=np.array([1,2,3])
 prob=np.array([0.2,0.5,0.3])


 system_index=0;
 T=10;M=2;

for t in np.arange(T):

     prob=weak_softmax(np.random.uniform(0,1,M+1));

     system_index=weighted_values(np.arange(M+1),prob,1)[0]

print(system_index)

However when I run this code, sometimes I get this error that

 Traceback (most recent call last):
 File "gradient_checking.py", line 75, in <module>
    system_index=weighted_values(np.arange(M+1),prob,1)[0]
 File "gradient_checking.py", line 57, in weighted_values
    return np.array(values[np.digitize(random_sample(size), bins)])
 IndexError: index 3 is out of bounds for axis 1 with size 3

Can anyone suggest what I am doing wrong and how to modify it?

Community
  • 1
  • 1
pikachuchameleon
  • 665
  • 3
  • 10
  • 27
  • 1
    Can you post your entire code along with the complete error and the line on which it is thrown? What is `random_sample(...)` doing? – cs95 Jan 15 '17 at 19:36
  • @Shiva: random_sample apparently generates uniform random numbers between 0 and 1 as explained in the link to the earlier answer. – pikachuchameleon Jan 15 '17 at 19:45

2 Answers2

8

The error tells me that you have an array with shape (n,3) (axis 1 size 3), and that you trying to index it with 3

In [9]: np.ones((5,3))[:,3]
... 
IndexError: index 3 is out of bounds for axis 1 with size 3

In the problem statement:

values[np.digitize(random_sample(size), bins)]

I'd suggest checking the shape of values. Off hand it looks like it is np.arange(M+1) where M is 2. That's size 3, but 1d.

Also what does np.digitize(random_sample(size), bins) produce?

When you have errors like this you need to check the shape of suspected arrays, and check the range of values of the indices. We can only guess so much from just reading your code.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

This is caused because Python (unlike R) is zero-based. That means if you have three elements their indexes are 0,1,2 not 1,2,3. So if you are trying to reference "3" it will be pulling the fourth element from the array not the third (because zero is the first)

Edward
  • 21
  • 1