python-novice-inflammation
python-novice-inflammation copied to clipboard
Delete numpy.delete
Hi everybody,
While reading through Episode 2, I stumbled across the use of numpy.delete
. From my experience, I have not a single examples where the usage of numpy.delete
is justified because it creates a new array instead of returning a view.
In the best case, the performance penalty does not matter and you never look at your code again and think "Hey, that element was deleted from the original array. So, it won't be there any more." Well, it is! You are bound to get surprised at least once by the fact that your original array didn't change at all and only if you are very lucky, that will not translate into a hard to track bug in your code where the numbers are slightly off all the time.
In the worst case, an inexperienced python user writes a for
-loop to delete all the elements they don't want instead of using a mask or proper slicing to return a view. If you do that on any reasonably sized data, you will immediately abandon python
for being almost as slow as doing it by hand or maybe even run into memory issues with multiple copies of large data in memory. And then again, they might forget to assign the created copy (as opposed to an in-place change) back to the original variable and all that.
So, I would be very interested in hearing about justifications I might have overlooked. But for the time being, IMHO the best that can be done is removing that part or, if someone insists on mentioning numpy.delete
, clearly state its pitfalls instead and recommend not to use it unless you really, really know what you are doing.
Best, Julian
PS: I came up with one kind of reasonable scenario for using numpy.delete
: Functional programming! From a purist's perspective, a function should have no side effects there and that is what numpy.delete
achieves in returning a copy. If it is very (num)pythonic to use pure functional programming is to be decided by others.