简体   繁体   中英

Python not using variable value correctly

I have written the following code. Something very weird is happening. I have 2 variables and when I print them, I get the values sums[d_index][k]=[0 0] and rewards[k]=[1] . So when I perform sums[d_index][k] = sums[d_index][k]+rewards[k] for k=0 , I should expect to get sums[d_index][k]=[1 0] . But for some absurd reason, I get sums[d_index][k]=[0.2 0] . I have no idea how on earth this is even possible. Why is this happening and how can I fix it?

I have marked the problem line with the comment #HERE!!!!

import numpy as np
import math 

e = 0.1
np.random.seed(2)

#Initializing the parameters of the bernoulli distributions randomly
p = np.random.rand(1,2)[0]
#>>>>>>>>>>> p = np.array([ 0.26363424, 0.70255294])
suboptimality_gap = np.max(p)-p
print p

powers = [1]
cumulative_regret = np.zeros((len(powers),1,10))
for round_number in range(1):
    #Initializing the arrays to store the estimate and sum of rewards, and count of each action
    estimates = np.zeros((len(powers),2))
    estimates[:,0] = np.random.binomial(1, p[0], 1)
    estimates[:,1] = np.random.binomial(1, p[1], 1)
    counts = np.ones((len(powers),2))
    sums = estimates[:]
    #Updating estimates for action at time t>K=2
    for t in range(1,10):
        rewards = np.array([np.random.binomial(1, p[0], 1),np.random.binomial(1, p[1], 1)])
        for d_index,d in enumerate([1./(t**power) for power in powers]):
            #print (np.asarray([(estimates[d_index][i]+((2*math.log(1/d))/(counts[d_index][i]))**0.5) for i in [0,1]]))
            k = np.argmax(np.asarray([(estimates[d_index][i]+((2*math.log(1/d))/(counts[d_index][i]))**0.5) for i in [0,1]]))
            counts[d_index][k] = counts[d_index][k]+1
            print "rewards=",rewards[k]
            print "sums=",sums[d_index]
            sums[d_index][k] = sums[d_index][k]+rewards[k] #HERE!!!!
            estimates[d_index] = np.true_divide(sums[d_index], counts[d_index])
            cumulative_regret[d_index][round_number][t]=cumulative_regret[d_index][round_number][t-1]+suboptimality_gap[k]
    #print counts

Output:

[ 0.4359949   0.02592623]
rewards= 0
sums= [ 0.  0.]
rewards= 0
sums= [ 0.  0.]
rewards= 0
sums= [ 0.  0.]
rewards= 0
sums= [ 0.  0.]
rewards= 0
sums= [ 0.  0.]
rewards= 0
sums= [ 0.  0.]
rewards= 1
sums= [ 0.  0.]
rewards= 1
sums= [ 0.2  0. ]
rewards= 0
sums= [ 0.2  0. ]

I apologize that my code is kind of not organized. But that is because I have been trying to debug the problem for last hour.

As mentioned in the comments of your question, sums = estimates doesn't create a new copy of your array, just a new reference pointing to the original object which can cause things to get messy. To get your desired results you can use:

sums = estimates.copy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM