Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider replacing numpy.delete() with numpy.take() to avoid unnecessary copying #1220

Open
dbsi-pinkman opened this issue Mar 18, 2025 · 0 comments

Comments

@dbsi-pinkman
Copy link

self._features = numpy.delete(self._features, index, axis=0)

In the line:
self._features = numpy.delete(self._features, index, axis=0)
it is recommended to consider using numpy.take() as an alternative. While numpy.delete() is convenient, it creates a new array and copies data internally, which can introduce unnecessary memory and performance overhead — especially when used in performance-critical or high-frequency operations.

Since the goal here is to exclude a specific row, the same effect can be achieved more efficiently using numpy.take() with an explicit index list of the rows to keep. This avoids the overhead of internal deletion logic and gives more control over index management.

Suggested replacement:
self._features = numpy.take(self._features, [i for i in range(self._features.shape[0]) if i != index], axis=0)
This provides the same functionality while offering better performance characteristics in larger datasets.

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

No branches or pull requests

1 participant