Python/Numpy - Masked Arrays are Very Slow

前端 未结 1 1984
庸人自扰
庸人自扰 2021-02-12 09:33

Is there anything I can do to speed up masked arrays in numpy? I had a terribly inefficient function that I re-wrote to use masked arrays (where I could just mask rows instead

1条回答
  •  一整个雨季
    2021-02-12 10:12

    I have no idea why the masked array functions are moving so slowly, but since it sounds like you are using the mask to select rows (as opposed to individual values), you can create a regular array from the masked rows and use the np function instead:

    b.mask = np.zeros(500)
    b.mask[498] = True
    t = timeit.Timer('c=b.view(np.ndarray)[~b.mask[:,0]]; np.apply_along_axis(test, 1, c)','from __main__ import test, b, ma, np')
    print round(t.timeit(100), 6)
    

    Better yet, don't use masked arrays at all; just maintain your data and a 1D mask array as separate variables:

    a = np.arange(1000).reshape(500, 2)
    mask = np.ones(a.shape[0], dtype=bool)
    mask[498] = False
    out = np.apply_along_axis(test, 1, a[mask])
    

    0 讨论(0)
提交回复
热议问题