Skip to content

Optimize predict() from O(n²) to O(n) using pandas groupby#94

Open
BHUVANA-SANKU24 wants to merge 3 commits intoruxailab:mainfrom
BHUVANA-SANKU24:optimize/predict-groupby
Open

Optimize predict() from O(n²) to O(n) using pandas groupby#94
BHUVANA-SANKU24 wants to merge 3 commits intoruxailab:mainfrom
BHUVANA-SANKU24:optimize/predict-groupby

Conversation

@BHUVANA-SANKU24
Copy link
Copy Markdown

🚀 Optimization: Reduce predict() complexity from O(n²) to O(n)

Fixes #74

Problem

The existing implementation iterates over each row and repeatedly filters the dataframe, resulting in O(n²) time complexity and poor scalability.

Solution

Replaced the nested iteration with pandas groupby(["True X", "True Y"]), ensuring each group is processed exactly once.

Improvements

  • Reduced effective time complexity from repeated filtering (O(n²)) to grouped processing (O(n))
  • Eliminated redundant dataframe filtering
  • Improved readability and maintainability

Additional Fix

  • Fixed indentation issue that caused incorrect dictionary population

Testing

  • Successfully tested /api/session/batch_predict endpoint using Postman
  • Verified correct JSON response
  • No regressions observed
Screenshot 2026-03-22 at 10 09 35 AM

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.

Optimize predict() to remove O(n²) dataframe iteration and filtering

1 participant