arrays - Equality of copy.copy and copy.deepcopy in python copy module -


i creating list of numpy arrays copying array keep original copy. copying done using deepcopy() function. when comparing 2 arrays now, showing false in equivalence. when using copy() function .i understand difference between copy , deepcopy function, shall equivalence not same?

that is:

 grid1=np.empty([3,3],dtype=object)  in xrange(3):     j in xrange(3):         grid1[i][j] = [i,np.random.uniform(-3.5,3.5,(3,3))]    grid_init=[]  grid_init=copy.deepcopy(grid1)  grid1==grid_init      #returns false   grid_init=[]  grid_init=copy.copy(grid1)  grid1==grid_init      #returns true   grid_init=[]  grid_init=copy.deepcopy(grid1)  np.array_equal(grid1,grid_init)      #returns false 

shall not true?

i must running different version of numpy/python, different errors and/or results. still same issue applies - mixing arrays , lists can produce complicated results.

make 2 copies

in [217]: x=copy.copy(grid1) in [218]: y=copy.deepcopy(grid1) 

equality shallow copy, gives element element comparison, 3x3 boolean:

in [219]: x==grid1 out[219]:  array([[ true,  true,  true],        [ true,  true,  true],        [ true,  true,  true]], dtype=bool) 

the elements 2 item lists:

in [220]: grid1[0,0] out[220]:  [0, array([[ 2.08833787, -0.24595155, -3.15694342],         [-3.05157909,  1.83814619, -0.78387624],         [ 1.70892355, -0.87361521, -0.83255383]])] 

and in shallow copy, list ids same. 2 arrays have different data buffers (x not view), both point same list objects (located else in memeory).

in [221]: id(grid1[0,0]) out[221]: 2958477004 in [222]: id(x[0,0]) out[222]: 2958477004 

with same id lists equal (they satisfy is test).

in [234]: grid1[0,0]==x[0,0] out[234]: true 

but == deepcopy produces simple false. no element element comparison here. i'm not sure why. maybe area in numpy undergoing development.

in [223]: y==grid1 out[223]: false 

note deepcopy element ids different:

in [229]: id(y[0,0]) out[229]: 2957009900 

when try apply == element of these arrays error:

in [235]: grid1[0,0]==y[0,0] ... valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all() 

this error comes repeatedly in questions, because people try use boolean array (from comparison) in scalar python context.

i can compare arrays in lists:

in [236]: grid1[0,0][1]==y[0,0][1] out[236]:  array([[ true,  true,  true],        [ true,  true,  true],        [ true,  true,  true]], dtype=bool) 

i can reproduce valueerror simpler comparison - 2 lists, contain array. on surface same, because arrays have different ids, fails.

in [239]: [0,np.arange(3)]==[0,np.arange(3)] ... valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all() 

this pair of comparisons shows going on:

in [242]: [0,np.arange(3)][0]==[0,np.arange(3)][0] out[242]: true in [243]: [0,np.arange(3)][1]==[0,np.arange(3)][1] out[243]: array([ true,  true,  true], dtype=bool) 

python compares respective elements of lists, , tries perform logical operation combine them, all(). can't perform all on [true, array([true,true,true])].

so in version, y==grid1 returns false because element element comparisons return valueerrors. it's either or raise error or warning. aren't equal.

in sum, array of lists of number , array, equality tests end mixing array operations , list operations. outcomes logical, complicated. have keenly aware of how arrays compared, , how lists compared. not interchangeable.

a structured array

you put data in structured array, dtype like

 dt = np.dtype([('f0',int),('f1',float,(3,3))])  in [263]: dt = np.dtype([('f0',int),('f1',float,(3,3))]) in [264]: grid2=np.empty([3,3],dtype=dt) in [265]: in range(3):         j in range(3):                 grid2[i][j] = (i,np.random.uniform(-3.5,3.5,(3,3)))    .....:          in [266]: grid2 out[266]:  array([[ (0,      [[2.719807845330254, -0.6379512247418969, -0.02567206509563602],      [0.9585030371031278, -1.0042751112999135, -2.7805349057485946],       [-2.244526250770717, 0.5740647379258945, 0.29076071288760574]]), ....]])]],        dtype=[('f0', '<i4'), ('f1', '<f8', (3, 3))]) 

the first field, integers can fetched (giving 3x3 array)

in [267]: grid2['f0'] out[267]:  array([[0, 0, 0],        [1, 1, 1],        [2, 2, 2]]) 

the second field contains 3x3 arrays, when accessed field name 4d array:

in [269]: grid2['f1'].shape out[269]: (3, 3, 3, 3) 

a single element record (or tuple),

in [270]: grid2[2,1] out[270]: (2, [[1.6236266210555836, -2.7383730706629636, -0.46604477485902374], [-2.781740733659544, 0.7822732671353201, 3.0054266762730473], [3.3135671425199824, -2.7466097112667103, -0.15205961855874406]]) 

now both kinds of copy produce same thing:

in [271]: x=copy.copy(grid2) in [272]: y=copy.deepcopy(grid2) in [273]: x==grid2 out[273]:  array([[ true,  true,  true],        [ true,  true,  true],        [ true,  true,  true]], dtype=bool) in [274]: y==grid2 out[274]:  array([[ true,  true,  true],        [ true,  true,  true],        [ true,  true,  true]], dtype=bool) 

since grid2 pure ndarray (no intermediate lists) suspect copy.copy , copy.deepcopy end using grid2.copy(). in numpy use array copy method, , don't bother copy module.

p.s. appears dtype=object, grid1.copy() same copy.copy(grid1) - new array, same object pointers (i.e. same data).


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -