Monday, 15 April 2013

python find matches between two list of n by 1 numpy arrays -



python find matches between two list of n by 1 numpy arrays -

it's been while looking solution of probleme of type: instance(because real problem more complex) :

import numpy a=[numpy.array([1,2]),numpy.array([2,2]),numpy.array([3,2]),numpy.array([4,2])] b=[numpy.array([2,2]),numpy.array([3,2]),numpy.array([6,2]),numpy.array([5,2]),numpy.array([5,2])] ya=numpy.array([1,2,3,4]) size_a=len(a) size_b=len(b) yb=numpy.empty((size_b,1)) yb.fill(numpy.nan) in xrange(size_b): j in xrange(size_a): if numpy.array_equiv(yb,ya): ya[i]=yb[j]

i want fill yb ya value of index of element matching 1 element of b. yb longer ya, it's normal yb contain "nan" @ end of loops. code below takes long time proceed. in fact don't know if works because hadn't wait end of loops...

in real case, ya , yb longer: 7007 , 3525

is there way accomplish goal?

to find matches between lists of arrays, straightforward method broadcast lists same n x m shape; can done np.tile using stride_tricks faster:

a = np.array(a) b = np.array(b) shape = (2, a.shape[0], b.shape[0]) numpy.lib.stride_tricks import as_strided = as_strided(a, shape=shape, strides=(a.strides[1], a.strides[0], 0)) b = as_strided(b, shape=shape, strides=(b.strides[1], 0, b.strides[0])) np.where(np.all(a == b, axis=0))

this gives result

(array([1, 2]), array([0, 1]))

i.e. a[1] == b[0], , a[2] == b[1], no other matches.

python numpy

No comments:

Post a Comment