Skip to content

Comments

Refactor predict() to use grouped iteration instead of O(n²) dataframe filtering#75

Open
midaa1 wants to merge 11 commits intoruxailab:mainfrom
midaa1:prediction-refactor
Open

Refactor predict() to use grouped iteration instead of O(n²) dataframe filtering#75
midaa1 wants to merge 11 commits intoruxailab:mainfrom
midaa1:prediction-refactor

Conversation

@midaa1
Copy link
Contributor

@midaa1 midaa1 commented Feb 18, 2026

Summary

This PR optimizes the data aggregation step inside predict() in this issue #74 by removing row-wise dataframe iteration and repeated filtering.

Changes Made

Replaced:

  • df_data.iterrows() loop

  • Per-row dataframe filtering based on (True X, True Y)

Added:

  • Single-pass iteration over:

  • df_data.groupby("True XY")

  • Direct extraction of grouped predictions using:

group["Predicted X"].tolist()
group["Predicted Y"].tolist()
  • Reused existing precomputed metrics:

     - precision_xy
    
     - accuracy_xy
    

Before

  • Nested logic caused repeated full-dataframe scans.

  • Complexity: O(n²).

  • Duplicate processing for identical truth coordinates.

After

  • Each (True X, True Y) group processed once.

  • Complexity reduced to O(n).

  • Cleaner and more readable logic.

  • Performance Impact

  • Significant speed improvements on larger datasets.

  • Reduced CPU usage and memory overhead.

Behavior Verification

  • Output dictionary structure remains unchanged.

  • Precision and accuracy values unchanged.

  • Filtering of negative predictions still respected.

Notes

This refactor prepares the code for better scalability and aligns the aggregation logic with pandas best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant