Tom Ritchford
2 min readDec 23, 2020

--

Yes.

My classic example is "zero crossings" - find the points where the signal transitions through zero. This is useful for matching curves and e.g. preventing clicks in audio data.

The trouble is that .index() just won't work in that situation, because the signal might well jump from -1 to +1 without going through zero. And that's assuming of course that you have an integer signal and not floating point — comparing arbitrary floating point numbers for equality is a bad idea.

In fact, I claim that equality-based search is almost always wrong in numerical calculations. It's easy to come up with toy examples of using .index(), but I was unable to come up with any real world examples of its use.

Your threshold example isn't it, unless you are given some artificial guarantee of continuity (and even so, I think requiring exact equality makes the code brittle in a threshold calculation).

I'll bet there is some useful example, but I can't see it.

Now, np.searchsorted() is a completely different animal. It does not use equality but order comparison! You can see that, for example, it handles the "-1 to +1" issue without any problems, and works perfectly well with floating point arrays. (Of course, np.searchsorted() is no good for zero-crossings because the data has to be sorted.)

One of the things you quickly notice on any question answering site like SO is that questions that beginners might ask are hugely upvoted. This is not a measure of their quality but simply the very high beginner to expert ratio. The question you cite is one of those, perhaps?

I believe reasoning like the above is probably why the numpy team has chosen for the last eight years not to spend the small amount of time it would take to add an .index() operation — well, for an expert in the codebase once the hours of discussion about the details of the API were finished, perhaps two hours to write, four hours to test and validate and eight hours to document, but still it's a minor feature.

And I'm still thinking of some real use for np.ndarray.index()!

The array type has to be integer of course. I was thinking about "finding the inverse of an element in a permutation" but if I did that more than once, a linear search would be stupid. Any ideas?

Thanks for the thought-provoking comment!

--

--

No responses yet