Numpy rebinning a 2D array

后端 未结 3 1568
孤街浪徒
孤街浪徒 2021-02-08 04:50

I am looking for a fast formulation to do a numerical binning of a 2D numpy array. By binning I mean calculate submatrix averages or cumulative values. For ex. x = numpy.arange(

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-08 05:37

    I assume that you only want to know how to generally build a function that performs well and does something with arrays, just like numpy.reshape in your example. So if performance really matters and you're already using numpy, you can write your own C code for that, like numpy does. For example, the implementation of arange is completely in C. Almost everything with numpy which matters in terms of performance is implemented in C.

    However, before doing so you should try to implement the code in python and see if the performance is good enough. Try do make the python code as efficient as possible. If it still doesn't suit your performance needs, go the C way.

    You may read about that in the docs.

提交回复
热议问题