1

I can't seem to find the code for numpy argmax.

The source link in the docs lead me to here, which doesn't have any actual code.

I went through every function that mentions argmax using the github search tool and still no luck. I'm sure I'm missing something.

Can someone lead me in the right direction?

Thanks

akore128
  • 69
  • 8
  • Much of numpy is written is C, I bet you won't find it in the Python code, but in the C code instead. – joanis Apr 19 '22 at 19:21
  • It looks to me like the function you found is the Python wrapper around the C function. – joanis Apr 19 '22 at 19:23
  • 1
    I think this might be it: https://github.com/numpy/numpy/blob/main/numpy/core/src/multiarray/calculation.c Function _PyArray_ArgMinMaxCommon seems to be where the nitty-gritty stuff happens. – joanis Apr 19 '22 at 19:27
  • 1
    What were you hoping to learn from this code? You'd have to be pretty proficient in C to get much from reading @joanis link. If I have questions about behavior of a function/method, I usually just test it with a variety of inputs. – hpaulj Apr 19 '22 at 19:54
  • @hpaulj Excellent point. I never look at the underlying C code, it's usually too cryptic to be useful. It wasn't easy to find, either. – joanis Apr 19 '22 at 19:57
  • I was hoping to add to the method as per an issue created on github. It was listed as a good beginner challenge. The idea was to add a parameter for getting the top k indices. – akore128 Apr 19 '22 at 20:58

2 Answers2

2

Numpy is written in C. It uses a template engine that parsing some comments to generate many versions of the same generic function (typically for many different types). This tool is very helpful to generate fast code since the C language does not provide (proper) templates unlike C++ for example. However, it also make the code more cryptic than necessary since the name of the function is often generated. For example, generic functions names can look like @TYPE@_@OP@ where @TYPE@ and @OP@ are two macros that can take different values each. On top of all of this, the CPython binding also make the code more complex since C functions have to be wrapped to be called from a CPython code with complex arrays (possibly with a high amount of dimensions and custom user types) and CPython arguments to decode.

_PyArray_ArgMinMaxCommon is a quite good entry point but it is only a wrapping function and not the main computing one. It is only useful if you plan to change the prototype of the Numpy function from Python.

The main computational function can be found here. The comment just above the function is the one used to generate the variants of the functions (eg. CDOUBLE_argmax). Note that there are some alternative specific implementation for alternative type below the main one like OBJECT_argmax since CPython objects and strings must be computed a bit differently. Thank you for contributing to Numpy.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
1

As mentioned in the comments, you'll likely find what you are searching in the C code implementation (here under _PyArray_ArgMinMaxCommon). The code itself can be very convoluted, so if your intent was to open an issue on numpy with a broad idea, I would do it on the page you linked anyway.

rikyeah
  • 1,896
  • 4
  • 11
  • 21