Numpy index array of unknown dimensions? -


i need compare bunch of numpy arrays different dimensions, say:

a = np.array([1,2,3]) b = np.array([1,2,3],[4,5,6]) assert(a == b[0]) 

how can if not know either shape of , b, besides

len(shape(a)) == len(shape(b)) - 1  

and neither know dimension skip b. i'd use np.index_exp, not seem me ...

def compare_arrays(a,b,skip_row):     u = np.index_exp[ ... ]     assert(a[:] == b[u]) 

edit or put otherwise, wan't construct slicing if know shape of array , dimension want miss. how dynamically create np.index_exp, if know number of dimensions , positions, put ":" , put "0".

i looking @ code apply_along_axis , apply_over_axis, studying how construct indexing objects.

lets make 4d array:

in [355]: b=np.ones((2,3,4,3),int) 

make list of slices (using list * replicate)

in [356]: ind=[slice(none)]*b.ndim  in [357]: b[ind].shape    # same b[:,:,:,:] out[357]: (2, 3, 4, 3)  in [358]: ind[2]=2     # replace 1 slice index  in [359]: b[ind].shape   # slice, indexing on third dim out[359]: (2, 3, 3) 

or example

in [361]: b = np.array([1,2,3],[4,5,6])   # missing [] ... typeerror: data type not understood  in [362]: b = np.array([[1,2,3],[4,5,6]])  in [366]: ind=[slice(none)]*b.ndim     in [367]: ind[0]=0 in [368]: a==b[ind] out[368]: array([ true,  true,  true], dtype=bool) 

this indexing same np.take, same idea can extended other cases.

i don't quite follow questions use of :. note when building indexing list use slice(none). interpreter translates indexing : slice objects: [start:stop:step] => slice(start, stop, step).

usually don't need use a[:]==b[0]; a==b[0] sufficient. lists alist[:] makes copy, arrays nothing (unless used on rhs, a[:]=...).


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -