python - Fastest way of comparing two numpy arrays -


i have 2 arrays:

>>> import numpy np >>> a=np.array([2, 1, 3, 3, 3]) >>> b=np.array([1, 2, 3, 3, 3]) 

what fastest way of comparing these 2 arrays equality of elements, regardless of order?

edit measured execution times of following functions:

def compare1():        #works arrays without redundant elements     a=np.array([1,2,3,5,4])     b=np.array([2,1,3,4,5])     temp=0     in a:         temp+=len(np.where(b==i)[0])     if temp==5:             val=true     else:             val=false     return 0  def compare2():     a=np.array([1,2,3,3,3])     b=np.array([2,1,3,3,3])     val=np.all(np.sort(a)==np.sort(b))     return 0  def compare3():                        #thx odiogosilva     a=np.array([1,2,3,3,3])     b=np.array([2,1,3,3,3])     val=set(a)==set(b)     return 0  import numpy.lib.arraysetops aso def compare4():                        #thx tom10     a=np.array([1,2,3,3,3])     b=np.array([2,1,3,3,3])     val=len(aso.setdiff1d(a,b))==0     return 0 

the results are:

>>> import timeit >>> timeit.timeit(compare1,number=1000) 0.0166780948638916 >>> timeit.timeit(compare2,number=1000) 0.016178131103515625 >>> timeit.timeit(compare3,number=1000) 0.008063077926635742 >>> timeit.timeit(compare4,number=1000) 0.03257489204406738 

seems "set"-method odiogosilva fastest.

do know other methods can test well?

edit2 runtime above not right measure comparing arrays, explained in comment user2357112.

#test.py import numpy np import numpy.lib.arraysetops aso  #without duplicates n=10000 a=np.arange(n,0,step=-2) b=np.arange(n,0,step=-2)  def compare1():     temp=0     in a:         temp+=len(np.where(b==i)[0])     if temp==len(a):         val=true     else:         val=false     return val def compare2():     val=np.all(np.sort(a)==np.sort(b))     return val def compare3():     val=set(a)==set(b)     return val def compare4():     val=len(aso.setdiff1d(a,b))==0     return val 

the output is:

>>> test import * >>> import timeit >>> timeit.timeit(compare1,number=1000) 101.16708397865295 >>> timeit.timeit(compare2,number=1000) 0.09285593032836914 >>> timeit.timeit(compare3,number=1000) 1.425955057144165 >>> timeit.timeit(compare4,number=1000) 0.44780397415161133 

now compare2 fastest. there still method outgun this?

numpy collection of set operations.

import numpy np import numpy.lib.arraysetops aso  a=np.array([2, 1, 3, 3, 3]) b=np.array([1, 2, 3, 3, 3])  print aso.setdiff1d(a, b) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -