Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make this kind of equality array fast (in numpy)?

Tags:

python

numpy

I have two numpy array (2 dimensional) e.g.

a1 = array([["a","b"],["a","c"],["b","b"],["a","b"]])
a2 = array([["a","b"],["b","b"],["c","a"],["a","c"]])

What is the most elegant way of getting a matrix like this:

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

Where element (i,j) is 1 if all(a1[i,:] == a2[j,:]) and otherwise 0

(everything involving two for loops I don't consider elegant)

like image 516
Peter Smit Avatar asked Dec 02 '11 13:12

Peter Smit


People also ask

How can I make NumPy array faster?

The key to making it fast is to use vectorized operations, generally implemented through NumPy's universal functions (ufuncs). This section motivates the need for NumPy's ufuncs, which can be used to make repeated calculations on array elements much more efficient.

Are NumPy arrays faster?

NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types.

Is appending to NumPy array faster than list?

NumPy Arrays Are NOT Always Faster Than Lists " append() " adds values to the end of both lists and NumPy arrays. It is a common and very often used function. The script below demonstrates a comparison between the lists' append() and NumPy's append() .

How do you compare two NumPy arrays are equal?

Method 1: We generally use the == operator to compare two NumPy arrays to generate a new array object. Call ndarray. all() with the new array object as ndarray to return True if the two NumPy arrays are equivalent.


1 Answers

>>> (a1[:,numpy.newaxis] == a2).all(axis=2)
array([[ True, False, False, False],
       [False, False, False,  True],
       [False,  True, False, False],
       [ True, False, False, False]], dtype=bool)

If you really need integers, convert to int as last step:

>>> (a1[:,numpy.newaxis] == a2).all(axis=2).astype(int)
array([[1, 0, 0, 0],
       [0, 0, 0, 1],
       [0, 1, 0, 0],
       [1, 0, 0, 0]])
like image 73
Sven Marnach Avatar answered Sep 22 '22 21:09

Sven Marnach