0

I am doing a Python project and trying to cut down on some computational time at the start using Pandas. The code currently is:

for c1 in centres1: for c2 in centres2: if ((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2) < search_rad*search_rad: possible_commet.append([c1,c2]) 

I am trying to put centres1 and centres2 into data frames then compare each value to each other value. Would pandas help me cut some time off it? (currently 2 mins). If not how could I work around it? Thanks

1
  • 1
    Is there a specific need for a nested loop? Seems like you're looking for zip() Commented Apr 26, 2021 at 17:37

2 Answers 2

1

Unfortunately this is never going to be fast as you are going to be performing n squared operations. For example if you are comparing n objects where n = 1000 then you only have 1 million comparisons. If however you have n = 10_000 then you 100 million comparisons. A problem 10x bigger becomes 100 times slower.

nevertheless, for loops in python are relatively expensive. Using a library like pandas may mean that you can only make one function call and will shave some time off. Without any input data it's hard to assist further but the below should provide some building blocks

import pandas df1 = pandas.Dataframe(centres1) df2 = pandas.Dataframe(centres2) df3 = df1.merge(df2, how = 'cross') df3['combined_centre'] = ((df3['0_x']-df2['0_y']**2 + (df1['1_x']-df['1_y'])**2) df3[df3['prod'] > search_rad**2 
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, for sure pandas will help in cutting-off atleast some time which will be less that what you are getting right now, but you can try this out:

 for i,j in zip(center1, center2): if ((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2) < search_rad*search_rad: possible_commet.append([c1,c2]) 

3 Comments

This is so much faster already, thanks a lot :)
hi, so I tried implementing it and it ended up rejecting a load of my points, specifically those bellow 500 x
I don't think zip would work here as from what I understand from the question they are looking all possible permutations not simply the first entry from each list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.