0
def func(input):
    pass
a = np.random.randint(0,10,size=(1000,1000,5,6))
for i in range(1000):
    for j in range(1000):
        a[i][j]=func(a[i][j])

The loops waste too much time. Is there a way to implement loops through numpy's own functions?

Ce Jiang
  • 3
  • 2
  • This kind of loops is exactly what you want to avoid by using numpy. You want to operate on the whole ndarrays at once. Usually this is possible, but it depends on the contents of func. Take a look at [https://docs.scipy.org/doc/numpy/user/quickstart.html] . – Koen G. Oct 31 '19 at 08:36
  • Read this : https://stackoverflow.com/questions/35215161/most-efficient-way-to-map-function-over-numpy-array – jkhadka Oct 31 '19 at 08:41
  • 1
    If `func` can only work with one scalar element at a time, there is much you can do. You have to call it once for each element. `numpy` can't compile your function. – hpaulj Oct 31 '19 at 16:17

0 Answers0