1

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

user3821012
  • 1,291
  • 2
  • 16
  • 27
  • Can you please check your output? I am getting a different output when I run your code. The output i have is
    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
  • I am certainly positive on the output I received. What version of numpy do you use? I guess it is another bug of numpy. – user3821012 May 16 '16 at 09:22
  • Hmm, I use numpy 1.10.4. So, all this problem could be due to Numpy bug? – pmaniyan May 16 '16 at 09:28
  • Yes. I just updated my numpy to 1.11.0, and the output is as expected. Thanks @pmaniyan for pointing this out. – user3821012 May 16 '16 at 09:35
  • 1
    Possible duplicate of [Assigning to numpy structured arrays](http://stackoverflow.com/questions/28431747/assigning-to-numpy-structured-arrays) – Ilja Everilä May 16 '16 at 09:38

1 Answers1

0

It seems that this is due to a bug in numpy. After updating the version from 1.9.2 to 1.11.0, the output is as expected.

user3821012
  • 1,291
  • 2
  • 16
  • 27