1

I have built a program that takes values out of one dataframe, and use these as an input for another dataframe. df_coordinate is a dataframe with rows of x and y coordinates (with sizes between 0 and max_size_x or max_size_y). updated_coordinates is a new dataframe with the size of the screen, that uses the coordinates and a sort of Euclidean distance to make the coordinates represent an area instead of a pixel. The code works as I intend, but right now it is way to slow. I know that vectorizing is much faster, and I try to implement it as much as possible. However, I can't seem to find a way how to vectorize with formulas where the column and row index is used. With .apply I use x.name and x.index as you can see, but is there a way to implement this faster?

max_size_x = 1080
max_size_y = 720

    for index, row in df_coordinate.iterrows():
        updated_coordinates = pd.DataFrame(np.zeros((max_size_x, max_size_y))) # to do: maybe empty instead of zeroes, and already delete the ones from attention_max
        current_time = df_coordinate.loc[index]
        coordinate_x = current_time['x_coordinate']
        coordinate_y = current_time['y_coordinate']

        # calculate area with Euclidean distance:
        updated_coordinates = updated_coordinates.apply(lambda x: 1/ ( np.power((np.sqrt(np.power(x.name-coordinate_x,2) + np.power(x.index-coordinate_y,2))),2)))
0

1 Answer 1

1

I found the answer, by adding this outside of the loop:

df_x = pd.DataFrame(np.zeros((image_size_x, image_size_y))) 
df_x = df_x.apply(lambda x: x.name) 
df_y = pd.DataFrame(np.zeros((image_size_x, image_size_y))) 
df_y = df_y.apply(lambda x: x.index) 

And replace the .apply function with the vectors:

updated_coordinates = 1/ ( * np.power((np.sqrt(np.power(df_x - gaze_x,2) + np.power(df_y - gaze_y,2))),2))

I've checked and this runs over ten times as fast.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.