I am trying to create a record array with specific values. However, I noticed that the values of the record array cannot be set correctly. The record array has three fields: "startstate" (a scalar), "action" (a 4x1 array), and "transition" (a 4x2 array). "startstate" can be set easily. For the other two fields, however, when I try to set their values using another array, it seems that only the first element can be set appropriately. The following code illustrates the issue
import numpy as np
NUM_STATES = 4
NUM_ACTIONS = 2
NUM_OBS = 2
strategy_id = 62605133
new_auto, = np.zeros(1,dtype = [('startstate', np.int32),
('action', np.int32, NUM_STATES), ('transition', np.int32, (NUM_STATES, NUM_OBS))])
start_id = strategy_id%10
action_id = np.int(strategy_id/10)%100
transition_id = np.int(strategy_id/1000)
new_auto['startstate'] = start_id
print new_auto['startstate'],start_id
action_array = new_auto['action'].copy()
for i in xrange(NUM_STATES):
action_array[-1-i] = action_id%NUM_ACTIONS
action_id = np.int(action_id/NUM_ACTIONS)
new_auto['action'] = action_array.copy()
print new_auto['action']
print action_array
transition_array = new_auto['transition'].flatten().copy()
for i in xrange(NUM_STATES*2):
transition_array[-1-i] = transition_id%NUM_STATES
transition_id = np.int(transition_id/NUM_STATES)
new_auto['transition'] = transition_array.reshape((NUM_STATES,NUM_OBS)).copy()
print new_auto['transition']
print transition_array.reshape((NUM_STATES,NUM_OBS))
The output of the code:
startstate
3 3
actions
[1 0 0 0]
[1 1 0 1]
transitions
[[3 0]
[0 0]
[0 0]
[0 0]]
[[3 3]
[1 0]
[2 0]
[3 1]]
For each field, the first array gives the actual values, while the second array gives the correct values. In theory, they are supposed to be the same because I have already set the values using the following:
new_auto['action'] = action_array.copy()
new_auto['transition'] = transition_array.reshape((NUM_STATES,NUM_OBS)).copy()
Could anyone help identify what caused this issue? The numpy verison I am using is 1.9.2
3 3 [1 1 0 1] [1 1 0 1] [[3 3] [1 0] [2 0] [3 1]] [[3 3] [1 0] [2 0] [3 1]]
– pmaniyan May 16 '16 at 08:35