Efficiently get indices of histogram bins in Python

前端 未结 5 1213
清歌不尽
清歌不尽 2020-12-13 19:12

Short Question

I have a large 10000x10000 elements image, which I bin into a few hundred different sectors/bins. I then need to perform some iterative calculation

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 19:54

    Pandas has a very fast grouping code (I think it's written in C), so if you don't mind loading the library you could do that :

    import pandas as pd
    
    pdata=pd.DataFrame({'vals':vals,'ind':ind})
    resultsp = pdata.groupby('ind').sum().values
    

    or more generally :

    pdata=pd.DataFrame({'vals':vals,'ind':ind})
    resultsp = pdata.groupby('ind').agg(func).values
    

    Although the latter is slower for standard aggregation functions (like sum, mean, etc)

提交回复
热议问题