2

Is there a way to better understand the logic behind a function instead of just using it/try an error or referring to doc? eg tolist()

I've searched on google and find no source code that I can refer to or recreate. I would like to understand the basic building block of some function. Or any better suggestion to understand 3rd party function/package?

1
  • 2
    tolist is compiled, so finding the code will be hard, and following all details even harder,, Commented Feb 7, 2019 at 8:29

4 Answers 4

5

Andrew Bowling is right, the source code is on github.com/numpy/numpy. However, there are simpler ways.

For example, let's say I want to find the implementation of numpy.cumsum:

  1. Google for it. You will find the documentation page
  2. Click on [source] on the documentation page.
  3. Keep in mind that the implementation might be WAY more complicated than reading the docs. Big parts are written in .

Another approach is to clone the repository and use grep.

$ grep -rnil "cumsum" * --exclude-dir=doc/ --exclude-dir=benchmarks/
numpy/random/mtrand/mtrand.pyx
numpy/lib/recfunctions.py
numpy/lib/_iotools.py
numpy/lib/shape_base.py
numpy/lib/histograms.py
numpy/lib/arraysetops.py
numpy/lib/function_base.py
numpy/lib/nanfunctions.py
numpy/lib/tests/test_function_base.py
numpy/lib/tests/test_nanfunctions.py
numpy/lib/info.py
numpy/matrixlib/tests/test_interaction.py
numpy/core/include/numpy/ufuncobject.h
numpy/core/src/multiarray/methods.c
numpy/core/src/multiarray/calculation.c
numpy/core/src/multiarray/calculation.h
numpy/core/src/multiarray/scalartypes.c.src
numpy/core/src/umath/_umath_tests.c.src
numpy/core/fromnumeric.py
numpy/core/code_generators/numpy_api.py
numpy/core/tests/test_ufunc.py
numpy/core/tests/test_regression.py
numpy/core/info.py
numpy/core/_add_newdocs.py
numpy/ma/core.py
numpy/ma/API_CHANGES.txt
numpy/ma/README.txt
numpy/ma/tests/test_core.py
numpy/ma/tests/test_old_ma.py

But please: If you are a beginner and you can't find the explanation in the docs - just ask. That is way faster. Give good examples what confuses you, put effort in phrasing the question. Going down the rabbit hole in checking the implementation of numpy is only worth it if your question is VERY specific and if StackOverflow / a Github issue on numpy / scipy can't help. The community for numpy/scipy is very good.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I was confuse of all the file on github numpy repository. Am still new to programming as well
No its not that I am unable to find a good explanation on how to use the function in the docs. I wanted to learn how do they implement the code to solve that particular problem. My thinking is it's much beneficial to understand the basic building block, than to blindly use it.
Ok. Then the way above might help. But my guess is that you will be stuck at some point. I would rather take one example function, ask early for help there and then go into details yourself. The best way to ask for an "easy learning path" would be scipy.org/scipylib/mailing-lists.html - people are super knowledgable there and friendly.
I can't seem to find the [source] links in the web documentation for v1.19. E.g. for random.random_sample. Have they been removed?
2

I believe the Numpy Github repository has a lot of the source code.

Comments

1

Use inspect library:

>>> import inspect
>>> print(inspect.getsource(np.cumsum))
def cumsum(a, axis=None, dtype=None, out=None):
    """
    Return the cumulative sum of the elements along a given axis.

    Parameters
    ----------
    a : array_like
        Input array.
    axis : int, optional
        Axis along which the cumulative sum is computed. The default
        (None) is to compute the cumsum over the flattened array.
    dtype : dtype, optional
        Type of the returned array and of the accumulator in which the
        elements are summed.  If `dtype` is not specified, it defaults
        to the dtype of `a`, unless `a` has an integer dtype with a
        precision less than that of the default platform integer.  In
        that case, the default platform integer is used.
    out : ndarray, optional
        Alternative output array in which to place the result. It must
        have the same shape and buffer length as the expected output
        but the type will be cast if necessary. See `doc.ufuncs`
        (Section "Output arguments") for more details.

    Returns
    -------
    cumsum_along_axis : ndarray.
        A new array holding the result is returned unless `out` is
        specified, in which case a reference to `out` is returned. The
        result has the same size as `a`, and the same shape as `a` if
        `axis` is not None or `a` is a 1-d array.


    See Also
    --------
    sum : Sum array elements.

    trapz : Integration of array values using the composite trapezoidal rule.

    diff :  Calculate the n-th discrete difference along given axis.

    Notes
    -----
    Arithmetic is modular when using integer types, and no error is
    raised on overflow.

    Examples
    --------
    >>> a = np.array([[1,2,3], [4,5,6]])
    >>> a
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> np.cumsum(a)
    array([ 1,  3,  6, 10, 15, 21])
    >>> np.cumsum(a, dtype=float)     # specifies type of output value(s)
    array([  1.,   3.,   6.,  10.,  15.,  21.])

    >>> np.cumsum(a,axis=0)      # sum over rows for each of the 3 columns
    array([[1, 2, 3],
           [5, 7, 9]])
    >>> np.cumsum(a,axis=1)      # sum over columns for each of the 2 rows
    array([[ 1,  3,  6],
           [ 4,  9, 15]])

    """
    try:
        cumsum = a.cumsum
    except AttributeError:
        return _wrapit(a, 'cumsum', axis, dtype, out)
    return cumsum(axis, dtype, out)

>>> 

But only work for functions that are in numpy, you can do tolist, because it is a sub-function, meaning, it is np.array.tolist, not just np.tolist, i am giving cumsum as an example.

1 Comment

Notice that all this np.cumsum code does it delegate the action to .cumsum method. That method is 'builtin'. I do know from other reading and experience that np.cumsum is similar to np.add.accumulate. But that too is builtin.
0

If you are using an IDE, they commonly have "code navigation" tools, which allow you to go directly to the definition of an object (seems to work fine with numpy in my case).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.