Home > OS >  Numpy argmax source
Numpy argmax source

Time:04-20

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related