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()
Related
I have a 2D-matrix of some numbers and I want to randomly change a fraction of the non-zero members (e.x. 0.2) to become zero and then again randomly choose equal to that fraction amount (0.2) between all zeroes and give them random numbers. Is there any straight forward way to do that?
for example:
The original matrix is : x = [[1,2,3],[4,0,7],[2,10,0]]
After first step (2 randomly selected numbers change to zero): x = [[1,0,0],[4,0,7],[2,10,0]]
After second step (2 randomly selected zeros change to random numbers): x = [[1,0,5],[4,7,7],[2,10,0]]
One method:
arr = np.ones((5, 5)) # Your matrix
print("Before Replacement")
print(arr)
# Number of elements to replace
num_replaced = 3
# Random (x, y) coordinates
indices_x = np.random.randint(0, arr.shape[0], num_replaced)
indices_y = np.random.randint(0, arr.shape[1], num_replaced)
arr[indices_x, indices_y] = 0
print("After replacement")
print(arr)
Sample Output:
Before Replacement
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
After replacement
[[0. 1. 1. 1. 1.]
[1. 0. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 0. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
EDIT
You can use np.random.choice instead on np.random.randint as follows:
indices_x = np.random.choice(range(arr.shape[0]), num_replaced, replace=REPLACE)
indices_y = np.random.choice(range(arr.shape[1]), num_replaced, replace=REPLACE)
Here, you can easily switch between sampling with or without replacement.
I would try to create a simple function for this. So you can input the number desired.
import pandas as pd
import random
def random_converter(dataframe, k, isZero=True, input_data='random_value'):
# Copy df
dataframe_local = dataframe.copy()
if input_data=='random_value':
input_data = random.randint(0,10)
ki = 0
while ki < k:
row_selected = dataframe_local.sample(1).T
# VERIFY CONDITION
if isZero:
attributes = row_selected[row_selected.iloc[:, 0] == 0]
else:
attributes = row_selected[row_selected.iloc[:, 0] != 0]
# No zero in the row
if attributes.size == 0:
continue
column_index = attributes.index
row_index = attributes.columns
dataframe_local.iloc[row_index, column_index] = input_data
ki += 0
return dataframe_local
I am given this matrix and am trying to write a function to build this matrix for any size of n. I am told the height of the matrix is n, but not sure the width.
Below is my code and output, is this correct? I am slightly confused by the notation of the matrix itself.
def buildMatrix(n, a):
matrix = np.zeros([n, n], dtype=float)
x_diag, y_diag = np.diag_indices_from(matrix)
for (x,y) in zip(x_diag, y_diag):
if x > (n / 2):
matrix[x][y] = -2*a
elif x == (n / 2):
matrix[x][y] = -(1 + a)
else:
matrix[x][y] = -2
if x != n - 1:
matrix[x + 1][y] = a if x >= (n / 2) else 1
matrix[x][y + 1] = a if x >= (n / 2) else 1
return matrix
Output with buildMatrix(5, 2)
[[-2. 1. 0. 0. 0.]
[ 1. -2. 1. 0. 0.]
[ 0. 1. -3. 2. 0.]
[ 0. 0. 2. -4. 2.]
[ 0. 0. 0. 2. -4.]]
Can anyone help me out?
To answer your first question, the matrix has to have a width of n in order for the matrix-vector product to be compatible.
The picture of the matrix is ambiguous on where the switch from -2 to -(1-a) to -2a occurs. In your code, you check if x==n/2 to set the switch. This is fine in python2 but will cause problems in python3 since x/2 returns 2.5. Using safer x==n//2 since n//2 return an integer in python2 as well as python3.
For generality, I'm going to assume that the switch happens at row m. The matrix can be built easier using slicing and the np.diag command.
def buildmat(n, m, a):
diag = np.zeros(n)
offdiag = np.zeros(n-1)
offdiag[0:m] = 1
offdiag[m:n-1] = a
diag[0:m] = -2
diag[m] = -(1+a)
diag[m+1:n] = -2*a
matrix = np.diag(diag) + np.diag(offdiag, 1) + np.diag(offdiag, -1)
return matrix
Running
buildmat(5, 2, 3)
produces
[[-2. 1. 0. 0. 0.]
[ 1. -2. 1. 0. 0.]
[ 0. 1. -3. 2. 0.]
[ 0. 0. 2. -4. 2.]
[ 0. 0. 0. 2. -4.]]
'car3.csv' file download link
import csv
num = open('car3.csv')
nums = csv.reader(num)
nums_list = []
for i in nums:
nums_list.append(i)
import numpy as np
nums_arr = np.array(nums_list, dtype = np.float32)
print(nums_arr)
print(np.std(nums_arr, axis=0))
The result is this.
[[ 1. 1. 2.]
[ 1. 1. 2.]
[ 1. 1. 2.]
...,
[ 0. 0. 5.]
[ 0. 0. 5.]
[ 0. 0. 5.]]
[ 0.5 0.5 1.11803401]
There are lots of spaces that I didn't expected.
How can I handle these anyway?
That is not a spacing problem. What all you need to do is to save the output of the standard deviation. Then, you can access each value like this:
std_arr = np.std(nums_arr, axis=0) # array which holds std of each column
# now, you can access them by indexing:
print(std_arr[0]) # output here is 0.5
print(std_arr[1]) # output here is 0.5
print(std_arr[2]) # output here is 1.118034
right now I'm working on control.matlap.tf2ss and I would like to access my array in my state space.
Here is my code
Gs = tf([P.l], [P.Jzz, 0, 0])
Cs = tf([P.Kp, P.Kd], 1)
Gcl = feedback(series(Cs, Gs), 1)
po = pole(Gcl)
num, den = tfdata(Gs)
sys = tf2ss(Gs)
print sys
Result:
A = [[ 0. 0.]
[ 1. 0.]]
B = [[-10.58350385]
[ 0. ]]
C = [[ 0. -1.]]
D = [[ 0.]]
How can I access array A, B, C, D?
For arrays of state-space models with variable numbers of states, use the syntax:
[a,b,c,d] = ssdata(sys,'cell')
I'm trying to use numpy with numba but I'm getting weird results while trying to access or set some values to a numpy array of float using a float index converted to an int.
Check with this basic function.
#numba.jit("void(f8[:,::1],f8[:,::1])")
def test(table, index):
x,y = int(index[0,0]), int(index[1,0)
table[y,x] = 1.0
print index[0,0], index[1,0], x,y
print table
print table[y,x]
table = np.zeros((5,5), dtype = np.float32)
index = np.random.ranf(((2,2)))*5
test(table, index)
results:
index[0,0] = 1.34129550525 index[1,0] = 0.0656177324359 x = 1 y = 0
table[0,1] = 1.0
table [[ 0. 0. 1.875 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]]
Why do I get a 1.875 in my table and not a 1.0? This a basic example but I'm working with big array and it gives me a lot of error. I know i can convert index to np.int32 and change #numba.jit("void(f8[:,::1],f8[:,::1])") to #numba.jit("void(f8[:,::1],i4[:,::1])") and that is working fine, but I would you like ton understand why this is not working.
Is it a problem while parsing the type from python to c++?
Thanks for you help
In [198]: np.float64(1.0).view((np.float32,2))
Out[198]: array([ 0. , 1.875], dtype=float32)
So when
table[y,x] = 1.0
writes a np.float64(1.0) into table, table views the data as np.float32 and interprets it as a 0 and a 1.875.
Notice that the 0 shows up at index location [0,1], and 1.875 shows up at index location [0,2], whereas the assignment occurred at [y,x] = [0,1].
You could fix the dtype mismatch by changing
#numba.jit("void(f8[:,::1],f8[:,::1])")
to
#numba.jit("void(f4[:,::1],f8[:,::1])")
These are the 8 bytes in np.float64(1.0):
In [201]: np.float64(1.0).tostring()
Out[201]: '\x00\x00\x00\x00\x00\x00\xf0?'
And when the 4 bytes '\x00\x00\xf0?' are interpreted as a np.float32 you get 1.875:
In [205]: np.fromstring('\x00\x00\xf0?', dtype='float32')
Out[205]: array([ 1.875], dtype=float32)