3

I need to compare a bunch of numpy arrays with different dimensions, say:

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

How can I do this if I do not know either the shape of a and b, besides that

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

and neither do I know which dimension to skip from b. I'd like to use np.index_exp, but that does not seem to help me ...

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

Edit Or to put it otherwise, I wan't to construct slicing if I know the shape of the array and the dimension I want to miss. How do I dynamically create the np.index_exp, if I know the number of dimensions and positions, where to put ":" and where to put "0".

kakk11
  • 898
  • 8
  • 21

1 Answers1

3

I was just looking at the code for apply_along_axis and apply_over_axis, studying how they construct indexing objects.

Lets make a 4d array:

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

Make a list of slices (using list * replicate)

In [356]: ind=[slice(None)]*b.ndim

In [357]: b[ind].shape    # same as b[:,:,:,:]
Out[357]: (2, 3, 4, 3)

In [358]: ind[2]=2     # replace one slice with index

In [359]: b[ind].shape   # a slice, indexing on the third dim
Out[359]: (2, 3, 3)

Or with your 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 is basically the same as np.take, but the same idea can be extended to other cases.

I don't quite follow your questions about the use of :. Note that when building an indexing list I use slice(None). The interpreter translates all indexing : into slice objects: [start:stop:step] => slice(start, stop, step).

Usually you don't need to use a[:]==b[0]; a==b[0] is sufficient. With lists alist[:] makes a copy, with arrays it does nothing (unless used on the RHS, a[:]=...).

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thanks a lot. Looks like I was unaware of the "slice()" construction, with this I can indeed construct any kind and any dimension indexes, perfect! – kakk11 Jul 05 '16 at 06:55